Enclave_ 2022-04-03 20:12 采纳率: 88.5%
浏览 61
已结题

类构造函数、拷贝构造函数输出的问题

这是题目答案要求的的输出:

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行就有些不同

img


明明我调用了拷贝构造 但还是输出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;
}

  • 写回答

2条回答 默认 最新

  • [PE]经典八炮 2022-04-03 20:35
    关注
    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 Point &a, const Point &b, const Point &c):A(a),B(b),C(c)
    {
        cout << "Triangle Constructor run" << endl;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月12日
  • 已采纳回答 4月4日
  • 创建了问题 4月3日

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂