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.

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.

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.

Priyanka said:   10 years ago
Option B is correct. When we use virtual function for declaring base class then only it will calls to derived version of function as late binding takes place.

If we don't use virtual function for declaring base class then it will calls base class function. Because early binding takes place.

Abhishek Verma said:   9 years ago
I think B is the right answer if you want to call a derive class same prototype function you have to use virtual concept.

Anisha said:   9 years ago
Yes, it's B. Agree with the above explanation.

Ramanagarjuna R K said:   9 years ago
Yes, only base class function will be invoked irrespective of an object.

So, the answer is B.

Mangesh said:   9 years ago
D option is correct without doubt. I agree with the given answer.

Swapnil said:   8 years ago
If virtual not used so only baser class function is called. If virtual keyword used so both can be called.

Suyog said:   8 years ago
Please, anybody give me the right answer.

Akhil said:   8 years ago
Yes, if you don't give a virtual key word in base class, the function in the base-class gets executed even if you call derived class object. If you use the virtual key word before function in the base class you can call any of the function.


Post your comments here:

Your comments will be displayed after verification.