将子对象复制到父对象
Copy child object to parent object
我有三个类:Shape
、Rectangle
和Circle
。 Shape
是另外两个类的父类。此类的定义在以下代码中:
#include <iostream>
using namespace std;
class Shape {
public:
Shape() {
}
~Shape() {
}
void set(float BORDER, string COLOR) {
border = BORDER;
color = COLOR;
}
double computeArea() {
return 0;
}
private:
float border;
string color;
};
class Circle : public Shape {
public:
Circle() {
}
~Circle() {
}
void setRadius(float RADIUS) {
radius = RADIUS;
}
double computeArea() {
return 3.14 * radius * radius;
}
private:
float radius;
};
class Rectangle : public Shape {
public:
Rectangle() {
}
~Rectangle() {
}
void setWidth(float w) {
width = w;
}
void setLength(float l) {
length = l;
}
double computeArea() {
return width * length;
}
private:
float width;
float length;
};
我从Circle
类和Rectangle
类构建两个对象。然后我将这两个对象复制到Shape
类中。当我按照以下顺序运行computeArea()
函数时0
我得到结果。
#include <iostream>
using namespace std;
int main() {
Circle c;
c.setRadius(3);
Rectangle r;
r.setWidth(4);
r.setLength(5);
Shape sh[2];
sh[0] = c;
sh[1] = r;
cout << sh[0].computeArea() << endl;
cout << sh[1].computeArea();
return 0;
}
我想要具有正确功能的所有形状的计算区域。我该怎么做?
提前致谢
要扩展 Fureeish 所说的内容,请将您的代码更改为以下内容:
int main() {
Circle c;
c.setRadius(3);
Rectangle r;
r.setWidth(4);
r.setLength(5);
Shape *sh[2];
sh[0] = &c;
sh[1] = &r;
cout << sh[0]->computeArea() << endl;
cout << sh[1]->computeArea();
return 0;
}
并将computeArea
(以及Shape
的析构函数,以防通过指向基类的指针销毁派生对象)声明为 virtual
。
将派生类分配给基类的对象称为"对象切片",通常会导致不希望的结果。 使用指针(或引用)可以避免这种情况。
在 Shape 类中,使 computeArea() 成为抽象(或纯虚拟)方法:
virtual double computeArea() =0;
您似乎缺少virtual
关键字。
基类 Shape 的析构函数也需要是虚拟的(以防您决定通过指向基类的指针操作任何派生对象)。
函数 computeArea() 必须声明为 virtual,如下所示 virtual double computeArea() { ... }
。
#include <iostream>
using namespace std;
class Shape {
public:
Shape() {
}
virtual ~Shape() {
}
void set(float BORDER, string COLOR) {
border = BORDER;
color = COLOR;
}
virtual double computeArea() {
return 0;
}
private:
float border;
string color;
};
class Circle : public Shape {
public:
Circle() {
}
virtual ~Circle() {
}
void setRadius(float RADIUS) {
radius = RADIUS;
}
virtual double computeArea() {
return 3.14 * radius * radius;
}
private:
float radius;
};
class Rectangle : public Shape {
public:
Rectangle() {
}
virtual ~Rectangle() {
}
void setWidth(float w) {
width = w;
}
void setLength(float l) {
length = l;
}
virtual double computeArea() {
return width * length;
}
private:
float width;
float length;
};
此外,如果您使用的是 C++11 或更高版本,则可以在computeArea
函数定义的末尾添加 overrride
关键字,如下所示
virtual double computeArea() override {
return width * length;
}
而这个,
virtual double computeArea() override {
return 3.14 * radius * radius;
}
在各自的类中。
然后像这样修改主函数。
int main() {
Circle c;
c.setRadius(3);
Rectangle r;
r.setWidth(4);
r.setLength(5);
Shape *sh1, *sh2;
sh1 = &c;
sh2 = &r;
cout << sh1->computeArea() << endl;
cout << sh2->computeArea();
return 0;
}
编辑:此外,正如其他人指出的那样,您需要指向基类的指针,并且不需要将派生类对象复制到基类对象。请参阅修改后的main()函数。
相关文章:
- 为什么C++在将一个对象复制到另一个对象时需要对这两个对象进行低级常量限定
- 如何在向量列表初始化时避免对象复制以及如何延长临时的生存期
- 了解在 C++ 中分配容器时的对象复制
- C++类对象复制构造函数和运算符=
- 复制 elision/RVO 会导致从同一对象复制/移动吗?
- 如何在循环内将大对象复制到omp任务中?
- 为什么当我添加一个不同的对象(复制构造函数中的参数)时调用复制构造函数?
- 无法将类构造函数中新创建的对象复制到 C++ 中的向量成员
- 将C++对象复制到指向新指针的指针处
- 将一个 OpenCV OutputArrayOfArrays 对象复制到另一个对象
- 选择性地禁用隐式对象复制
- 不能从文件到指针向量的对象复制
- 从派生对象复制构造
- OpenCV Mat对象复制速度更快
- 将子对象复制到父对象
- 为什么普通的 c++ 编译器不优化对象复制?
- C :将一个对象复制到构造函数中的另一个对象
- 使用命名对象复制构造函数
- 不必要的对象复制 - C++ STL
- 如何将一个类的对象复制到另一个类中