#include "iostream"
using namespace std;
class CALLComers//报名者类
{
public:
int m_bh=0;//编号
virtual void show(){cout<<"CALLComers::show():我是"<<m_bh<<"号。"<<endl;}
virtual void show(int a){cout<<"CALLComers::show():我是"<<m_bh<<"号。"<<endl;}
};
class CGirl:public CALLComers
{//超女类
public:
int m_age=0;//年龄
void show(){
cout<<"CGirl::show()我是:"<<m_bh<<"号,"<<m_age<<endl;
}
void show(int a){
cout<<"CGirl::show()我是:"<<m_bh<<"号,"<<m_age<<endl;
}
};
int main()
{
CALLComers a;
a.m_bh=3;//创建基类对象并赋值
CGirl g;
g.m_bh=8;//创建派生类对象并赋值
g.m_age=23;
g.show();
CALLComers* p;//声明基类指针
p=&a; p->show();//让基类指针指向基类对象,并调用虚函数.
p=&g; p->show();
p->show(12);//让基类指针指向派生类对象,并调用虚函数.
}
输出:
CGirl::show()我是:8号,23
CALLComers::show():我是3号。
CGirl::show()我是:8号,23
CGirl::show()我是:8号,23
发表回复