C++ Programming - OOPS Concepts - Discussion

Discussion :: OOPS Concepts - General Questions (Q.No.38)

38. 

What happens if the base and derived class contains definition of a function with same prototype?

[A]. Compiler reports an error on compilation.
[B]. Only base class function will get called irrespective of object.
[C]. Only derived class function will get called irrespective of object.
[D]. Base class object will call base class function and derived class object will call derived class function.

Answer: Option D

Explanation:

No answer description available for this question.

Vishal Tavande said: (Jul 27, 2012)  
Only base class function will get called irrespective of object. As I tried this at my desk.

Dhanesh Ful said: (Aug 28, 2012)  
This example of method overrding only the the method of derived class is call istead of base class.

Sona said: (Mar 12, 2013)  
The answer given is right as it totally depends on calling class object.

Mohit said: (Aug 9, 2013)  
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.

Sonu said: (Aug 29, 2013)  
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.

Shraddha said: (Oct 30, 2013)  
Only base class constructor is called.

Pallavi said: (Dec 14, 2013)  
We can declare both base & derived class objects and as per that we can directly call the functions of that class.

Abdul Rizwan said: (May 10, 2014)  
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.

Yogita said: (Jun 24, 2014)  
I also agree with option B because both class contain same prototype. Prefrence gives to base class.

Pooja said: (Mar 12, 2015)  
It will raised an ambiguity error cause compiler will confused when we create the object of derived class.

Poonam said: (Jun 12, 2015)  
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: (Jul 4, 2015)  
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: (Jan 1, 2016)  
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: (Sep 13, 2016)  
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: (Nov 5, 2016)  
Yes, it's B. Agree with the above explanation.

Ramanagarjuna R K said: (Jan 2, 2017)  
Yes, only base class function will be invoked irrespective of an object.

So, the answer is B.

Mangesh said: (Jan 8, 2017)  
D option is correct without doubt. I agree with the given answer.

Swapnil said: (May 7, 2017)  
If virtual not used so only baser class function is called. If virtual keyword used so both can be called.

Suyog said: (Jul 2, 2017)  
Please, anybody give me the right answer.

Akhil said: (Sep 2, 2017)  
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.

Khushbu said: (Sep 8, 2017)  
This is wrong only base class is executed bcoj both have same prototype if we call them reverse ie base class as derived or vice versa then what is the difference between them.

Ankit said: (Dec 1, 2017)  
Given answer is correct.

#include <iostream>
using namespace std;
class A
{
public:
void f() { cout << "A plain" << endl; }
};
class B : public A
{ public:
void f() { cout << "B plain" << endl; }
};
int main(){
A a;
B b;
a.f();
b.f();
}
o/p : A plain
B plain

Reason : It does so because methods of both classes are unknown to each other. They do need 'virtual' keyword if u intend to use method overriding i.e. Polymorphism which implemented using references and pointers.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.