如何覆盖子 class 中的抽象 class 构造函数
How to override abstract class constructor in sub class
我刚刚从 java 转到 C++,我真的很难理解曾经的基本概念。我想要做的就是创建一个名为 'Spaceship' 的抽象 class,给它一个带有 4 个参数的构造函数,然后在一个名为 'Alienspaceship' 的子 class 中,我想创建一个带有 5 个参数但继承 'spaceship' 构造函数的前 4 个参数的构造函数。我一直在到处寻找,似乎无法找到我正在寻找的东西。下面是我的代码。请并感谢任何可以帮助我的人!
header
#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;
}
AlienSpaceship(float, float, float, float, OrganismType):Spaceship(float, float, float, float){}
应该是:
AlienSpaceship(float, float, float, float, OrganismType);
在你的 header 中。您实际上已经在 header...
中创建了第二个实现
您似乎误解了构造函数初始化列表的工作原理。
它用于构造函数的定义(即实现),构造函数的声明不需要它。
实际上,您当前在 AlienSpaceship
class 中的声明实际上 定义了 (实现)构造函数,因为结尾 {}
.
所以对于class,你需要做的就是
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 定义本身中内联完成:
class AlienSpaceship: public Spaceship{
...
public:
AlienSpaceship(float health, float food, float cash, float misc, OrganismType race)
: Spaceship(health, food, cash, misc), race(race)
{ /* Empty */ }
};
每个 class 成员应该只有一个定义,但是你有两个
第一个(隐含在 class 定义中)
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定义
要么在 class
声明它
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.
我刚刚从 java 转到 C++,我真的很难理解曾经的基本概念。我想要做的就是创建一个名为 'Spaceship' 的抽象 class,给它一个带有 4 个参数的构造函数,然后在一个名为 'Alienspaceship' 的子 class 中,我想创建一个带有 5 个参数但继承 'spaceship' 构造函数的前 4 个参数的构造函数。我一直在到处寻找,似乎无法找到我正在寻找的东西。下面是我的代码。请并感谢任何可以帮助我的人!
header
#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;
}
AlienSpaceship(float, float, float, float, OrganismType):Spaceship(float, float, float, float){}
应该是:
AlienSpaceship(float, float, float, float, OrganismType);
在你的 header 中。您实际上已经在 header...
中创建了第二个实现您似乎误解了构造函数初始化列表的工作原理。
它用于构造函数的定义(即实现),构造函数的声明不需要它。
实际上,您当前在 AlienSpaceship
class 中的声明实际上 定义了 (实现)构造函数,因为结尾 {}
.
所以对于class,你需要做的就是
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 定义本身中内联完成:
class AlienSpaceship: public Spaceship{
...
public:
AlienSpaceship(float health, float food, float cash, float misc, OrganismType race)
: Spaceship(health, food, cash, misc), race(race)
{ /* Empty */ }
};
每个 class 成员应该只有一个定义,但是你有两个
第一个(隐含在 class 定义中)
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定义
要么在 class
声明它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.