实验4.1
已有类Time和Date,要求设计一个派生类Birthtime,它是Time和Date的公有派生类,新增一个数据成员childName用于表示小孩的名字,设计主程序显示一个小孩的名字和出生日期时间。数据通过键盘输入,需要判断输入的年月日时分秒是否有效。
输入样例:
赵无忌
2017 8 35
2017 8 25
25 45 68
23 45 23
输出样例:
日期输入错误!请重新输入数据!
时间输入错误!请重新输入数据!
姓名:赵无忌
出生年月:2017年8月25日
出生时间:23时45分23秒
赵无忌
2017 2 29
2017 2 28
25 63 23
15 23 53
日期输入错误!请重新输入数据!
时间输入错误!请重新输入数据!
姓名:赵无忌
出生年月:2017年2月28日
出生时间:15时23分53秒
赵飞燕
2013 45 62
2013 45 23
2013 12 23
6 5 23
日期输入错误!请重新输入数据!
日期输入错误!请重新输入数据!
姓名:赵飞燕
出生年月:2013年12月23日
出生时间:6时5分23秒
曹冲
2010 23 24
2010 12 31
25 56 63
25 35 36
15 36 35
日期输入错误!请重新输入数据!
时间输入错误!请重新输入数据!
时间输入错误!请重新输入数据!
姓名:曹冲
出生年月:2010年12月31日
出生时间:15时36分35秒
赵飞燕
2000 2 29
23 14 25
姓名:赵飞燕
出生年月:2000年2月29日
出生时间:23时14分25秒
#include<iostream>
using namespace std;
#include<string>
class Time
{
public:
Time(int h,int m,int s)
{
hours=h;
minutes=m;
seconds=s;
}
void display()
{
cout<<"出生时间:"<<hours<<"时"<<minutes<<"分"<<seconds<<"秒"<<endl;
}
protected:
int hours,minutes,seconds;
};
class Date
{
public:
Date(int m,int d,int y)
{
month=m;
day=d;
year=y;
}
void display()
{
cout<<"出生年月:"<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
}
protected:
int month,day,year;
};
class Birthtime:public Time,public Date
{
public:
Birthtime(int h,int m,int s,int mon,int d,int y,string childName):Time(h,m,s),Date(mon,d,y)
{
m_childName=childName;
}
void display()
{
cout<<"姓名:"<<m_childName<<endl;
Date::display();
Time::display();
}
private:
string m_childName;
};
bool ValidTime(int hour,int minute,int second)
{
if(hour<0||hour>=24||minute<0||minute>=60||second<0||second>=60)
{
return false;
}
return true;
}
bool ValidDate(int month,int day,int year)
{
if(year<0||month<1||month>12||day<1||day>31)
{
return false;
}
if(month==2)
{
if ((year%4==0&&year%100!=0)||year%400==0)
{
if(day>29)
{
return false;
}
}
else
{
if(day>28)
{
return false;
}
}
}
else if(month==4||month==6||month==9||month==11)
{
if(day>30)
{
return false;
}
}
return true;
}
int main()
{
int flag1=0,flag2=0;
string childName;
int h,m,s,mon,d,y;
cin>>childName;
cin>>y>>mon>>d;
while(!ValidDate(mon,d,y))
{
cin>>y>>mon>>d;
flag1++;
}
cin>>h>>m>>s;
while(!ValidTime(h,m,s))
{
cin>>h>>m>>s;
flag2++;
}
while(flag1--)
{
cout<<"日期输入错误!请重新输入数据!"<<endl;
}
while(flag2--)
{
cout<<"时间输入错误!请重新输入数据!"<<endl;
}
Birthtime person(h,m,s,mon,d,y,childName);
person.display();
return 0;
}
实验4.2
定义一个基类Shape,有成员函数:calArea(),但什么都不做,返回0。在此基础上公有派生出Rectangle(矩形)类和Circle类,添加相应的数据成员(成员均为double型),重新定义calArea()计算对象的面积。
主函数中,分别定义一个Rectangle和Circle类对象,初始值由键盘输入。(1)通过对象调用相应的calArea(),输出结果;(2)定义Shape类对象指针,分别赋以Rectangle和Circle类的对象地址,通过指针调用calArea(),输出结果;(3)定义Shape类对象引用r,以Rectangle和Circle类的对象初始化r,通过引用r调用calArea(),输出结果。
PI取值:3.1415926
输入样例:
3 6
3
输出样例:
通过Rectangle类对象调用calArea():18
通过Circle类对象调用calArea():28.2743
基类指针指向Rectangle类对象,调用calArea():0
基类指针指向Circle类对象,调用calArea():0
Rectangle类对象初始化基类对象引用r,r调用calArea():0
Circle类对象初始化基类对象引用r,r调用calArea():0
2 5
2
通过Rectangle类对象调用calArea():10
通过Circle类对象调用calArea():12.5664
基类指针指向Rectangle类对象,调用calArea():0
基类指针指向Circle类对象,调用calArea():0
Rectangle类对象初始化基类对象引用r,r调用calArea():0
Circle类对象初始化基类对象引用r,r调用calArea():0
1 5
4
通过Rectangle类对象调用calArea():5
通过Circle类对象调用calArea():50.2655
基类指针指向Rectangle类对象,调用calArea():0
基类指针指向Circle类对象,调用calArea():0
Rectangle类对象初始化基类对象引用r,r调用calArea():0
Circle类对象初始化基类对象引用r,r调用calArea():0
#include<iostream>
using namespace std;
#define PI 3.1415926
class Shape
{
public:
double calArea()
{
return 0;
}
};
class Rectangle:public Shape
{
public:
Rectangle(double length,double width):m_length(length),m_width(width){}
double calArea()
{
return m_length*m_width;
}
private:
double m_length,m_width;
};
class Circle:public Shape
{
public:
Circle(double r):m_r(r){}
double calArea()
{
return m_r*m_r*PI;
}
private:
double m_r;
};
int main()
{
double length,width,R;
cin>>length>>width;
cin>>R;
Rectangle rectangle(length,width);
cout<<"通过Rectangle类对象调用calArea():"<<rectangle.calArea()<<endl;
Circle circle(R);
cout<<"通过Circle类对象调用calArea():"<<circle.calArea()<<endl;
Shape* p=&rectangle;
cout<<"基类指针指向Rectangle类对象,调用calArea():"<<p->calArea()<<endl;
p=&circle;
cout<<"基类指针指向Circle类对象,调用calArea():"<<p->calArea()<<endl;
Shape &r=rectangle;
cout<<"Rectangle类对象初始化基类对象引用r,r调用calArea():"<<r.calArea()<<endl;
r=circle;
cout<<"Circle类对象初始化基类对象引用r,r调用calArea():"<<r.calArea()<<endl;
return 0;
}
实验4.3
Person类添加日期类对象作数据成员,由Person类公有派生出Student类和Teacher类,由Student类和Teacher类公有派生子类TA(助教博士生)类,添加数据成员:专业(string类型),添加构造函数、setDate()(设置出生年月日)、输出函数(输出一个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
专 业:计算机科技与技术
1101 魏建华 m 河南郑州 132145678 1990 9 10 85 88 92 86
讲师 4500 软件工程
1990 1 10
编 号:1101
姓 名:魏建华
性 别:m
家庭住址:河南郑州
联系电话:132145678
出生日期:1990年9月10日
数 学:85
物 理:88
英 语:92
程序设计:86
职 称:讲师
工 资:4500
专 业:软件工程
编 号:1101
姓 名:魏建华
性 别:m
家庭住址:河南郑州
联系电话:132145678
出生日期:1990年1月10日
数 学:85
物 理:88
英 语:92
程序设计:86
职 称:讲师
工 资:4500
专 业:软件工程
5021 梅长苏 m 河北石家庄 1329806785 1995 8 25 82 78 90 84
助教 3600 电子信息工程
1996 8 25
编 号:5021
姓 名:梅长苏
性 别:m
家庭住址:河北石家庄
联系电话:1329806785
出生日期:1995年8月25日
数 学:82
物 理:78
英 语:90
程序设计:84
职 称:助教
工 资:3600
专 业:电子信息工程
编 号:5021
姓 名:梅长苏
性 别:m
家庭住址:河北石家庄
联系电话:1329806785
出生日期:1996年8月25日
数 学:82
物 理:78
英 语:90
程序设计:84
职 称:助教
工 资:3600
专 业:电子信息工程
#include<iostream>
using namespace std;
#include<string>
class Date
{
public:
Date(int year,int month,int day):m_year(year),m_month(month),m_day(day){}
void print()
{
cout<<"出生日期:"<<m_year<<"年"<<m_month<<"月"<<m_day<<"日"<<endl;
}
int m_year;
int m_month;
int m_day;
};
class Person
{
public:
Person(string name,string number,string sex,string address,string phonenumber,int year,int month,int day):m_name(name),m_number(number),
m_sex(sex),m_address(address),m_phonenumber(phonenumber),m_date(year,month,day){}
void print()
{
cout<<"编 号:"<<m_number<<endl;
cout<<"姓 名:"<<m_name<<endl;
cout<<"性 别:"<<m_sex<<endl;
cout<<"家庭住址:"<<m_address<<endl;
cout<<"联系电话:"<<m_phonenumber<<endl;
m_date.print();
}
protected:
string m_name;
string m_number;
string m_sex;
string m_address;
string m_phonenumber;
Date m_date;
};
class Student:virtual public Person
{
public:
Student(int math,int physics,int English,int course,string name,string number,string sex,string address,string phonenumber,int year,int month,int day): Person(name,number,sex,address,phonenumber,year,month,day),m_math(math),m_physics(physics),m_English(English),m_course(course){}
void print()
{
cout<<"数 学:"<<m_math<<endl;
cout<<"物 理:"<<m_physics<<endl;
cout<<"英 语:"<<m_English<<endl;
cout<<"程序设计:"<<m_course<<endl;
}
private:
int m_math;
int m_physics;
int m_English;
int m_course;
};
class Teacher:virtual public Person
{
public:
Teacher(string title,double wages,string name,string number,string sex,string address,string phonenumber,int year,int month,int day): Person(name,number,sex,address,phonenumber,year,month,day),m_title(title),m_wages(wages){}
void print()
{
cout<<"职 称:"<<m_title<<endl;
cout<<"工 资:"<<m_wages<<endl;
}
private:
string m_title;
double m_wages;
};
class TA:public Student,public Teacher
{
public:
TA(string number,string name,string sex,string address,string phonenumber,int year,int month,int day,
int math,int physics,int English,int course,string title,double wages,string major):Person(name,number,sex,address,phonenumber,year,month,day),
Student(math,physics,English,course,name,number,sex,address,phonenumber,year,month,day),Teacher(title,wages,name,number,sex,address,phonenumber,year,month,day),m_major(major){}
void setDate(int year,int month,int day)
{
m_date.m_year=year;
m_date.m_month=month;
m_date.m_day=day;
}
void print()
{
Person::print();
Student::print();
Teacher::print();
cout<<"专 业:"<<m_major<<endl;
}
private:
string m_major;
};
int main()
{
double wages;
int year1,month1,day1,year2,month2,day2;
int math,physics,English,course;
string number,name,sex,address,phonenumber,title,major;
cin>>number>>name>>sex>>address>>phonenumber>>year1>>month1>>day1>>math>>physics>>English>>course>>title>>wages>>major;
cin>>year2>>month2>>day2;
TA p(number,name,sex,address,phonenumber,year1,month1,day1,math,physics,English,course,title,wages,major);
p.print();
p.setDate(year2,month2,day2);
p.print();
return 0;
}