以下是源代码。
为什么输出的sphere的球心是(0,0,3)而不是(1,2,3)?
#include <iostream>
using namespace std;
class Point2D
{
protected:
double x;
double y;
public:
Point2D() { x = 0; y = 0; }
Point2D(double a, double b)
{
x = a;
y = b;
}
virtual void show()
{
cout << "The 2D point is:(" << x << ',' << y << ')'<<endl;
}
};
class Circle:virtual public Point2D
{
protected:
double radius;
public:
Circle() { radius = 0; }
Circle(double a, double b, double c) :Point2D(a, b)
{
radius = c;
}
virtual void show()
{
cout << "The center of circle is:(" << x << ',' << y << ')' << endl;
cout << "The radius of circle is:" << radius << endl;
}
};
class Point3D :virtual public Point2D
{
protected:
double z;
public:
Point3D(double a, double b, double c) :Point2D(a, b)
{
z = c;
}
virtual void show()
{
cout << "The 3D point is:(" << x << ',' << y << ',' << z << ')' << endl;
}
};
class Sphere :public Circle, public Point3D
{
public:
Sphere(double a, double b, double c, double r) :Point3D(a, b, c)
{
radius = r;
}
virtual void show()
{
cout << "The center of sphere is:(" << x << ',' << y << ','<<z<<')' << endl;
cout << "The radius of sphere is:" << radius << endl;
}
};
int main()
{
Point2D p1(1, 1);
Circle c1(1, 2, 2);
Point3D p2(1.5, 2.5, 3.5);
Sphere s1(1, 2, 3, 4);
//p1 = c1;
Point2D* pointer = &p1;
pointer->show();
pointer = &c1;
pointer->show();
pointer=&p2;
pointer->show();
pointer = &s1;
pointer->show();
system("pause");
return 0;
}