如何在子类中重写抽象类构造函数

时间:2019-04-11 09:46:42

标签: c++ inheritance constructor abstract

我刚刚从Java移到了c ++,我真的在为曾经的基本概念而苦苦挣扎。我要做的就是创建一个名为“ Spaceship”的抽象类,为它提供一个带有4个参数的构造函数,然后在一个名为“ Alienspaceship”的子类中,我要使一个带有5个参数但继承“ Spaceship”的构造函数'构造函数的前4个参数。我一直在到处寻找,似乎找不到确切的位置。下面是我的代码。请并感谢任何可以帮助我的人!

标题

#ifndef SPACESHIP
#define SPACESHIP

#include "Enum.h"

class Spaceship{
    public:
    float health;
    float food;
    float cash;
    float misc;

public:
    Spaceship(float, float, float, float);

};

class AlienSpaceship: public Spaceship{
    OrganismType race;

    public:
        AlienSpaceship(float, float, float, float, OrganismType):Spaceship(float, float, float, float){}
};

#endif

cpp

#include "Spaceship.h"

Spaceship::Spaceship(float health, float food, float cash, float misc){
    this->health = health;
    this->food = food;
    this->cash = cash;
    this->misc = misc;
}

AlienSpaceship::AlienSpaceship(float health, float food, float cash, float misc, OrganismType race):Spaceship(health, food, cash, misc){
    this->race = race;
}

3 个答案:

答案 0 :(得分:2)

您似乎误解了构造函数初始化器列表的工作原理。

它在构造函数的定义(即实现)中使用,声明构造函数不需要。

实际上,由于结尾AlienSpaceship{}类中的当前声明实际上定义了(实现)构造函数。

因此,对于这堂课,您要做的就是

class AlienSpaceship: public Spaceship{
    ...
    public:
        AlienSpaceship(float, float, float, float, OrganismType);
};

在相关说明中,我建议您也使用初始化程序列表来初始化成员变量,这样构造函数就可以了

Spaceship::Spaceship(float health, float food, float cash, float misc)
    : health(health), food(food), cash(cash), misc(misc)
{ /* Empty */ }

AlienSpaceship::AlienSpaceship(float health, float food, float cash, float misc, OrganismType race)
    : Spaceship(health, food, cash, misc), race(race)
{ /* Empty */ }

当然,这可以在类定义本身中内联完成:

class AlienSpaceship: public Spaceship{
    ...
    public:
        AlienSpaceship(float health, float food, float cash, float misc, OrganismType race)
            : Spaceship(health, food, cash, misc), race(race)
        { /* Empty */ }
};

答案 1 :(得分:0)

AlienSpaceship(float, float, float, float, OrganismType):Spaceship(float, float, float, float){}

应该是:

AlienSpaceship(float, float, float, float, OrganismType);

在标题中。您实际上已经在标头中创建了第二个实现...

答案 2 :(得分:0)

每个类成员应该只有一个定义,但是有两个

第一个(隐含在类定义中)

public:
AlienSpaceship(float, float, float, float, OrganismType):Spaceship(float, float, float, float)
{}

第二个在源文件中

AlienSpaceship::AlienSpaceship(float health, float food, float cash, float misc, OrganismType race):Spaceship(health, food, cash, misc)
{
    this->race = race;
}

选择其中一项,或者放

AlienSpaceship(float health, float food, float cash, float misc, OrganismType race):Spaceship(health, food, cash, misc)
{
    this->race = race;
}

进入类定义

可以在课堂上声明

class AlienSpaceship: public Spaceship{
OrganismType race;

public:
AlienSpaceship(float, float, float, float, OrganismType);

并在源文件中定义

AlienSpaceship::AlienSpaceship(float health, float food, float cash, float misc, OrganismType race):Spaceship(health, food, cash, misc)
{
    this->race = race;
}

此外,这样的声明

AlienSpaceship(float, float, float, float, OrganismType):Spaceship(float, float, float, float)

is incorrect too. Actually, it conatains a part of defenition.