Enclave_ 2022-04-04 20:13 采纳率: 88.5%
浏览 73
已结题

c++类的输出顺序问题 出现行错位 输出顺序错误的情况

程序可以运行 的数也是对的 但是输出出现错位 而且有些输出的地方不一样
这是我和答案输出的不同

img


img


img

img

可以看到应该是length=答案 area=答案 但是length area=和答案输出错了位 差了好几行
而且有些地方应该输出copyconstructor run但却输出成destructor run
有些地方应该输出destructor run却成了copyconstructor run

下面是答案要求得到的输出

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 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))
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
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

下面是我的代码

注意1行到61行 和152行到182行是题里给的程序测试样例 是改不了的 其他地方可以改

#include <iostream>
#include <cmath>
using namespace std;
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);                
    void setY(double yv);                
    double getX() const;                 
    double getY() const;                 
};
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 = xv;
}
void Point::setY(double yv)
{ 
    y = yv;
}
double Point::getX() const
{ 
    return x;
}
double Point::getY() const
{ 
    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条回答 默认 最新

  • 有问必答小助手 2022-04-06 10:16
    关注

    你好,我是有问必答小助手,本次您提出的有问必答问题,已经由其他小伙伴为您做出解答,并解决了您的问题。

    本次提问扣除的有问必答次数,已经为您补发到账户,我们后续会持续优化,扩大我们的服务范围,为您带来更好地服务。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 4月14日
  • 已采纳回答 4月6日
  • 修改了问题 4月4日
  • 创建了问题 4月4日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度