#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
class Piont {//类的定义
public:
Piont(int xx = 0, int yy = 0) {
x = xx;
y = yy;
}
Piont(Piont& p);
int getX() { return x; }
int getY() { return y; }
private:
int x, y;
};
Piont::Piont(Piont& p) {//复制构造函数的实现
x = p.getX();
y = p.getY();
cout << "Calling the copy constructor of Piont" << endl;
}
//类的组合
class Line {//Line类的定义
public://外部接口
Line(Piont xp1, Piont xp2);
Line(Line& l);
double getLen() { return len; }
private:
Piont p1, p2;
double len;
};
//组合类的构造函数
Line::Line(Piont xp1, Piont xp2) :p1(xp1), p2(xp2) {
cout << "线段类型的构造函数" << endl;
double x=static_cast<double>(p1.getX() - p2.getX());
double y=static_cast<double>(p1.getY() - p2.getY());
len = sqrt(x * x + y * y);
}
int main()
{
Piont myp1(1, 1), myp2(4, 5);//建立Piont类的对象
Line line(myp1, myp2);//建立Line类的对象
Line line2(line);//利用复制构造函数建立一个新对象
cout << "这个线段的长度是:";
cout << line.getLen() << endl;
cout << "线段2的长度是:";
cout << line2.getLen() << endl;
return 0;
}
复制构造函数没有实现
你只知道有错误
都不看错误提示的吗
告诉你了是Line(Line& l);这一行出错了
很明显的这个函数只有声明,没定义函数体