#include <iostream>
using namespace std;
class Date
{
friend ostream& operator<<(ostream& os, Date& d);
friend istream& operator>>(istream& is, Date& d);
public:
Date(int year = 1900, int month = 1, int day = 1)
:_year(year)
,_month(month)
,_day(day)
{}
private:
int _year;
int _month;
int _day;
};
ostream& operator<<(ostream& os, const Date& d)
{
os<<d._year<<"-"<<d._month<<"-"<<d._day;
return os;
}
istream& operator>>(istream& is, Date& d)
{
cout<<"请依次输入年月日:";
is>>d._year>>d._month>>d._day;
return is;
}
int main()
{
Date d1;
cin>>d1;
cout<<d1<<endl;
return 0;
}
【C++】输入输出运算符的重载
最新推荐文章于 2020-01-18 16:13:32 发布