C++编程题(急急急)

来源:百度知道 编辑:UC知道 时间:2024/06/07 18:32:59
定义一个表示点的结构类型Point和一个由直线方程y=ax+b确定的直线类Line。结构类型Point有x和y两个成员,分别表示点的横坐标和纵坐标。Line类有两个数据成员a和b,分别表示直线方程中的系数a和b。Line类有一个成员函数print用于显示直线方程;友员函数setPoint(Line &11,Line &12)用于求两条直线的交点。在main函数中,建立两个直线对象,分别调用print函数显示两条直线方程,并调用函数setPoint求这两条直线的交点

#include <iostream>
#include <cmath>
using namespace std;
class Point //Point类声明
{
public:
Point(int xx=0, int yy=0) {X=xx;Y=yy;}
Point(Point &p);
int GetX() {return X;}
int GetY() {return Y;}
private:
int X,Y;
};
Point::Point(Point &p) //拷贝构造函数的实现
{
X=p.X;
Y=p.Y;
cout<<"Point拷贝构造函数被调用"<<endl;
}
//类的组合
class Line //Line类的声明
{
public: //外部接口
Line (Point xp1, Point xp2);
Line (Line &);
double GetLen(){return len;}
private: //私有数据成员
Point p1,p2; //Point类的对象p1,p2
double len;
};
//组合类的构造函数
Line:: Line (Point xp1, Point xp2)
:p1(xp1),p2(xp2)
{
cout<<"Line构造函数被调用"<<endl;
double x=double(p1.GetX()-p2.GetX());
double y=double(p1.GetY()-p2.GetY());
len=sqrt(x*x+y*y);
}

//组合类的拷贝构造函数
Line:: Line (Line &Seg): p1(Seg.p1),