C++代码注释 在线等

来源:百度知道 编辑:UC知道 时间:2024/09/23 12:01:20
保持main 函数不变,写Date类
Class Date{
………
};
int main(){
Date d1;
Date d2(3,12,1995);
Date d3(1995,3,5);
cout<<d1; //输出:1900年1月1日
int t=d2-d3;
cout<<t; //t=7 相差的天数
Date d4;
d4=d2+5; //日期可以和整数相加
cout<<d4; //输出:1995年3月17日
if(d2>d3){ //日期可以比较
cout<<"OK";
}
return 0;
}

#include<iostream>
#include<math.h>
using namespace std; // 使用命名空间
class Date{ //定义DATE类
private: //私有成员
int year;
int month;
int day;
public: //公共成员函数
Date(){
year=1900;month=1;day=1; //构造函数
}
Date(int a,int b,int c){ //一般成员函数
if(a>b&&a>c)
{year=a;month=b;day=c;}
else
{month=a;day=b;year=c;}
}
friend void operator <<(ostream& y,Date& x){ //友元声明重载运算符
y&

#include<iostream>
#include<math.h>
using namespace std; // 使用命名空间
class Date{ //定义DATE类
private: //私有成员
int year; //类私有变量年
int month;//类私有变量月
int day; //类私有变量日
public: //公共成员函数
Date(){
year=1900;month=1;day=1; //构造函数 ,初始化为1900年1月1日
}
Date(int a,int b,int c){ //重载构造函数
if(a>b&&a>c) //判断输入格式,年月日或者月日年,西方使用月日年
{year=a;month=b;day=c;}
else
{month=a;day=b;year=c;}
}
friend void operator <<(ostream& y,Date& x){ //友元声明重载运算符
y<<x.year<<"年"<<x.month<<"月"<<x.day<<"日"<<endl;
}
friend long operator -(Date& x,Date& y){ //重载-号操作符,求相差天数
return abs(x.year*365+x.month*30+x.day-y.year*365-y.month*30-y.day);
}
friend Date& operator +(Date& x,int y){ 重载+号操作符
x.day+=y;
if(x.day>30)
{x.month+=1;x.day-=30;}
if(x.month>12)
{x.year+=1;x.month-=