我有两节课;一个名为Date
,另一个名为University
。 Date
类有两个重载运算符:operator<<
和operator>>
来接收数据并打印出来。
Date.h
#ifndef DATE_H_
#define DATE_H_
#include <iostream>
#include "University.h"
using namespace std;
class Date {
public:
Date(); // constructor
void setDate( int d, int m, int y ); // set day, month, year
friend ostream & operator<<(ostream & out, Date & x); // print date format "month dd, yyyy (example: January 11, 2013)
friend istream & operator>>(istream & In, Date & x); // to read date
private:
int day;
int month;
int year; //
};
#endif
Date.CPP
#include <iostream>
#include "Date.h"
#include "University.h"
using namespace std;
Date::Date()
{}
void Date::setDate( int d, int m, int y )
{
day=d;
month=m;
year=y;
}
ostream & operator<<(ostream & out, Date & x)
{
out<< x.month << "/" << x.day << "/" << x.year ;
return out;
}
istream & operator>>(istream & in, Date & x)
{
in>> x.day >> x.month >> x.year ;
return in;
}
University
类有一个名为Date
的{{1}}类型的对象,我必须使用它来打印 date 以及大学姓名和位置。
这是班级establishDate
:
University
如何使用对象// University.h
class University {
public:
University (); // constructor
friend ostream & operator<<(ostream & out, University & x); // print the university data
friend istream & operator>>(istream & in, University & x); // to read university data
private:
const static string uname;
string location;
Date establishDate;
};
const string uname = "London University";
?
答案 0 :(得分:0)
我想你想要的东西如下:
ostream & operator<<(ostream & out, University & x)
{
out<< x.uname << " in " << x.location << " has been established in: " << x.establishDate.month << "/" << x.establishDate.day << "/" << x.establishDate.year ;
return out;
}
这很有效,因为运营商是朋友,因此可以访问私人会员。