📄 p8_4.cpp
字号:
/**************************************
* p8_4.cpp *
* 从circle类公有派生出圆柱类Cylinder *
* 演示类的兼容性 *
****************************************/
#include"Circle.h"
class Cylinder: public Circle
{
private:
double height;
public:
Cylinder(int X, int Y, double R, double H):Circle(X,Y,R)
{
height=H;
}
void ShowCylinder()
{
ShowCircle();
cout<<"height of cylinder:"<<height<<endl;
}
};
void main()
{
Point P(1,1); //Point类对象
Circle Cir(20,20,15.5); //Circle类对象
Cylinder CY(300,300,15.5,50); //Cylinder类对象
Point *Pp; //point类指针
Pp=&P; //将派生类对象地址赋给指向基类的指针
Pp->ShowXY();
Pp=&Cir; //将派生类对象地址赋给指向基类的指针
Pp->ShowXY();
Pp=&CY; //将派生类对象地址赋给指向基类的指针
Pp->ShowXY();
Circle & RC=CY; //Circle类引用引用了派生类Cylinder对象
RC.ShowXY();
P=Cir; //Circle类对象赋值给基类Point类对象
P.ShowXY();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -