Enclave_ 2022-04-04 21:00 采纳率: 88.5%
浏览 92
已结题

c++基类的成员函数调用无效的问题

一个圆类Circle及立体图形类Solid为基础设计球类Sphere的问题
其实这个问题很好理解 没啥难度的 但就是输出不对
如果我输入4.0
得到应该的到两个不同的球类数据
但是我却得到了两个相同的数据
可是测试样例里明明有s2.setR(r);啊
这是和答案输出样例的不同

img

这是答案给的输出样例

Point Constructor run
Circle Constructor run
Sphere Constructor run
Point CopyConstructor run
Circle CopyConstructor run
Sphere CopyConstructor run
Sphere(Circle(Point(X=1,Y=2),Radius=3))
Area=28.2743
Length=18.8495
S_Area=113.097
Volumn=113.097
Sphere(Circle(Point(X=1,Y=2),Radius=4))
Area=50.2654
Length=25.1327
S_Area=201.062
Volumn=268.082
Sphere Destructor run
Circle Destructor run
Point Destructor run
Sphere Destructor run
Circle Destructor run
Point Destructor run

我的代码啥的其实都没啥细看的必要 我只是想知道在214行父类Circle的数据r改变后 子类sphere如何继承新的r
这是我的代码 而其样例要求55行以前 182行以后不许修改

#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();                           
    void setX(double xv);             
    void setY(double yv);               
    double getX() const;               
    double getY() const;                
    virtual void show() 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::setX(double xv)
{
    x = xv;
}
void Point::setY(double yv)
{ 
    y = yv;
}
double Point::getX() const
{ 
    return x;
}
double Point::getY() const
{
    return y;
}
void Point::show() const
{ 
    cout << "Point(X=" << x << ",Y=" << y << ")";
}

class Plane
{
public:
    virtual double length() const = 0;
    virtual double area() const = 0;
};
class Solid
{
public:
    virtual double volume() const = 0; 
    virtual double s_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() const;
    double length() const;
};
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) : Point(c)
{
    X = c.X;
    Y = c.Y;
    radius = c.radius;
    PI = c.PI;
    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::show();
    cout << ",Radius=" << radius << ")";
}
double Circle::area() const
{
    return PI * radius * radius;
}
double Circle::length() const
{
    return 2 * PI * radius;
}
class Sphere : public Solid, public Circle
{
protected:
    static double PI;

private:
    int X;
    int Y;
    double radius;

public:
    Sphere(double x = 0, double y = 0, double r = 0);
    Sphere(Sphere &);
    ~Sphere();
    double volume() const;
    double s_Area() const;
    void show() const;
};
double Sphere::PI = 3.14159;
Sphere::Sphere(double x, double y, double r) : Circle(x, y, r)
{
    X = Circle::Point::getX();
    Y = Circle::Point::getY();
    radius = Circle::getR();
    cout << "Sphere Constructor run" << endl;
}
Sphere::Sphere(Sphere &s) : Circle(s)
{
    X = s.X;
    Y = s.Y;
    radius = s.radius;
    PI = s.PI;
    cout << "Sphere CopyConstructor run" << endl;
}
Sphere::~Sphere()
{
    cout << "Sphere Destructor run" << endl;
}
double Sphere::volume() const
{
    return 4 * PI * radius * radius * radius / 3;
}
double Sphere::s_Area() const
{
    return 4 * PI * radius * radius;
}
void Sphere::show() const
{
    cout << "Sphere(Circle(Point(X=" << X << ",Y=" << Y << "),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;
}

void volumn(Solid *s)
{ 
    cout << "Volumn=" << s->volume() << endl;
}
void s_Area(Solid &s)
{ 
    cout << "S_Area=" << s.s_Area() << endl;
}
int main(void)
{
    double r;
    cin >> r;
    Sphere s1(1, 2, 3), s2(s1);
    show(&s1);
    cout << endl;
    area(s1);
    length(&s1);
    s_Area(s1);
    volumn(&s1);
    s2.setR(r);
    show(&s2);
    cout << endl;
    area(s2);
    length(&s2);
    s_Area(s2);
    volumn(&s2);
    return 0;
}

  • 写回答

1条回答 默认 最新

  • wlj1234 2022-04-04 23:33
    关注

    Sphere继承Circle,同时也会继承Circle成员x,y,radius,当调用Sphere::setR()时,实际上是将参数赋值给父类Circle中的变量radius而不是Sphere的变量radius
    正确的做法应该是移除Sphere中的三个成员,并将Circle的成员设置为public或protected

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

报告相同问题?

问题事件

  • 系统已结题 4月13日
  • 已采纳回答 4月5日
  • 修改了问题 4月4日
  • 修改了问题 4月4日
  • 展开全部

悬赏问题

  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R