C++ 重载输出符号

1 C++ 重载输出符号.cpp

#include<iostream>
using namespace std;

 struct Date{
    int year,month,day;
    Date()=default;
    Date(int y,int m, int d)
        :year(y),month(m),day(d){}
    ~Date(){}
    Date operator++(int i)
    {
        Date tmp;
        tmp.year = year;
        tmp.month = month;
        tmp.day = day;
        switch(month)
        {
        case 1:case 3:case 5:case 7:
        case 8:case 10:
            if(day == 31){
                day =1;
                month++;
            }else
                day++;
            break;
        case 4:case 6:case 9:case 11:
            if(day == 30){
                day=1;
                month++;
            }else
                day++;
            break;
        case 12:
            if(day == 31){
                day =1;
                month=1;
                year++;
            }else
                day++;
            break;
        case 2:
            if(year %4==0 && year%100 !=0 || year %400 ==0){
                if(day == 29){
                    day =1;
                    month++;
                }else
                    day++;
            }else{
                if(day == 28){
                    day =1;
                    month++;
                }else
                    day++;

            }
            break;
        default:
            break;
        }
        return *this;
    }
friend ostream & operator<<(ostream &cout, Date d);
 };
ostream & operator<<(ostream &cout, Date d)
{
    cout<<d.year<<"-"<<d.month<<"-"<<d.day;
    return cout;
}


/*
ostream cout;
cout<<d1<<d2<<d3;
ostream{
    operator<<(int);
    operator<<(string);
    operator<<(Date){...}
};
*/
int main()
{
    Date d1(2015,8,31);
    d1++;
    cout<<d1<<endl;
}
原文地址:https://www.cnblogs.com/Sico2Sico/p/5384248.html