c++, inheritance , virtual

#include<iostream>
using namespace std;
class a
{
public:
virtual void add()
{
cout<<"in a class";
}
};
class b: public a
{
public:
void add()
{
cout<<"in b class ";
};
class c : public b
{
};
class d: public c
{
};
int main()
{
c *p;
d q;
p=&q;
p->add();
return 0;
}
// the output of this code Is b::add()
but we are working with class c and d can anyone explain me ?
Sort out your terrible indentation and use code tags please.
You would then see that you've missed a closing brace for your class b.

but we are working with class c and d can anyone explain me ?

you need to override add() in your other classes.
Last edited on
Because c and d classes does not overload add() function, so latest overloaded function is used.
Topic archived. No new replies allowed.