Enclave_ 2022-04-04 21:00 采纳率: 85.2%

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

``````Point Constructor run
Circle Constructor run
Sphere Constructor run
Point CopyConstructor run
Circle CopyConstructor run
Sphere CopyConstructor run
Area=28.2743
Length=18.8495
S_Area=113.097
Volumn=113.097
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
``````

``````#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;

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();
cout << "Circle Constructor run" << endl;
}
Circle::Circle(const Circle &c) : Point(c)
{
X = c.X;
Y = c.Y;
PI = c.PI;
cout << "Circle CopyConstructor run" << endl;
}
Circle::~Circle()
{
cout << "Circle Destructor run" << endl;
}
void Circle::setR(double r)
{
}
double Circle::getR() const
{
}
void Circle::show() const
{
cout << "Circle(";
Point::show();
}
double Circle::area() const
{
}
double Circle::length() const
{
return 2 * PI * radius;
}
class Sphere : public Solid, public Circle
{
protected:
static double PI;

private:
int X;
int Y;

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();
cout << "Sphere Constructor run" << endl;
}
Sphere::Sphere(Sphere &s) : Circle(s)
{
X = s.X;
Y = s.Y;
PI = s.PI;
cout << "Sphere CopyConstructor run" << endl;
}
Sphere::~Sphere()
{
cout << "Sphere Destructor run" << endl;
}
double Sphere::volume() const
{
}
double Sphere::s_Area() const
{
}
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的成员设置为public或protected

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

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

#### 悬赏问题

• ¥15 CCF-CSP 2023 第三题 解压缩(50％)
• ¥30 comfyui openpose报错
• ¥20 Wpf Datarid单元格闪烁效果的实现
• ¥15 图像分割、图像边缘提取
• ¥15 sqlserver执行存储过程报错
• ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
• ¥15 浮窗和全屏应用同时存在，全屏应用输入法无法弹出
• ¥100 matlab2009 32位一直初始化
• ¥15 Expected type 'str | PathLike[str]…… bytes' instead
• ¥15 三极管电路求解，已知电阻电压和三级关放大倍数