实验八 多态:运算符的重载
1 实验目的
学习面向对象程序设计中“多态机制”的一种实现方法——运算符重载,包括:
(1)学习如何定义运算符的重载;
(2)学习如何使用重载的运算符。
2 实验内容
2.1 改进《实验三 面向对象初步》中的日期类
在《实验三 面向对象初步》中,设计了日期类Date。本次实验将daysTo函数重载为减号运算符,使得t1 - t2的值为从日期t2到日期t1的天数。如果t1在t2之前,则为负数,即计算t1比t2晚多少天。
重载大于运算符,如果t1在t2之后,则表达式t1 > t2为真;
重载小于运算符,如果t1在t2之前,则表达式t1 < t2为真。
2.2 使用改进后的日期类,修改《实验四 对象作为数据成员》中的雇员类(Employee)
(1)修改getDaysWorked,使其调用日期类中重载的减号运算符。
(2)添加一个静态成员函数,调用日期类中重载的“>”运算符,通过比较雇佣日期,在雇员对象数组中,找出工作年限最长的雇员。该函数的说明如下:
/*
*参数employees[]是雇员对象的数组;n是雇员对象数组的元素个数
*返回值:工作年限最长的雇员对象的引用
*/
static const Employee& getMostFaith(const Employee employees[], int n);
2.3 测试getMostFaith静态成员函数
编写一个主函数,生成含有5个雇员对象的数组,然后调用Employee类的getMostFaith静态成员函数,找出工作年限最长的雇员,打印其信息以及工作了多少天。
3 代码
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <cstring>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int goans(int a, int b, int c, int x, int y, int z) {
int daysa = 0, daysb = 0;
for (int i = 0; i < a; i++) {
if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) { daysa += 366; }
else { daysa += 365; }
}
for (int i = 0; i < x; i++) {
if ((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0)) { daysb += 366; }
else { daysb += 365; }
}
if ((a % 4 == 0 && a % 100 != 0) || (a % 400 == 0)) { days[2] = 29; } else { days[2] = 28; }
for (int i = 1; i < b; i++) {
daysa += days[i];
}
if ((x % 4 == 0 && x % 100 != 0) || (x % 400 == 0)) {
days[2] = 29;
} else {
days[2] = 28;
}
for (
int i = 1;
i < y;
i++) {
daysb += days[i];
}
daysa += c, daysb += z;
return daysa - daysb;
}
class Date {
public:
Date(int year = 1990, int month = 1, int day = 1) {
this->year = year;
this->month = month;
this->day = day;
this->separator = '-';
}/* get、set方法 */ // 设置日期,如果有非法的月或日,将其置为1
void setDate(int year, int month, int day) {
if (year >= 0) { this->year = year; } else { this->year = 1; }
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { days[2] = 29; } else { days[2] = 28; }
if (month > 12 || month < 1) {
this->month = 1;
this->day = 1;
} else if (day < 1 || day > days[month]) { this->day = 1; } else { this->day = day; }
}
void setYear(int year) { if (year >= 0) { this->year = year; } else { this->year = 1; }}
int getYear() { return this->year; }
void setMonth(int month) { if (month > 0 && month <= 12) { this->month = month; } else { this->month = 1; }}
int getMonth() { return this->month; }
void setDay(int month) {
int year = getYear();
int m = getMonth();
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { days[2] = 29; } else { days[2] = 28; }
if (day < 1 || day > days[m]) { this->day = 1; } else { this->day = day; }
}
int getDay() {
return this->day;
}
void
setSeparator(
char separator) { this->separator = separator; }/* 输出函数,请使用setfill(‘0’)和setw(2),需要包含<iomanip>头文件 */ void
printFullYear() {
cout << setw(4) << this->year << this->separator << setw(2) << setfill('0') << this->month << this->separator
<< setw(2) << setfill('0') << this->day;
cout << endl;
}
void printStandardYear() {
int yy = this->year % 100;
cout << setw(4) << setfill(' ') << yy << this->separator << setw(2) << setfill('0') << this->month
<< this->separator << setw(2) << setfill('0') << this->day;
cout << endl;
} // 以YY-MM-DD的形式打印,比如11-01-08 /* 计算函数 */ // 计算当前日期与参数日期之间相差几个整年,仅考虑参数日期比当前日期晚的情况
int fullYearsTo(int year, int month, int day) {
int ans = year - this->year;
if (this->month > month) { ans--; } else if (this->month == month) { if (this->day > day) { ans--; }}
return ans;
}/* 计算当前日期与参数日期之间相差多少天(考虑闰年),如果参数日期在当前日期之前,返回负数。 */ int operator-(const Date &da) {
return -goans(year, month, day, da.year, da.month, da.day);
}
bool operator>(const Date &da) {
bool ans;
if (year > da.year) { ans = true; }
else if (year < da.year) { ans = false; }
else {
if (month > da.month) {
ans = true;
} else if (month < da.month) {
ans = false;
} else {
if (day > da.day) {
ans = true;
} else {
ans = false;
}
}
}
return
ans;
}
bool operator<(const Date &da) {
bool ans;
if (year < da.year) { ans = true; }
else if (year > da.year) { ans = false; }
else {
if (month < da.month) { ans = true; }
else if (month > da.month) { ans = false; }
else {
if (day < da.day) { ans = true; } else { ans = false; }
}
}
return ans;
}
private:
int year;
int month;
int day;
char separator; // 日期分隔符
};
int main() {
map<string, Date> mp;
string a, b;
int ya, ma, da, yb, mb, db;
int i = 2;
while (i--) {
cin >> a >> ya >> ma >> da >> b >> yb >> mb >> db;
mp[a] = Date(ya, ma, da);
mp[b] = Date(yb, mb, db);
cout << mp[a] - mp[b] << endl;
}
}
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <cstring>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;
class Date {
friend int operator-(Date& da, Date& date);
public:
Date(int year = 1990, int month = 1, int day = 1) {
this->
year = year;
this->
month = month;
this->
day = day;
this->
separator = '-';
}/* get、set方法 */ // 设置日期,如果有非法的月或日,将其置为1
void setDate(int year, int month, int day) {
if (year >= 0) { this->year = year; }
else { this->year = 1; }
if (isLeapyear(year)) { days[2] = 29; }
else { days[2] = 28; }
if (month > 12 || month < 1) {
this->month = 1;
this->day = 1;
}
else if (day < 1 || day > days[month]) { this->day = 1; }
else { this->day = day; }
}
void setYear(int year) { if (year >= 0) { this->year = year; } else { this->year = 1; } }
int getYear() { return this->year; }
void setMonth(int month) {
if (month > 0 && month <= 12) {
this->
month = month;
}
else {
this->
month = 1;
}
}
int getMonth() { return this->month; }
void setDay(int day) { this->day = checkDay(day); }
int getDay() { return this->day; }
void
setSeparator(
char separator) {
this->separator = separator;
}/* 输出函数,请使用setfill(‘0’)和setw(2),需要包含<iomanip>头文件 */ void
printFullYear() {
cout << setw(4) << this->year << this->separator << setw(2) << setfill('0') << this->month << this->separator
<< setw(2) << setfill('0') << this->day;
}
void printStandardYear() {
int yy = this->year % 100;
cout << setw(4) << setfill(' ') << yy << this->separator << setw(2) << setfill('0') << this->month
<< this->separator << setw(2) << setfill('0') << this->day;
cout << endl;
} // 以YY-MM-DD的形式打印,比如11-01-08 /* 计算函数 */ // 计算当前日期与参数日期之间相差几个整年,仅考虑参数日期比当前日期晚的情况
int fullYearsTo(Date& date) {
int ans = date.getYear() - this->year;
if (this->month > date.getMonth()) { ans--; }
else if (this->month == date.getMonth()) {
if (this->day > date.getDay()) { ans--; }
}
return ans;
}/* 计算当前日期与参数日期之间相差多少天(考虑闰年),如果参数日期在当前日期之前,返回负数。 */
bool operator<(const Date& da) const {
bool ans;
if (year < da.year) { ans = true; }
else if (year > da.year) { ans = false; }
else {
if (month < da.month) { ans = true; }
else if (month > da.month) { ans = false; }
else {
if (day < da.day) { ans = true; }
else { ans = false; }
}
}
return ans;
}
bool operator>(const Date& da) const {
bool ans;
if (year > da.year) { return true; }
else if (year < da.year) { return false; }
else {
if (month > da.month) { return true; }
else if (month < da.month) { return false; }
else {
if (day > da.day) { return true; }
else { return false; }
}
}
return ans;
}
int getDayOfYear() {
int ans = 0;
if (isLeapyear(year)) { days[2] = 29; }
else { days[2] = 28; }
for (int i = 1; i < month; i++) { ans += days[i]; }
return ans + day;
} //计算当前日期是本年的第几天
int getLeftDaysYear() {
if (isLeapyear(year)) { return 366 - getDayOfYear(); }
else {
return 365 - getDayOfYear();
}
} //计算当前日期距本年结束还有几天,不包括当前日期这一天
bool isLeapyear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { return true; }
else { return false; }
}
private:
int year;
int month;
int day;
char separator; // 日期分隔符 /*声明静态常变量,每月的天数,在.cpp文件中定义并初始化 */
static int days[13]; /*根据年和月,判断参数日期是否合法。如果合法,返回day,否则返回1。*/ int checkDay(int day) {
if (isLeapyear(year)) { days[2] = 29; }
else { days[2] = 28; }
if (day < 1 || day > days[month]) { return 1; }
else { return day; }
}
};
int Date::days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
class Employee {
public:
Employee(string firstName, string lastName, Date& birthDate, Date& hireDate);
static const Employee& getMostFaith(const Employee employees[], int n) {
int mi = 0;
for (int i = 1; i < n; i++) { if (employees[mi].hireDate > employees[i].hireDate) { mi = i; } }
return employees[mi];
}//构造函数,使用“成员初始化器”初始化数据成员 Employee(string, string, Date&, Date&); //打印员工的信息。调用Date类的print函数,打印员工的生日和雇佣日期。
void print() {
cout << lastName << ", " << firstName << " Hired:";
hireDate.printFullYear();
cout << " Birthday: ";
birthDate.printFullYear();
cout << endl;
}//计算员工在参数指定的日期时,满多少岁。请使用Date类的fullYearsTo函数
int getAge(Date& date) { return birthDate.fullYearsTo(date); }
int getYearsWorked(Date& date) { return hireDate.fullYearsTo(date); }
int getDaysWorked(Date& date) { return date - hireDate; }
~Employee() {} //析构函数
private:
string lastName;
string firstName;
Date birthDate; //内嵌对象,出生日期
Date hireDate; //内嵌对象,雇用日期
};
Employee::Employee(string firstName, string lastName, Date& birthDate, Date& hireDate) {
this->firstName = firstName;
this->lastName = lastName;
this->birthDate = birthDate;
this->hireDate = hireDate;
}
int operator-(Date& da, Date& date) {
int ans = 0;
if (da.getYear() < date.getYear()) {
for (int i = da.getYear() + 1; i < date.getYear(); i++) {
if (da.isLeapyear(i)) { ans += 366; }
else { ans += 365; }
}
ans = ans + da.getLeftDaysYear() + date.getDayOfYear();
}
else if (da.getYear() > date.getYear()) {
for (int i = date.getYear() + 1; i < da.getYear(); i++) {
if (da.isLeapyear(i)) { ans -= 366; }
else { ans -= 365; }
}
ans = ans - date.getLeftDaysYear() - da.getDayOfYear();
}
else {
if (da.isLeapyear(da.getYear())) { ans = 366 - da.getDayOfYear() - date.getLeftDaysYear(); }
else { ans = 365 - da.getDayOfYear() - date.getLeftDaysYear(); }
}
return -
ans;
}
int main() {
Date birth(1969, 8, 11);
Date hire1(1998, 4, 1), hire2(1999, 8, 15), hire3(2010, 4, 30), hire4(2010, 4, 1), hire5(2021, 12, 31);
Date today(2021, 5, 19);
Employee man[5] = { Employee("e1", "e1", birth, hire1), Employee("e2", "e2", birth, hire2),
Employee("e3", "e3", birth, hire3), Employee("e4", "e4", birth, hire4),
Employee("e5", "e 5", birth, hire5) };
for (int i = 0; i <= 4; i++) { man[i].print(); }
cout << endl;
Employee manager = Employee::getMostFaith(man, 5);
manager.print();
cout << "年龄:" << manager.getAge(today) << endl;
cout << "工作天数 :" << manager.getDaysWorked(today) << endl;
return 0;
}