设计一个日期类Date,,要求: (1)包含年(year)、月(month)和日(day)私有数据成员。 (2)包含构造函数,重载关于一日期加上天数的加法运算符+、重载关于一日期减去天数的减加运算符-

#include<iostream.h>
class Date{
private:
	double year,month,day;
public:
	Date(double y=0,double m=0,double d=0):year(y),month(m),day(d){};
	Date operator+(Date b);                         //重载运算符+
	Date operator-(Date b);                         //重载运算符-
	friend ostream &operator<<(ostream &os,Date &s);//重载流运算符<<
	friend istream &operator>>(istream &is,Date &s);//重载流运算符>>
	void Show();
};
Date Date::operator+(Date b){
    if((day+b.day)>31){
	   month++;
	   int dd=day+b.day-31;
	return Date(year,month,dd);
	   }
	else
	return Date(year,month,day+b.day);
	
}
Date Date::operator-(Date b){
	if(day-b.day<=0){
		month--;
		int dd=day-b.day+30;
	return Date(year,month,dd);
	}
		else
	return Date(year,month,day-b.day);
}
void Date::Show(){
	cout<<year<<"-"<<month<<"-"<<day<<endl;
}
ostream& operator<<(ostream &os,Date &s){
	os<<s.year<<"\t";
	os<<s.month<<"\t";
	os<<s.day<<"\t"<<endl;
	return os;
}
istream &operator>>(istream &is,Date &s){
	cout<<"按顺序输入年 月 日"<<endl;
	is>>s.year;
	is>>s.month;
	is>>s.day;
	cout<<endl;
	return is;
}
void main(){
	Date t1(2013,10,11),t2(0,0,25),t3,t4;
	t1.Show();
	t3=t1+t2;
	t4=t1-t2;
	t3.Show();
	t4.Show();
	cin>>t1;
	cout<<t1;
}

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值