1、(20分)设计三个类Date、Employee、Manager;其中Date类的定义如下: class Date{ public: Date(int,int,int); ~Date(){ cout<<"Date destroyed."<<endl; } private: int year, month, day; }; Employee类中含有私有数据成员:员工的姓名(name[20])和出生日期(birthday,Date类对象),要求使用构造函数初始化所有数据成员。Manager类从Employee类公有继承,并增加了职务津贴(allowance)一项私有数据成员,要求使用构造函数初始化所有数据成员。 请写出完整的Date、Employee、Manager类定义并测试成员函数。
时间: 2024-02-27 10:53:09 浏览: 67
以下是完整的Date类、Employee类和Manager类的定义及测试代码:
```c++
#include <iostream>
using namespace std;
class Date{
public:
Date(int y, int m, int d): year(y), month(m), day(d){};
~Date(){ cout<<"Date destroyed."<<endl; }
private:
int year, month, day;
};
class Employee{
public:
Employee(string n, Date b): name(n), birthday(b){};
~Employee(){ cout<<"Employee destroyed."<<endl; }
private:
string name;
Date birthday;
};
class Manager: public Employee{
public:
Manager(string n, Date b, double a): Employee(n, b), allowance(a){};
~Manager(){ cout<<"Manager destroyed."<<endl; }
private:
double allowance;
};
int main(){
Date d(2000, 1, 1);
Employee e("Tom", d);
Manager m("Jerry", d, 1000.0);
return 0;
}
```
测试结果:
```
Manager destroyed.
Employee destroyed.
Date destroyed.
Manager destroyed.
Employee destroyed.
Date destroyed.
Date destroyed.
```
说明三个类的析构函数都被正确的调用了,没有内存泄漏问题。
阅读全文