已知表示点的类CPoint和表示线段的CLine类
类CPoint包含:
(1)表达点位置的私有数据成员x,y
(2)构造函数及复制构造函数
类CLine包含:
(1)两个CPoint的点对象(该两点分别为线段的两个端点)
(2)构造函数(提示:构造函数中用初始化列表对内嵌对象进行初始化)
(3)公有成员函数GetLen,其功能为返回线段的长度,返回值类型为整型
(4)类属性成员count用于记录创建的CLine类对象的个数,及用于显示count值的ShowCount函数;
要求: (1)实现满足上述属性和行为的CPoint类及CLine类定义;
(2)保证如下主函数能正确运行。
#include<iostream>
#include<cmath>
using namespace std;
class CPoint
{
private:
int x,y;
public:
CPoint()
{
x=0;
y=0;
}
CPoint(int a,int b)
{
x=a;
y=b;
}
CPoint(const CPoint & point)
{
x=point.x;
y=point.y;
}
int getx()
{
return x;
}
int gety()
{
return y;
}
};
class CLine
{
public:
CLine(const CPoint &point1,const CPoint &point2)
{
count++;
exm1=point1;
exm2=point2;
}
CLine(const CLine &line)
{
count++;
exm1=line.exm1;
exm2=line.exm2;
}
int GetLen()
{
return sqrt(pow(exm1.getx() - exm2.getx(), 2) + pow(exm1.gety() - exm2.gety(), 2));
}
static int ShowCount()
{
return count;
}
~CLine()
{
--count;
}
private:
CPoint exm1;
CPoint exm2;
static int count;
};
int CLine::count=0;
int main(){
int x,y;
cin>>x>>y;
CPoint p1(x,y);
cin>>x>>y;
CPoint p2(x,y);
CLine line1(p1,p2);
cout<<"the length of line1 is:"<<line1.GetLen()<<endl;
CLine line2(line1);
cout<<"the length of line2 is:"<<line2.GetLen()<<endl;
cout<<"the count of CLine is:"<<CLine::ShowCount()<<endl;
return 0;
}