#include <iostream>
#include <string>
using namespace std;
struct Date{
int month;
int day;
int year;
};
struct Student{
int num;
string name;
char sex;
Date brithday;
float score;
};
ostream& operator<< (ostream& o,const Date& d)
{
o<<"["<<d.year<<"-"<<d.month<<"-"<<d.day<<"]"<<endl;
return o;
}
ostream& operator<< (ostream& o, const Student& s)
{
o<<<<","<<s.num<<","<<s.sex<<","<<s.score<<","<<s.brithday<<endl;//此行决定了Date结构体的运算符重载应该在Student结构体的前面(Date运算符的重载应该声明在Student前面)
return o;
}
int main() {
std::cout << "Hello, World!" << std::endl;
Student student1={1,"suyunzzz",'f',12,19,1997,100};
Student student2=student1;
cout<<"student2:"<<"\n";
cout<<student2<<endl;
return 0;
}
输出:
Hello, World!
student2:
suyunzzz,1,f,100,[1997-12-19]