📄 demo_inheritance_1_a.cpp
字号:
//***************************************************
// 公有继承类
//***************************************************
#include<iostream.h>
#include<math.h>
class Point //基类Point类的定义
{
public:
void InitP(float xx=0, float yy=0) {X=xx;Y=yy;}
void Move(float xOff, float yOff) {X+=xOff;Y+=yOff;}
float GetX() {return X;}
float GetY() {return Y;}
private:
float X,Y;
};
class Rectangle: public Point //公有派生类声明部分
{
public: //新增公有函数成员
void InitR(float x, float y, float w, float h)
{ InitP(x,y); W=w; H=h; } //调用基类公有成员函数
float GetH() {return H;}
float GetW() {return W;}
private: //新增私有数据成员
float W,H;
};
void main()
{
Rectangle rect; //定义Rectangle类的对象
rect.InitR(2,3,20,10); //设置矩形的数据
rect.Move(3,2); //移动矩形位置
cout<<"The data of rect(X,Y,W,H):"<<endl;
cout<<rect.GetX()<<"," //输出矩形的特征参数
<<rect.GetY()<<","
<<rect.GetW()<<","
<<rect.GetH()<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -