#include<iostream>
using namespace std;
class BaseClass
{
public:
void fn2()
{
cout << "我是base的fn2" << endl;
}
void fn1()
{
cout << "我是base的fn1" << endl;
}
};
class DerivedClass :public BaseClass
{
public:
void fn2()
{
cout << "我是der 的fn2" << endl;
}
void fn1()
{
cout << "我是der 的fn1" << endl;
}
};
int main()
{
DerivedClass D;
DerivedClass* der = &D;
BaseClass* base = &D;
der->fn1();
der->fn2();
base->fn1();
base->fn2();
}
当基类所使用的函数为非虚函数的输出为:
我是der 的fn1
我是der 的fn2
我是base的fn1
我是base的fn2
#include<iostream>
using namespace std;
class BaseClass
{
public:
void fn2()
{
cout << "我是base的fn2" << endl;
}
virtual void fn1()
{
cout << "我是base的fn1" << endl;
}
};
class DerivedClass :public BaseClass
{
public:
void fn2()
{
cout << "我是der 的fn2" << endl;
}
void fn1()
{
cout << "我是der 的fn1" << endl;
}
};
int main()
{
DerivedClass D;
DerivedClass* der = &D;
BaseClass* base = &D;
der->fn1();
der->fn2();
base->fn1();
base->fn2();
}
当基类所使用的函数为虚函数的输出为:
我是der 的fn1
我是der 的fn2
我是der 的fn1
我是base的fn2