继承中的构造函数
Constructor in inheritance
如何在Tanks类上声明构造函数,以便创建新对象,如
tanks t34(durability, velocity, damage);
这是我的课:
#include <iostream>
using namespace std;
class vehicles{
private:
double durability;
double velocity;
public:
void drive() { cout << "driven"; }
void info() { cout << durability << " " << velocity << "n"; }
vehicles(double d, double v) : durability(d), velocity(v) {}
~vehicles() {}
};
class tanks:public vehicles{
private:
double damage;
public:
using vehicles::vehicles;
tanks(double dmg) : damage(dmg) {}
void shot();
};
所以我想从复制变量
vehicles(double d, double v) : durability(d), velocity(v) {}
并将其添加到坦克类中。
只需在tanks
:中添加另一个构造函数
tanks(double dmg, double v, double d):vechicles(d,v), dmanage(dmg) {}
//^^call base class constructor to init base part
然后您应该能够创建tanks
的对象,如下所示:
tanks t34(durability, velocity, damage);
相关文章:
- 具有相同名称的类的构造函数继承
- 多复制构造函数继承中的惊人行为
- CRTP 和复制/移动赋值/构造函数继承
- 复制构造函数继承动态分配的数组
- 无法从 c++ 中的构造函数继承
- 在构造函数继承中使用默认构造函数
- 构造函数继承和直接成员初始化
- C++ - 使用私有参数的构造函数继承
- 通过可变参数模板进行C++11构造函数继承
- C++11构造函数继承和不带参数的构造函数
- C++11 - 构造函数继承
- C++构造函数继承
- 使用 C++11 编译器的构造函数继承正在生成错误
- 选择性构造函数继承"Ambiguous resolution"错误
- 什么是构造函数继承
- C++对象中的构造函数继承与方法继承
- C++构造函数继承没有匹配的函数
- C++构造函数继承(从派生类调用构造函数)
- 模板构造函数继承的标准符合语法是什么
- C++构造函数继承错误