p8_4.cpp

来自「相当丰富的C++源码」· C++ 代码 · 共 39 行

CPP
39
字号
/**************************************
* 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 + =
减小字号Ctrl + -
显示快捷键?