求教

yd2011222 yd2011222 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include<iostream>
using namespace std;
class Date{
Date& Date::operator ++(); //前置++

Date Date::operator ++(int); //后置++

Date& Date::operator --(); //前置--

Date Date::operator --(int); //后置--

int year, month, day;
static int days[];
void increment();
void decrement();


public:

Date(int y = 2008, int m = 8, int d = 1){

year = y; month = m; day = d;

}

void set(int y, int m, int d){

year = y; month = m; day = d;

}

void print(){

cout << year << '-' << month << '-' << day << '\n';

}

};
int Date::days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
void Date::increment(){
if (month == 2 && day < 29 && year % 4 == 0 && year % 100 != 0)
++day;
else
if (day < Date::days[month])
++day;
else

if (month < 12){
++month;
day = 1;
}
else
{
++year;
month = 1;
day = 1;
}

}
void Date::decrement(){
if (day > 1)
--day;
else if (month == 3 && year % 4 == 0 && (year % 400 == 0) || (year % 100 != 0)){
day = 29;
--month;
}
else
if (month > 1)
day = days[--month];
else{

--year;
month = 12;
day = 31;
}

}

Date& Date:: operator ++(){
return Date(year,month, ++day);

}
Date Date:: operator ++( int){
return Date(year, month, day++);
}
Date &Date:: operator --(){
return Date(year, month, --day);
}
Date Date::operator --( int){

return Date(year, month, day--);

}
int main(){

Date B,A(2006,3,1);
A.print();
B.print();
B = ++A;
A.print();
B.print();
B = A++;
A.print();
B.print();
}
编译结果:
主函数的错误B = ++A; B = A++;
求教
...全文

相关推荐

评论 3
收藏 已收藏
分享
返回顶部
yd2011222
C++ 语言 65,213 成员 250,513 内容