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?
Discussion:
22 comments Page 1 of 3.
Ankit said:
8 years ago
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.
#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.
(1)
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.
If we don't use virtual function for declaring base class then it will calls base class function. Because early binding takes place.
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.
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.
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.
Khushbu said:
8 years ago
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.
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.
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.
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.
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:
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.
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers