如何编写已继承的大纲构造函数?
How to write outline constructor that has been inheritance?
我正在尝试用c ++编写OUTLINE继承构造函数。 我不确定这是否可能。 我只编写了内联继承构造函数。
如果有办法写,你能分享一下吗?
谢谢。
这是我的代码。
18 public:
19 FullTimeEmployee(int eid, string name, int salary):Employee(eid, name);
20 int getSalary(){ return salary; }
21 };
22
23 FullTimeEmployee::FullTimeEmployee(int eid, string name, int salary):Employee(eid, name){
24 this->salary = salary;
25 }
在第19 行,您不必再声明添加派生:Employee(eid, name)
。
FullTimeEmployee(int eid, string name, int salary);
所以如果它是一个完整的程序,代码可能会喜欢这样
#include <iostream>
using namespace std;
class Employee{
public:
Employee(int eid, string name){}
};
class FullTimeEmployee:Employee{
private:
int salary;
int eid = 10;
string name = "hei";
public:
FullTimeEmployee(int eid, string name, int salary);
int getSalary(){ return salary; }
};
FullTimeEmployee::FullTimeEmployee(int eid, string name, int salary):Employee(eid, name){
this->salary = salary;
};
int main()
{
FullTimeEmployee f(10, "yuza", 200);
cout<<f.getSalary();
return 0;
}
你可以构造一个派生类的构造函数
,如下所示:#include <iostream>
class base { // the base class
int x, y;
public:
base() {} // default constructor
base(int a, int b) : x(a), y(b) {};
};
class derived : public base {
int m, n;
public:
derived(int, int); // derived class's constructor with its signature
void print(void) {
std::cout << m << ' ' << n << std::endl;
}
};
derived :: derived(int Dx, int Dy) { // coding the constructor of derived class
// outside of the class.
m = Dx;
n = Dy;
}
int main(void) {
derived d(5, 3); // instantiating the object of the derived class
d.print(); // will print 5 <space> 3
return 0;
}
相关文章:
- 如何编写带有异常的构造函数
- 如何编写已继承的大纲构造函数?
- 如何将我编写的对象传递到另一个类的构造函数中?
- 有没有办法避免为 std::variant 类成员中的所有类型编写构造函数?
- 如何编写复制构造函数来复制指针?
- 如何为精灵编写复制构造函数
- 有没有办法初始化不涉及编写构造函数的新结构变量?
- 如何为已删除的默认构造函数编写测试
- 复制构造函数,深层复制以及如何正确编写
- 正确编写复制构造函数和分配运算符的方法
- 如何编写移动构造函数以处理非初始化的移动
- C++ 为什么不能编写这个构造函数?
- 正确编写基于范围的构造函数
- 你能为具有常量成员的联合编写一个复制构造函数吗?
- 是否可以编写便于副本初始化的显式构造函数
- 如何编写适当的std :: initializer_list构造函数
- 我可以避免在std ::变体中明确编写每个结构的构造函数
- 双重列表复制构造函数:与单一列表复制构造函数有何不同
- 为什么用户编写的构造函数会影响生成的程序集
- 如何编写两个初始化操作(一个作为默认初始化,另一个作为用户输入)?两者都是类的构造函数C++