#include <iostream>
using namespace std;
class father
{
public:
virtual void run()const{cout<<"父亲可以跑万米\n";} //将该函数说明为虚函数
};
class son:public father
{
public:
void run()const{cout<<"儿子可以跑十万米\n";}
};
class daughter:public father
{
public:
void run()const{cout<<"女儿可以跑十万米\n";}
};
void one(father); //该函数原型中声明一个指向father类的对象
void two(father*); //该函数原型中声明一个指向father类的指针
void three(father&); //该函数原型中声明一个指向father的引用
void main()
{
father *p=0;
int choice;
while(1)
{
bool quit=false;
cout<<"(0)quit (1)son (2)daughter (3)father:";
cin>>choice;
switch(choice)
{
case 0:quit=true;
break;
case 1:p=new son;
one(*p); //将对象作为参数传递给函数one()中
break;
case 2:p=new daughter;
two(p); //将对象的内存地址传递到函数two()中
break;
case 3:p=new father;
three(*p); //将对象的引用传递到函数three()中
break;
default:cout<<"请输入0到3之间的数字\n";
break;
}
if (quit)
break;
}
}
void one(father one) //函数头中接收一个指向father的对象
{
one.run(); //用该对象访问run函数
}
void two(father *two) //函数头中接收一个指向father的指针
{
two->run(); //用该指针访问run()函数
}
void three (father &three) //函数头中接收一个指向father的引用
{
three.run(); //用该引用访问run()函数
}
using namespace std;
class father
{
public:
virtual void run()const{cout<<"父亲可以跑万米\n";} //将该函数说明为虚函数
};
class son:public father
{
public:
void run()const{cout<<"儿子可以跑十万米\n";}
};
class daughter:public father
{
public:
void run()const{cout<<"女儿可以跑十万米\n";}
};
void one(father); //该函数原型中声明一个指向father类的对象
void two(father*); //该函数原型中声明一个指向father类的指针
void three(father&); //该函数原型中声明一个指向father的引用
void main()
{
father *p=0;
int choice;
while(1)
{
bool quit=false;
cout<<"(0)quit (1)son (2)daughter (3)father:";
cin>>choice;
switch(choice)
{
case 0:quit=true;
break;
case 1:p=new son;
one(*p); //将对象作为参数传递给函数one()中
break;
case 2:p=new daughter;
two(p); //将对象的内存地址传递到函数two()中
break;
case 3:p=new father;
three(*p); //将对象的引用传递到函数three()中
break;
default:cout<<"请输入0到3之间的数字\n";
break;
}
if (quit)
break;
}
}
void one(father one) //函数头中接收一个指向father的对象
{
one.run(); //用该对象访问run函数
}
void two(father *two) //函数头中接收一个指向father的指针
{
two->run(); //用该指针访问run()函数
}
void three (father &three) //函数头中接收一个指向father的引用
{
three.run(); //用该引用访问run()函数
}