父类Employee 属性:name、sex , 带一个构造方法Employee(String n, char s)
子类 Worker继承自Employee 属性:char category;//类别 boolean 、属性:dressAllowance; //是否提供服装津贴 , 有一个构造方法 负责构造所有属性,还有一个自定义方法 isDressAll() 这个方法 负责通过判断dressAllowance的值输出 ,是否提供服装津贴。新建一个类测试类InheDemo 在main方法中新建一个Worker对象,输出这个对象的所有属性 并调用isDressAll()方法得到津贴信息
#include<iostream>
#include<string.h>
using namespace std;
class Employee {
public:
string name;
char sex;
Employee(string n, char s) {
name = n;
sex = s;
}
};
class Worker : public Employee {
public :
char category;
bool dressAllowance;
Worker(string n, char s,char c,bool d) :Employee( n, s) {
category = c;
dressAllowance = d;
}
void isDressAll() {
if (dressAllowance)
{
cout << "提供服装津贴" << endl;
}
else {
cout << "不提供服装津贴" << endl;
}
}
};
int main() {
Worker w1("zhangsan", '男', '1', true);
w1.isDressAll();
return 0;
}
效果: