C++实验04_类继承
实验04(01)Time、 Date类及其子类
题目描述
已有类 Time和Date,要求设计一个派生类Birthtime,它是Time和Date的公有派生类,新增一个数据成员childName用于表示小孩的名字,设计主程序显示一个小孩的名字和出生日期时间。数据通过键盘输入,需要判断输入的年月日时分秒是否有效。Time与Date的成员见教材上习题4.21。
输入描述
Birthtime类对象的数据
输出描述
Birthtime类对象--小孩的姓名及出生日期时间
输入样例
赵无忌
2017 8 35
2017 8 25
25 45 68(顺序:时分秒)
23 45 23
输出样例
日期输入错误!请重新输入数据!(中文标点)
时间输入错误!请重新输入数据!
姓名:赵无忌
出生年月:2017年8月25日
出生时间:23时45分23秒
#include <iostream>
#include <string>
using namespace std;
class Time
{
public:
Time(int h = 0, int m = 0, int s = 0)
{
hours = h;
minutes = m;
seconds = s;
};
void display()
{
cout << "出生时间:" << hours << "时" << minutes << "分" << seconds << "秒" << endl;
}
protected:
int hours, minutes, seconds;
};
class Date
{
public:
Date(int m = 0, int d = 0, int y = 0)
{
year = m;
month = d;
day = y;
}
void display()
{
cout << "出生年月:" << year << "年" << month << "月" << day << "日" << endl;
}
protected:
int month, day, year;
};
class Birthtime : public Time, public Date
{
public:
Birthtime(string a = " ", int b = 0, int c = 0, int d = 0, int e = 0, int f = 0, int g = 0) : Time(e, f, g), Date(b, c, d)
{
childName = a;
}
void output()
{
cout << "姓名:" << childName << endl;
Date::display();
Time::display();
}
protected:
string childName;
};
int main()
{
int year, month, day, hours, minutes, seconds;
string childName;
cin >> childName;
here1:
cin >> year >> month >> day;
if (month < 0 || month > 12 || day < 0 || day > 31)
{
cout << "日期输入错误!请重新输入数据!" << endl;
goto here1;
}
if (month == 2 || month == 4 || month == 6 || month == 9 || month == 11)
{
if (day > 30)
{
cout << "日期输入错误!请重新输入数据!" << endl;
goto here1;
}
}
if (!(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)))
{
if (month == 2)
if (day > 28)
{
cout << "日期输入错误!请重新输入数据!" << endl;
goto here1;
}
}
here2:
cin >> hours >> minutes >> seconds;
if (hours < 0 || hours > 24 || minutes < 0 || minutes > 60 || seconds < 0 || seconds > 60)
{
cout << "时间输入错误!请重新输入数据!" << endl;
goto here2;
}
Birthtime A(childName, year, month, day, hours, minutes, seconds);
A.output();
return 0;
}
实验04(02)Shape类及其子类
题目描述
定义一个基类Shape,有成员函数:calArea(),但什么都不做,返回0。在此基础上公有派生出Rectangle(矩形)类和Circle类,添加相应的数据成员(成员均为double型),重新定义calArea()计算对象的面积。
主函数中,分别定义一个Rectangle和Circle类对象,初始值由键盘输入。(1)通过对象调用相应的calArea(),输出结果;(2)定义Shape类对象指针,分别赋以Rectangle和Circle类的对象地址,通过指针调用calArea(),输出结果;(3)定义Shape类对象引用,以Rectangle和Circle类的对象初始化,通过引用调用calArea(),输出结果。
PI: 3.1415926
输入描述
两行
Rectangle类对象的初值
Circle类对象的初值
输出描述
通过基类对象、基类指针和基类对象引用调用之后的结果
输入样例
3 6
3
输出样例
通过Rectangle类对象调用calArea():18
通过Circle类对象调用calArea():28.2743
通过基类指针调用Rectangle类的calArea():0
通过基类指针调用Circle类的calArea():0
通过基类对象引用调用Rectangle类的calArea():0
通过基类对象引用调用Circle类的calArea():0(中文冒号)
#include <iostream>
#define PI 3.1415926
using namespace std;
class Shape
{
public:
int calArea()
{
return 0;
}
};
class Rectangle : public Shape
{
public:
Rectangle(double a, double b)
{
h = a;
w = b;
}
double calArea()
{
return h * w;
}
void output()
{
cout << "通过Rectangle类对象调用calArea():" << calArea() << endl;
}
protected:
double h, w;
};
class Circle : public Shape
{
public:
Circle(double a)
{
r = a;
}
double calArea()
{
return r * r * PI;
}
void output()
{
cout << "通过Circle类对象调用calArea():" << calArea() << endl;
}
protected:
double r;
};
int main()
{
double h, w, r;
cin >> h >> w >> r;
Rectangle A(h, w);
Circle B(r);
Shape *p1, *p2;
Shape &q1 = A, &q2 = B;
p1 = &A;
p2 = &B;
A.output();
B.output();
cout << "通过基类指针调用Rectangle类的calArea():" << (*p1).calArea() << endl;
cout << "通过基类指针调用Circle类的calArea():" << (*p2).calArea() << endl;
cout << "通过基类对象引用调用Rectangle类的calArea():" << (q1).calArea() << endl;
cout << "通过基类对象引用调用Circle类的calArea():" << (q2).calArea() << endl;
return 0;
}
实验04(03)多继承
题目描述
在习题4-3的基础上,Person类添加日期类对象作数据成员,由Person类公有派生出Student类和Teacher类,由Student类和Teacher类公有派生子类TA(助教博士生)类,添加数据成员:专业(string类型),添加构造函数、输出函数(输出一个TA对象的全部信息)。注意虚基类的使用。类之间的关系如图4-1所示(电子文档中)。
主函数中定义一个TA类对象,其各数据成员的初始值由键盘输入,输出TA类对象的全部信息;修改TA对象的出生年月日,数据由键盘输入(按年月日顺序),再输出TA类对象的全部信息。
输入描述
一个TA类对象的全部信息
修改的出生年月日信息
输出描述
修改前TA类对象的全部信息
修改后TA类对象的全部信息
输入样例
1001 刘玄德 m 四川成都 123456789
1989 8 12 87 90 85 88 讲师 3500 计算机科技与技术
1989 9 12
输出样例
编 号:1001
姓 名:刘玄德
性 别:m
家庭住址:四川成都
联系电话:123456789
出生日期:1989年8月12日
数 学:87
物 理:90
英 语:85
程序设计:88
职 称:讲师
工 资:3500
专 业:计算机科技与技术
编 号:1001
姓 名:刘玄德
性 别:m
家庭住址:四川成都
联系电话:123456789
出生日期:1989年9月12日
数 学:87
物 理:90
英 语:85
程序设计:88
职 称:讲师
工 资:3500
专 业:计算机科技与技术
#include <iostream>
#include <string>
using namespace std;
class Date
{
public:
Date(int m = 0, int d = 0, int y = 0)
{
year = m;
month = d;
day = y;
}
void setDate(int a, int b, int c)
{
year = a;
month = b;
day = c;
}
void display()
{
cout << "出生日期:" << year << "年" << month << "月" << day << "日" << endl;
}
protected:
int month, day, year;
};
class Person
{
public:
Person(string a = " ", string b = " ", char c = ' ', string d = " ", string e = " ")
{
Num = a;
Name = b;
Sex = c;
Adr = d;
Tel = e;
}
void SetNum(string a)
{
Num = a;
}
void SetName(string a)
{
Name = a;
}
void SetNum(char a)
{
Sex = a;
}
void SetAdr(string a)
{
Adr = a;
}
void SetTel(string a)
{
Tel = a;
}
void output()
{
cout << "编 号:" << Num << endl;
cout << "姓 名:" << Name << endl;
cout << "性 别:" << Sex << endl;
cout << "家庭住址:" << Adr << endl;
cout << "联系电话:" << Tel << endl;
}
protected:
string Num;
string Name;
char Sex;
string Adr;
string Tel;
Date A;
};
class Student : virtual public Person
{
public:
Student(int a = 0, int b = 0, int c = 0, int d = 0)
{
mScore = a;
pScore = b;
eScore = c;
cScore = d;
}
void setScore(char tag, int score)
{
switch (tag)
{
case 'm':
mScore = score;
break;
case 'p':
pScore = score;
break;
case 'e':
eScore = score;
break;
case 'c':
cScore = score;
break;
}
}
void output1()
{
cout << "编 号:" << Num << endl;
cout << "姓 名:" << Name << endl;
cout << "性 别:" << Sex << endl;
cout << "家庭住址:" << Adr << endl;
cout << "联系电话:" << Tel << endl;
cout << "数 学:" << mScore << endl;
cout << "物 理:" << pScore << endl;
cout << "英 语:" << eScore << endl;
cout << "程序设计:" << cScore << endl;
}
protected:
int mScore;
int pScore;
int eScore;
int cScore;
};
class Teacher : virtual public Person
{
public:
Teacher(string a = " ", double b = 0)
{
ZhiCheng = a;
GongZi = b;
}
void SetZhiCheng(string a)
{
ZhiCheng = a;
}
void SetGongZi(double a)
{
GongZi = a;
}
void output2()
{
cout << "编 号:" << Num << endl;
cout << "姓 名:" << Name << endl;
cout << "性 别:" << Sex << endl;
cout << "家庭住址:" << Adr << endl;
cout << "联系电话:" << Tel << endl;
cout << "职 称:" << ZhiCheng << endl;
cout << "工 资:" << GongZi << endl;
}
protected:
string ZhiCheng;
double GongZi;
};
class TA : public Student, public Teacher
{
public:
TA(string a, string b, char c, string d, string e, string j = " ", int aa = 0, int bb = 0, int cc = 0, int dd = 0, string ee = " ", double ff = 0) : Student(aa, bb, cc, dd), Teacher(ee, ff)
{
Num = a;
Name = b;
Sex = c;
Adr = d;
Tel = e;
major = j;
}
void setDate(int a, int b, int c)
{
A.setDate(a, b, c);
}
void output3()
{
cout << "编 号:" << Num << endl;
cout << "姓 名:" << Name << endl;
cout << "性 别:" << Sex << endl;
cout << "家庭住址:" << Adr << endl;
cout << "联系电话:" << Tel << endl;
A.display();
cout << "数 学:" << mScore << endl;
cout << "物 理:" << pScore << endl;
cout << "英 语:" << eScore << endl;
cout << "程序设计:" << cScore << endl;
cout << "职 称:" << ZhiCheng << endl;
cout << "工 资:" << GongZi << endl;
cout << "专 业:" << major << endl;
}
protected:
string major;
};
int main()
{
string Num;
string Name;
char Sex;
string Adr;
string Tel;
int month, day, year;
int month1, day1, year1;
int mScore;
int pScore;
int eScore;
int cScore;
string ZhiCheng;
double GongZi;
string major;
cin >> Num >> Name >> Sex >> Adr >> Tel >> year >> month >> day >> mScore >> pScore >> eScore >> cScore >> ZhiCheng >> GongZi >> major;
cin >> year1 >> month1 >> day1;
TA A(Num, Name, Sex, Adr, Tel, major, mScore, pScore, eScore, cScore, ZhiCheng, GongZi);
A.setDate(year, month, day);
A.output3();
A.setDate(year1, month1, day1);
cout << "\n";
A.output3();
return 0;
}