这是题目答案要求的的输出:
Point Constructor run
Point Constructor run
Point Constructor run
Point Constructor run
Point Constructor run
Point Constructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Triangle Constructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Triangle CopyConstructor run
Triangle(A=Point(X=0,Y=0),B=Point(X=0,Y=0),C=Point(X=0,Y=0))
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Area=0
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Length=0
Triangle(A=Point(X=3,Y=2),B=Point(X=1,Y=1),C=Point(X=2,Y=2))
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Area=0.5
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Length=4.65028
Triangle Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Triangle Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
这是我的输出:
Point Constructor run
Point Constructor run
Point Constructor run
Point Constructor run
Point Constructor run
Point Constructor run
Point Constructor run
Point Constructor run
Point Constructor run
Triangle Constructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Constructor run
Point Constructor run
Point Constructor run
Triangle CopyConstructor run
Triangle(A=Point(X=0,Y=0),B=Point(X=0,Y=0),C=Point(X=0,Y=0))
Area=Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
0
Length=Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
0
Triangle(A=Point(X=3,Y=2),B=Point(X=1,Y=1),C=Point(X=2,Y=2))
Area=Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
0.5
Length=Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Point CopyConstructor run
Point CopyConstructor run
Point Destructor run
Point Destructor run
Triangle Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Triangle Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
Point Destructor run
可以发现输出出现了错位 并且输出有些地方不符合答案比如我输出的第7 8 9行就有些不同
明明我调用了拷贝构造 但还是输出Point Constructor run 而不是 Point CopyConstructor run
但程序又能正常运行 算出的数据也对
不会改了
下面是我的代码
#include <iostream>
#include <cmath>
using namespace std;
//点类Point
class Point
{
private:
double x;
double y;
public:
Point(double xv = 0, double yv = 0); /*构造函数*/
Point(const Point &p); /*拷贝构造*/
~Point(); /*析构函数*/
virtual void show() const; /*显示*/
void setX(double xv); /*设置X坐标*/
void setY(double yv); /*设置Y坐标*/
double getX() const; /*获取X坐标*/
double getY() const; /*获取Y坐标*/
};
Point::Point(const double xv, const double yv)
{ /*构造函数*/
x = xv;
y = yv;
cout << "Point Constructor run" << endl;
}
Point::Point(const Point &p)
{ /*拷贝构造*/
x = p.x;
y = p.y;
cout << "Point CopyConstructor run" << endl;
}
Point::~Point()
{ /*析构函数*/
cout << "Point Destructor run" << endl;
}
void Point::show() const
{ /*显示*/
cout << "Point(X=" << x << ",Y=" << y << ")";
}
void Point::setX(double xv)
{ /*设置X坐标*/
x = xv;
}
void Point::setY(double yv)
{ /*设置Y坐标*/
y = yv;
}
double Point::getX() const
{ /*获取X坐标*/
return x;
}
double Point::getY() const
{ /*获取Y坐标*/
return y;
}
class Plane
{ /*平面图形基类*/
public:
virtual double length() const = 0; /*周长*/
virtual double area() const = 0; /*面积*/
};
double pointDistance(Point p1, Point p2)
{
double distance = 0;
distance = sqrt((p1.getY() - p2.getY()) * (p1.getY() - p2.getY()) + (p1.getX() - p2.getX()) * (p1.getX() - p2.getX()));
return distance;
}
class Triangle : public Plane
{
private:
Point A;
Point B;
Point C;
public:
Triangle(const Point &a = Point(0, 0), const Point &b = Point(0, 0), const Point &c = Point(0, 0));
Triangle(const Triangle &);
~Triangle();
void setA(const Point &);
Point getA() const;
void setB(const Point &);
Point getB() const;
void setC(const Point &);
Point getC() const;
void show() const;
double area() const;
double length() const;
};
Triangle::Triangle(const Point &a, const Point &b, const Point &c)
{
A = a;
B = b;
C = c;
cout << "Triangle Constructor run" << endl;
}
Triangle::Triangle(const Triangle &t)
{
A = t.A;
B = t.B;
C = t.C;
cout << "Triangle CopyConstructor run" << endl;
}
Triangle::~Triangle()
{
cout << "Triangle Destructor run" << endl;
}
void Triangle::setA(const Point &a)
{
A = a;
}
Point Triangle::getA() const
{
return A;
}
void Triangle::setB(const Point &b)
{
B = b;
}
Point Triangle::getB() const
{
return B;
}
void Triangle::setC(const Point &c)
{
C = c;
}
Point Triangle::getC() const
{
return C;
}
void Triangle::show() const
{
cout << "Triangle(A=Point(X=" << A.getX() << ",Y=" << A.getY() << "),B=Point(X=" << B.getX() << ",Y=" << B.getY() << "),C=Point(X=" << C.getX() << ",Y=" << C.getY() << "))";
}
double Triangle::area() const
{
double area = 0;
double a, b, c, s;
a = pointDistance(A, B);
b = pointDistance(B, C);
c = pointDistance(A, C);
s = 0.5 * (a + b + c);
area = sqrt(s * (s - a) * (s - b) * (s - c));
return area;
}
double Triangle::length() const
{
double length = 0;
double a, b, c;
a = pointDistance(A, B);
b = pointDistance(B, C);
c = pointDistance(A, C);
length = a + b + c;
return length;
}
void length(Plane *p)
{ /*平面图形的周长函数*/
cout << "Length=" << p->length() << endl;
}
void area(Plane &p)
{ /*平面图形的面积函数*/
cout << "Area=" << p.area() << endl;
}
//主函数
int main(void)
{
double x, y;
Point p1, p2(1, 1), p3(2, 2);
Triangle t1, t2(t1);
t1.show();
cout << endl;
area(t1);
length(&t1);
cin >> x >> y;
p1.setX(x);
p1.setY(y);
t2.setA(p1);
t2.setB(p2);
t2.setC(p3);
t2.show();
cout << endl;
area(t2);
length(&t2);
return 0;
}