📄 608a.cpp
字号:
//608a.CPP demo polimorphism and virtual function
#include <iostream.h>
#include <cstring.h>
class Person
{
protected:
string name;
public:
Person(){name="";}
Person(string n){name=n;}
void PutName(string s) { name=s; }
virtual void ShowInfo(void) { cout <<"Name is " <<name<<endl; }
};
class Address:public Person
{
string addr;
public:
Address():Person(){addr="";}
Address(string aa,string nn):Person(nn){addr=aa;}
void PutAddress(string a) { addr=a; }
void ShowInfo(void)
{ cout <<"Name is " <<name << " Addr. is "<<addr << endl; }
};
main(void)
{
Person *prtP, objP("PeiPei");
Address objA("New Zeland","Black");
prtP=&objP; // address of base
prtP->ShowInfo( );
prtP=&objA; // Access class Address via base pointer.
prtP->ShowInfo( );
return 0;
}
/*
Name is PeiPei
Name is Black Addr. is New Zeland
*/
/* if without virtual:
Name is PeiPei
Name is Black
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -