c++输入输出流运算符的重载测试代码

#pragma once
#include
using namespace std;
class Data
{
private:
	int year;
	int month;
	int day;
public:
	Data(int year=0,int month=0,int day=0) :year(year),month(month),day(day){}
	friend istream& operator>>(istream&,Data&);
	friend ostream& operator<<(ostream&, Data&);
};

 

#include "Data.h"
istream& operator>>(istream& in, Data& a) 
{
	in >> a.year >> a.month >> a.day;
	return in;
}
ostream& operator<<(ostream& out, Data& a) 
{
	out << a.year << '\t' << a.month << '\t' << a.day << endl;
	return out;
}
#include "Data.h"
int main()
{
	Data t[3];
	for (int i = 0; i < 3; i++)
		cin >> t[i];
	for (int i = 0; i < 3; i++)
		cout<< t[i];
	return 0;
}

 

你可能感兴趣的:(c++)