📄 exp15_1.cpp
字号:
/*范例:定义一个继承与派生关系的类体系,在派生类中访问基类成员。
[要求]定义一个点类,包含x,y坐标数据成员,显示函数和计算面积的函数成员;以点为基类
派生一个圆类,增加表示半径的数据成员,重载显示和计算面积的函数;定义一个直线类,
以两个点类对象作数据成员,定义显示、求面积及长度函数。编程测试所定义的类体系。
*/
#include <iostream.h>
#include <math.h>
#define PI 3.14159
class Point{
friend class Line;
protected:
double x, y ;
public:
Point(){x = 0 ; y = 0 ; }
Point(double xv,double yv){ x = xv; y = yv; }
double Area(){return 0;}
void Show() {
cout<<"x="<<x<<' '<<"y="<<y<<endl;
}
};
class Circle :public Point{
double radius;
public:
Circle(){ x = 0; y = 0; radius = 0; }
Circle(double xv,double yv,double vv):Point(xv,yv){ //调用基类构造函数
radius = vv;
}
double Area(){
return PI*radius*radius;
}
void Show(){
//访问基类的数据成员
cout<<"x="<<x<<' '<<"y="<<y<<" radius="<<radius<<endl;
}
};
class Line{
Point start,end; //对象成员
public:
Line():start(0,0),end(0,0){ } //对象成员初始化
Line(double xv1,double yv1,double xv2,double yv2) :
start(xv1,yv1),end(xv2,yv2){ }
double GetLength() {
return sqrt((start.x-end.x)*(start.x-end.x)+(start.y-end.y)*(start.y-end.y));
}
double Area(){return 0;}
void Show(){
cout<<"start point:\n";
start.Show();
cout<<"end point:\n";
end.Show();
}
};
void main(){
Point pt(0,0);
Circle cl(100,100,10);
Line ln1(0,0,100,100);
cout<<pt.Area()<<endl;
pt.Show();
cout<<cl.Area()<<endl;
cl.Show();
cout<<ln1. Area()<<'\t'<<ln1. GetLength()<<endl;
ln1.Show();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -