父类对象的数据发生改变后
子类的数据要怎样做 才可以跟着一起继承新的数据
这是举例
#include <iostream>
using namespace std;
class A
{
private:
int A_num;
public:
A(int n)
{
A_num = n;
}
void setnum(int n)
{
A_num = n;
}
int getnum()
{
return A_num;
}
};
class B : public A
{
private:
int B_num;
public:
B(int n) : A(n)
{
B_num = A::getnum();
}
void show()
{
cout << B_num << endl;
}
};
int main()
{
B b(1);
b.show();
b.setnum(2);
b.show();
}
可以看到在执行b.setnum(2)后 b中的B-num没有变成2
该怎样改才能输出2
运行结果:
代码修改如下:
#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); /*设置X坐标*/
void setY(double yv); /*设置Y坐标*/
double getX() const; /*获取X坐标*/
double getY() const; /*获取Y坐标*/
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坐标*/
x = xv;
}
void Point::setY(double yv)
{ /*设置Y坐标*/
y = yv;
}
double Point::getX() const
{ /*获取X坐标*/
return x;
}
double Point::getY() const
{ /*获取Y坐标*/
return y;
}
void Point::show() const
{ /*显示*/
cout << "Point(X=" << x << ",Y=" << y << ")";
}
//平面图形类Plane
class Plane
{
public:
virtual double length() const = 0; /*周长*/
virtual double area() const = 0; /*面积*/
};
//立体图形类Solid
class Solid
{
public:
virtual double volume() const = 0; /*体积*/
virtual double s_Area() const = 0; /*表面积*/
};
class Circle : public Plane,public Point
{
private:
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)
{
//X = Point::getX();
//Y = Point::getY();
setX(x);
setY(y);
radius=r;
cout << "Circle Constructor run" << endl;
}
Circle::Circle(const Circle &c)
{
setX(c.getX());
setY(c.getY());
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(";
cout <<"("<<getX()<<","<<getY()<<")";
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)
{
//X = Point::getX();
//Y = Point::getY();
//radius = getR();
setX(x);
setY(y);
setR(r);
cout << "Sphere Constructor run" << endl;
}
Sphere::Sphere(Sphere &s)
{
setX(s.getX());
setY(s.getY());
setR(s.getR());
PI = s.PI;
cout << "Sphere CopyConstructor run" << endl;
}
Sphere::~Sphere()
{
cout << "Sphere Destructor run" << endl;
}
double Sphere::volume() const
{
return 4 * PI * getR() * getR() * getR() / 3;
}
double Sphere::s_Area() const
{
return 4 * PI * getR() * getR();
}
void Sphere::show() const
{
cout << "Sphere(Circle(Point(X=" << getX() << ",Y=" << getX() << "),Radius=" << getR() << "))";
}
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;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!