WKYNEKO

博客园 首页 新随笔 联系 订阅 管理

/*
1311: C++2第十一章上机题4
时间限制:1.000s 内存限制:128MB

题目描述
1)建立一个教师类Teacher,其包含3个私有数据成员:num(工号)、name(姓名)、sex(性别),包含2个公有成员函数:带参构造函数(用于对三个数据成员进行初始化),display函数(输出数据成员)。

2)建立一个生日类BirthDate,其包含3个私有数据成员:year(年)、month(月)、day(日),包含3个公有成员函数:带参构造函数(用于对三个数据成员进行初始化),display函数(输出数据成员),change函数(修改生日数据,即将3个数据成员的值改为change函数参数指定的值)。

3)建立一个教授类Professor,它是Teacher类的公有派生类,在Teacher类的基础上增加了私有数据生日信息birthday(birthday是BirthDate类的对象),其新增成员函数包含:带参构造函数(对新增的和继承的数据成员初始化),display函数(输出所有数据成员),changebirth函数(将修改生日信息修改为函数参数指定日期)。

4)main函数中的操作步骤如下:

l 定义Professor类的对象prof1————Professor prof1(1001, "Wangli", 'f', 1973, 5, 10);

l 输出对象prof1的所有数据信息

l 修改prof1的生日信息

l 输出对象prof1的所有数据信息

输入格式

输出格式
num:1001
name:Wangli
sex:f
birthday:1973 / 5 / 10


num:1001
name:Wangli
sex:f
birthday:1975 / 10 / 12

样例输出
num:1001
name:Wangli
sex:f
birthday:1973 / 5 / 10


num:1001
name:Wangli
sex:f
birthday:1975 / 10 / 12
*/


#include<iostream>
using namespace std;

class Teacher
{
private:
int num;
string name;
char sex;

public:
Teacher(int n, string na, char s) : num(n), name(na), sex(s) {}
void display() {
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl;
}
};

class BirthDate
{
private:
int year;
int month;
int day;

public:
BirthDate(int y, int m, int d) : year(y), month(m), day(d) {}
void display() {
cout << "birthday:" << year << "/" << month << "/" << day << endl;
}
void change(int y, int m, int d) {
year = y;
month = m;
day = d;
}
};

class Professor : public Teacher {
private:
BirthDate birthday;

public:
Professor(int n, string na, char s, int y, int m, int d)
: Teacher(n, na, s), birthday(y, m, d) {}
void display() {
Teacher::display();
birthday.display();
}
void changebirth(int y, int m, int d) {
birthday.change(y, m, d);
}
};

int main()
{
Professor prof1(1001, "Wangli", 'f', 1973, 5, 10);
prof1.display();
cout << endl << endl;

prof1.changebirth(1975, 10, 12);
prof1.display();

return 0;
}

 

 

 

 

 

 

 

 

 

/*
1302: C++2第十一章上机题5 - 第三次上机
时间限制:1.000s 内存限制:128MB

题目描述
1.编写一个程序实现紫金学院的工资管理。我校主要有2类人员:教师(Teacher)、行政人员(Staff)。这些人员都是职员(Employee),具有3个基本信息:编号id、姓名name、月基本工资basic_salary。月工资的计算方法是:行政人员月工资 = 基本工资basic_salary + 奖金bonus,教师月工资 = 基本工资basic_salary + 课时hour * 10。假设员工的基本工资均为4000元,行政人员hanmei当月奖金bonus为6000元,教师lilei当月课时为100小时,要求编程计算此2个职员的当月工资并输出相应信息,运行效果如下所示:

提示:

(1)基类Employee包含3个数据成员:id、name、basic_salary,构造函数

(2)Employee派生出Teacher类(增加数据成员hour、构造函数、月工资计算函数、信息输出函数)和Staff类(增加数据成员bonus、构造函数、月工资计算函数、信息输出函数)。

(3)在main函数中定义行政人员对象(其id为101,name为hanmei)和教师对象(其id为102,name为lilei),计算并输出当月工资等信息。

输入格式

输出格式
员工的信息

Staff : 101 hanmei with salary 10000

Teacher : 102 lilei with salary 5000

样例输入

样例输出
Staff : 101 hanmei with salary 10000
Teacher : 102 lilei with salary 5000
*/

 


#include <iostream>
#include <string>
using namespace std;

class Employee {
protected:
int id;
std::string name;
double basic_salary;

public:
Employee(int id, const std::string& name, double basic_salary) : id(id), name(name), basic_salary(basic_salary) {}
};

class Teacher : public Employee {
private:
int hour;

public:
Teacher(int id, const std::string& name, double basic_salary, int hour) : Employee(id, name, basic_salary), hour(hour) {}

double calculate_salary() const {
return basic_salary + hour * 10;
}

void display_info() const {
std::cout << "Teacher: " << id << " " << name << " with salary " << calculate_salary() << std::endl;
}
};

class Staff : public Employee {
private:
double bonus;

public:
Staff(int id, const std::string& name, double basic_salary, double bonus) : Employee(id, name, basic_salary), bonus(bonus) {}

double calculate_salary() const {
return basic_salary + bonus;
}

void display_info() const {
std::cout << "Staff: " << id << " " << name << " with salary " << calculate_salary() << std::endl;
}
};

int main() {
Staff hanmei(101, "hanmei", 4000, 6000);
Teacher lilei(102, "lilei", 4000, 100);
hanmei.display_info();
lilei.display_info();

return 0;
}

 

posted on 2023-05-14 23:20  BKYNEKO  阅读(82)  评论(0编辑  收藏  举报