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