C++ Programming - OOPS Concepts - Discussion

Discussion Forum : OOPS Concepts - General Questions (Q.No. 38)
38.
What happens if the base and derived class contains definition of a function with same prototype?
Compiler reports an error on compilation.
Only base class function will get called irrespective of object.
Only derived class function will get called irrespective of object.
Base class object will call base class function and derived class object will call derived class function.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
22 comments Page 2 of 3.

Dhananjay said:   1 decade ago
I will also go with option C. But option D is right, when we will make explicit calls to both functions present in Base and Derived.

So, if we do not make any explicit call to the base class function, then it will call function of Derived class, as we are creating the object of the same.

Poonam said:   1 decade ago
No its wrong answer. Derived class object will call always derived class function irrespective of base class. This is called as function overriding.

Pooja said:   1 decade ago
It will raised an ambiguity error cause compiler will confused when we create the object of derived class.

Yogita said:   1 decade ago
I also agree with option B because both class contain same prototype. Prefrence gives to base class.

Abdul Rizwan said:   1 decade ago
Ya ! answer is B only.

Base class method will call all the time even we are calling derived class method so to reduce this problem we need to use Virtual function concept.

Pallavi said:   1 decade ago
We can declare both base & derived class objects and as per that we can directly call the functions of that class.

Shraddha said:   1 decade ago
Only base class constructor is called.

Sonu said:   1 decade ago
I am confused about objects but yes when we use base class pointer base class function is called irrespective of the address to which it is pointing to.

Mohit said:   1 decade ago
The correct answer is B.

If you call function with derived class object it calls base class function.

To prevent this you have to use virtual function concept.

Sona said:   1 decade ago
The answer given is right as it totally depends on calling class object.


Post your comments here:

Your comments will be displayed after verification.