#include <iostream>
using namespace std;
class Base1 {
public:
Base1(){}
void display();
};
class Base2:public Base1 {
public:
Base2(){}
void display();
};
class Derive:public Base2 {
public:
Derive(){}
void display();
};
void Base1::display()
{
cout<<"Base1::fun()"<<endl;
}
void Base2::display()
{
cout<<"Base2::fun()"<<endl;
}
void Derive::display()
{
cout<<"Derive::fun()"<<endl;
}
void fun(Base1 *p)
{
p->display();
}
int main()
{
Base1 b1;
Base2 b2;
Derive d;
fun(&b1);
fun(&b2);
fun(&d);
}
<span style="font-size:14px;">
</span>
$ g++ -o test2 test2.cpp
$ ./test2
Base1::fun()
Base1::fun()
Base1::fun()
#include <iostream>
using namespace std;
class Base1 {
public:
Base1(){}
virtual void display();
};
class Base2:public Base1 {
public:
Base2(){}
void display();
};
class Derive:public Base2 {
public:
Derive(){}
void display();
};
void Base1::display()
{
cout<<"Base1::fun()"<<endl;
}
void Base2::display()
{
cout<<"Base2::fun()"<<endl;
}
void Derive::display()
{
cout<<"Derive::fun()"<<endl;
}
void fun(Base1 *p)
{
p->display();
}
int main()
{
Base1 b1;
Base2 b2;
Derive d;
fun(&b1);
fun(&b2);
fun(&d);
}
$ g++ -o test3 test3.cpp
$ ./test3
Base1::fun()
Base2::fun()
Derive::fun()