C++ | Inheritance | Question 8

using namespace std;

class Base
    int fun()  { cout << "Base::fun() called"; }
    int fun(int i)  { cout << "Base::fun(int i) called"; }

class Derived: public Base
    int fun() {  cout << "Derived::fun() called"; }

int main()
    Derived d;
    return 0;

(A) Base::fun(int i) called
(B) Derived::fun() called
(C) Base::fun() called
(D) Compiler Error

Answer: (D)

Explanation: If a derived class writes its own method, then all functions of base class with same name become hidden, even if signaures of base class functions are different.

In the above question, when fun() is rewritten in Derived, it hides both fun() and fun(int) of base class.

Quiz of this Question

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:

0 Average Difficulty : 0/5.0
No votes yet.