Answer to Question #180171 in C++ for Saurabh Singh

Question #180171

Suppose, the same function is defined in both the derived class and the based class. Now if we call this function using the object of the derived class. Compiler will call which function and elaborate the same used by the compiler.


1
Expert's answer
2021-04-10T12:23:29-0400
#include<iostream>
using namespace std;
 
class Base
{
   int x;
public:
    virtual void fun() = 0;
    int getX() { return x; }
};
 
// This class inherits from Base and implements fun()
class Derived: public Base
{
    int y;
public:
    void fun() { cout << "fun() called"; }
};
 
int main(void)
{
    Derived d;
    d.fun();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS