第二题
2.1题目描述
利用继承性与派生类来管理学生教师档案:由Person(人员)类出发(作为基类),派生出Student(学生)及Teacher(教师)类;而后又由Student(学生)类出发(作为基类),派生出GraduateStudent(研究生)类。可假定这几个类各自具有的数据成员为:
Person(人员)类: 姓名、性别、年龄;
Student(学生)类: 姓名、性别、年龄、学号、系别;
Teacher(教师)类: 姓名、性别、年龄、职称、担任课程;
GraduateStudent(研究生)类: 姓名、性别、年龄、学号、系别、导师。
为简化起见,每个类可只设立构造函数以及显示类对象数据的成员函数Print。而后编制简单的主函数,说明上述有关的类对象,并对其类成员函数进行简单使用(调用)
2.2源代码
#include<iostream>
#include<string.h>
using namespace std;
class person
{
public:
char name[100];
char sex;
int age;
person(char *n,int a,char s)
{
strcpy(name,n);
sex=s;
age=a;
}
void print()
{
cout<<name<<" "<<age<<" "<<sex<<" ";
}
};
class student:public person
{
public:
int reg_number;
char department[100];
student(char *n,int a,char s,int r,char *d):person(n,a,s)
{
reg_number=r;
strcpy(department,d);
}
void print()
{
person::print();
cout<<reg_number<<" "<<department<<" ";
}
};
class teacher:public person
{
public:
char course[100];
char post[100];
teacher(char *n,int a,char s,char *c,char *p):person(n,a,s)
{
strcpy(course,c);
strcpy(post,p);
}
void print()
{
person::print();
cout<<course<<" "<<post;
}
};
class graduatestudent:public student
{
public:
char advisor[];
graduatestudent(char *n,int a,char s,int r,char *d,char *ad):student(n,a,s,r,d)
{
strcpy(advisor,ad);
}
void print()
{
student::print();
cout<<advisor;
}
};
int main()
{
char a1[100],a3,b1[100],b3,b5[100],c1[100],c3,c4[100],c5[100],d1[100],d3,d5[100],d6[100];
int a2,b2,b4,c2,d2,d4;
cin>>a1>>a2>>a3>>b1>>b2>>b3>>b4>>b5>>c1>>c2>>c3>>c4>>c5>>d1>>d2>>d3>>d4>>d5>>d6;
person x1(a1,a2,a3);
student x2(b1,b2,b3,b4,b5);
teacher x3(c1,c2,c3,c4,c5);
graduatestudent x4(d1,d2,d3,d4,d5,d6);
cout<<"== per1.Display() => name,age,sex"<<endl;
x1.person::print();
cout<<endl<<"== stu1.Display() => name,age,sex,Reg_Number,department"<<endl;
x2.student::print();
cout<<endl<<"== teach1.Display() => name,age,sex,course,post"<<endl;
x3.teacher::print();
cout<<endl<<"== gStu.Display() => name,age,sex,Reg_Number,department,advisor"<<endl;
x4.graduatestudent::print();
return 0;
}
2.3样例输出
第三题
3.1题目描述
自定义一个日期时间类DateTimeType,它含有类DateType与类TimeType的类对象作为其数据成员,并具有所列的其他几个成员函数。而后编制主函数,说明DateTimeType的类对象,并对其成员函数以及二对象成员所属类的公有成员函数进行使用。
class DateTimeType { //自定义的日期时间类 DateTimeType
DateType date; //类 DateType 的类对象 date 作为其数据成员
TimeType time; //类 TimeType 的类对象 time 作为其另一个数据成员
public:
DateTimeType(int y0=1, int m0=1, int d0=1, int hr0=0, int mi0=0, int se0=0);
//构造函数,设定 DateTimeType 类对象的日期时间,并为各参数设置了默认值
DateType& GetDate(){ return date; } //返回本类的私有数据对象 data
TimeType& GetTime(){ return time; } //返回本类的私有数据对象 time
void IncrementSecond(int s); //增加若干秒,注意“进位”问题
void PrintDateTime(); //屏幕输出日期时间对象的有关数据
};
注意,每一个DateTimeType类对象中总包含有一个DateType类对象(对象成员)以及一个TimeType类对象(对象成员),编制与实现本程序时,也必须包含DateType与TimeType自定义类(类型)。
之所以设置了公有的类成员函数GetDate与GetTime,是为类外如主函数处使用该类的私有数据成员date与time提供方便(否则的话,类外无法直接访问该类的私有数据成员)。另外,两成员函数返回的都为引用,为的是可将返回对象当作一个独立变量来使用(如可以继续作左值等)。例如,假设编制了如下形式的主函数:
void main() {
DateTimeType dttm1(1999,12,31,23,59,59), dttm2;
(dttm1.GetDate()).PrintDate(); //调用对象成员所属类的公有成员函数
cout<<endl;
dttm1.PrintDateTime(); //调用本派生类的成员函数 PrintDateTime
dttm2.PrintDateTime();
dttm1.IncrementSecond(30) ; //调用本派生类成员函数
dttm1.PrintDateTime();
}
3.2源代码
#include<iostream>
using namespace std;
class DateType
{
public:
int y;
int m;
int d;
DateType(int yy,int mm,int dd=0)
{
y=yy;
m=mm;
d=dd;
}
void printdate()
{
cout<<y<<"-"<<m<<"-"<<d;
}
};
class TimeType
{
public:
int h;
int mi;
int s;
TimeType(int hh,int mmi,int ss)
{
h=hh;
mi=mmi;
s=ss;
}
void printtime()
{
cout<<h<<":"<<mi<<":"<<s;
}
};
class DateTimeType
{
private:
DateType date;
TimeType time;
public:
DateTimeType(int y0=1, int m0=1, int d0=1, int hr0=0, int mi0=0, int se0=0):date(y0,m0,d0),time(hr0,mi0,se0)
{
date.y =y0;
date.m =m0;
date.d =d0;
time.h =hr0;
time.mi =mi0;
time.s =se0;
}
DateType& GetDate(){ return date; }
TimeType& GetTime(){ return time; }
void IncrementSecond(int add)
{
time.s+=add;
while(time.s>=60)
{
time.s-=60;
time.mi++;
}
while(time.mi>=60)
{
time.mi-=60;
time.h++;
}
while(time.h>=24)
{
time.h-=24;
date.d++;
}
int s[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if((date.y%4==0&&date.y%100!=0)||(date.y%400==0))
{
s[1] = 29;
}
for(int i=0;i<12;i++)
{
if(date.m-1==i)
{
if(date.d>=s[i]+1)
{
date.d -= s[i];
date.m +=1;
}
}
}
if (date.m>=13)
{
date.m-= 12;
date.y+=1;
}
}
void PrintDateTime()
{
date.printdate();
cout<<" ";
time.printtime();
cout<<endl;
}
};
int main()
{
DateTimeType dttm1(1999,12,31,23,59,59), dttm2;
(dttm1.GetDate()).printdate();
cout<<endl;
dttm1.PrintDateTime();
dttm2.PrintDateTime();
dttm1.IncrementSecond(30) ;
dttm1.PrintDateTime();
}
3.3样例输出
如果再也不能见到你,祝你早安,午安,晚安。 ------《楚门的世界》