두 클래스가 있습니다. 하나는 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
클래스는 establishDate
라는 유형 Date
의 객체를 가지고 있으며, 나는 대학 이름 및 위치와 함께 날짜를 출력하려면이 옵션을 사용해야합니다. 내가 개체 establishDate
를 어떻게 사용합니까
// 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";
:
여기에 클래스 University
입니까?
예를 들어'cout << establishedData'를 시도 했습니까? – Shahbaz
코드를 제대로 들여 씁니다. –
나는 Shahbaz 과 Konrad만큼 간단하지 않다고 생각하지만 여기 새로 온다 : P –