将子对象复制到父对象

Copy child object to parent object

本文关键字:对象 复制      更新时间:2023-10-16

我有三个类:ShapeRectangleCircleShape 是另外两个类的父类。此类的定义在以下代码中:

#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()函数。