C++实验代码三

实验内容:
设计一个时间类Time,要求:
(1)包含时(hour)、分(minute)和秒(second)私有数据成员。
(2)包含构造函数,重载关于一时间加上另一时间的加法运算符+、重载关于一时间减去另一时间的减加运算符-、重载输出运算符<<与输入运算符>>等。
在这里插入图片描述

#include <iostream.h>
//友元重载函数在VC++6.0中用.h的头文件
class Time
{	
public:
	Time(int h=0,int m=0,int s=0)
	{
		hour=h;
		minute=m;
		second=s;
	}
	Time operator+(Time b)
	{
		Time temp;
		temp.hour=hour+b.hour;
		temp.minute=minute+b.minute;
		temp.second=second+b.second;
		if(temp.minute>=60)
		{
			temp.hour++;
			temp.minute-=60;
		}
		if(temp.second>=60)
		{
			temp.minute++;
			temp.second-=60;
		}
		return temp;
	}
	Time operator-(Time b)
	{
		Time temp;
		temp.hour=hour-b.hour;
		temp.minute=minute-b.minute;
		temp.second=second-b.second;
		if (temp.second<0)
		{
			temp.minute--;
			temp.second+=60; 
		}
		if (temp.minute<0)
		{
			temp.hour--;
			temp.minute+=60;
		}
		if(temp.hour<0)
		{
			temp.hour+=24;
		}
		return temp;
	}
	friend ostream &operator<<(ostream &stream,Time &ob);
	friend istream &operator>>(istream &stream,Time &ob);
private:
	int hour;
	int minute;
	int second;
};
ostream &operator<<(ostream &stream,Time &ob)
{
	cout<<ob.hour<<":"<<ob.minute<<":"<<ob.second<<endl;
	return stream;
}
istream &operator>>(istream &input,Time &ob)
{
	cout<<"请输入时间:";
	input>>ob.hour;
	input>>ob.minute;
	input>>ob.second;
	return input;
}
int main()
{
	Time time1,time2,time3,time4;
	cin>>time1;
	cin>>time2;
	cout<<endl<<"输入的时间为:"<<endl;
	cout<<time1;
	cout<<time2;
	time3=time1+ time2;
	time4=time1- time2;
	cout<<"时间相加结果为:"; 
	cout<<time3;
	cout<<"时间相减结果为:";
	cout<<time4;
	return 0;
}

1.声明一个动物基Animal,私有整型成员变量年龄age,请定义一个派生Dog,在其成员函数SetAge(int n)中直接给age赋值,测试下看是否会出问题?如何解决? 2.设计一个单基继承的层次程序,用Person派生出Student,增加属性学号index和年级level。Person中至少有姓名name、年龄age等数据成员,以及构造函数、输出函数等,其余成员函数根据需要添加。在主函数中进行测试。 3.定义一个学生Student和教师Teacher,学生有姓名name、学号index等数据成员,教师有姓名name、工作证号workID、职称title、课程course、周学hoursPerWeek等数据成员。再定义一个助教TeachingAssistant,多继承于学生和教师,该可以使用学生的全部数据成员,以及教师的课程和周学数据成员。要求:每个提供自定义的构造函数和析构函数,并通过同名函数ShowInfo来显示全部数据成员的值。在主函数中进行测试。 4.声明一个Person,包含姓名name和年龄age等私有数据成员以及相关的成员函数;由它派生出领导Leader,包含职务position和部门department私有数据成员以及相关的成员函数;再由Person派生出工程师Engineer,包含职务position和专业speciality私有数据成员以及相关的成员函数;再由Leader和Engineer派生出主任工程师Chairman。在主函数中测试各对象初始化和信息输出,查看是否会出问题?如何解决?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫荆鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值