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 2 of 3.
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.
Vishal Tavande said:
1 decade ago
Only base class function will get called irrespective of object. As I tried this at my desk.
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.
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.
If you call function with derived class object it calls base class function.
To prevent this you have to use virtual function concept.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers