编译器:C++ (g++)
派生类定义:根据所给的基类,完成多重继承下的派生类定义
函数接口定义:
#include
#include
using namespace std;
//定义公共基类Person
class Person
{public:
Person(string nam,char s,int a)
{name=nam;sex=s;age=a;}
protected:
string name;
char sex;
int age;
};
//定义类Teacher
class Teacher:virtual public Person
{public:
Teacher(string nam,char s,int a,string t):Person(nam,s,a)
{title=t;
}
protected:
string title;
};
//定义类Student
class Student:virtual public Person
{public:
Student(string nam,char s,int a,float sco):
Person(nam,s,a),score(sco){}
protected:
float score;
};
/*这里添加派生类的定义*/
裁判测试程序样例:
int main( )
{Graduate grad1("Wang-li",'f',24,"assistant",89.5,1234.5);
grad1.show( );
return 0;
}
输出样例:
在这里给出相应的输出。例如:
name:Wang-li
age:24
sex:f
score:89.5
title:assistant
wages:1234.5
#include
#include
#include
using namespace std;
//在此处补充Date类的定义
class Date {
public:
Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}
int getYear() const { return year_; }
int getMonth() const { return month_; }
int getDay() const { return day_; }
string toText() const {
string str = to_string(year_) + "-" + to_string(month_) + "-" + to_string(day_);
return str;
}
Date operator+(int n) const {
int daysInMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int days = day_ + n;
int month = month_;
int year = year_;
while (days > daysInMonth[month - 1]) {
if (month == 2 && isLeapYear()) {
if (days > 29) {
days -= 29;
month++;
} else {
break;
}
} else {
days -= daysInMonth[month - 1];
month++;
}
if (month > 12) {
year++;
month = 1;
}
}
return Date(year, month, days);
}
Date operator-(int n) const {
int days = day_ - n;
int month = month_;
int year = year_;
while (days <= 0) {
if (month == 3 && isLeapYear()) {
days += 29;
month--;
} else {
month--;
if (month == 0) {
year--;
month = 12;
}
days += daysInMonth(month - 1);
}
}
return Date(year, month, days);
}
private:
int year_, month_, day_;
bool isLeapYear() const {
if (year_ % 4 == 0 && year_ % 100 != 0 || year_ % 400 == 0) {
return true;
} else {
return false;
}
}
int daysInMonth(int month) const {
if (month == 1 && isLeapYear()) {
return 29;
} else {
return daysInMonth_[month];
}
}
static const int daysInMonth_[12];
};
const int Date::daysInMonth_[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
int y, m, d;
cin >> y >> m >> d;
Date d1(y,m,d);
int n;
cin >> n;
cout << d1.toText() << " + " << n << " = " << (d1 + n).toText() << endl;
cout << d1.toText() << " - " << n << " = " << (d1 - n).toText() << endl;
return 0;
}