Enclave_ 2022-04-01 09:55 采纳率: 88.5%
浏览 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日

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助