/* 程序11.3:基类和包含类初始化.cpp:*/
#include //包含头文件
#include //包含头文件
#include //包含头文件
using namespace std; //使用名字空间std
class Point //声明基类Point
{
private:
float fX,fY;
public:
Point(float X=0,float Y=0) //声明基类构造符函数
{
fX=X; fY=Y;
}
float GetX() //通过公共成员函数访问私有成员变量
{
return fX;
}
float GetY()
{
return fY;
}
};
class Line //声明子类Line
{
public:
Point P1,P2; //包含两个点对象
Line(float X1=0,float Y1=0,
float X2=0,float Y2=0):P1(X1,Y1),P2(X2,Y2)
{
//不做任何事情;
}
float Length(Point P1,Point P2)
{
float L1;
float XZ=P1.GetX()-P2.GetX();
float YZ=P1.GetY()-P2.GetY();
L1=sqrt(pow(XZ,2)+pow(YZ,2));
return L1;
}
void Display(string S1,string S2)
{
cout }
};
class Triangle:public Line //声明子类Triangle
{
private:
Point P3;
public:
Triangle(float X1=0,float Y1=0,float X2=0,float Y2=0,
float X3=0,float Y3=0):Line(X1,Y1,X2,Y2),P3(X3,Y3)
{
//不做任何事情;
}
float TriLength()
{
float TriL1;
TriL1=Length(P1,P2)
+Length(P2,P3)
+Length(P3,P1);
cout return TriL1;
}
};
int main()
{
Triangle obj(0,0,10,0,0,10); //声明子类对象
obj.TriLength();
return 0;
}