Enclave_ 2022-04-01 09:55 采纳率: 85.2%
浏览 24
已结题

点为基类 用虚函数求面积的问题

运行报错 编译不报错 不会改了

img


#include <iostream>
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;   /*面积*/
};

class Circle : public Plane, public Point
{
private:
    double X;
    double Y;
    double radius;

protected:
    static double PI;

public:
    Circle(double x = 0, double y = 0, double r = 0);
    Circle(const Circle &);
    ~Circle();
    void setR(double);
    double getR() const;
    void show() const;
    double area();
    double length() const;
    virtual double length() const
    {
        return 2 * PI * radius;
    }
    virtual double area() const
    {
        return PI * radius * radius;
    }
};
double Circle::PI = 3.14159;
Circle::Circle(double x, double y, double r) : Point(x, y)
{
    X = Point::getX();
    Y = Point::getY();
    radius = r;
    cout << "Circle Constructor run" << endl;
}
Circle::Circle(const Circle &c)
{
    X = c.X;
    Y = c.Y;
    radius = c.radius;
    cout << "Circle CopyConstructor run" << endl;
}
Circle::~Circle()
{
    cout << "Circle Destructor run" << endl;
}
void Circle::setR(double r)
{
    radius = r;
}
double Circle::getR() const
{
    return radius;
}
void Circle::show() const
{
    cout << "Circle(Point(" << X << "," << Y << ")"
         << ",Radius=" << radius << endl;
}
double Circle::area()
{
    return PI * radius * radius;
}
double Circle::length() const
{
    return 2 * PI * radius;
}
/*double Circle::length() const
{
    return 2 * PI * radius;
}
double Circle::area() const
{
    return PI * radius * radius;
}*/
void show(Point *p)
{ /*点基类的显示函数*/
    p->show();
}
void length(Plane *p)
{ /*平面图形的周长函数*/
    cout << "Length=" << p->length() << endl;
}
void area(Plane &p)
{ /*平面图形的面积函数*/
    cout << "Area=" << p.area() << endl;
}
//主函数
int main(void)
{
    double x, y, r;
    Circle c1, c2(c1);
    show(&c1);
    cout << endl;
    area(c1);
    length(&c1);
    cin >> x >> y >> r;
    c2.setX(x);
    c2.setY(y);
    c2.setR(r);
    show(&c2);
    cout << endl;
    area(c2);
    length(&c2);
}

  • 写回答

1条回答 默认 最新

  • 真相重于对错 2022-04-01 10:19
    关注

    circle 里面有3个length

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月10日
  • 已采纳回答 4月2日
  • 创建了问题 4月1日

悬赏问题

  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动