2013-12-21 4 views
0

두 클래스가 있습니다. 하나는 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입니까?

+1

예를 들어'cout << establishedData'를 시도 했습니까? – Shahbaz

+1

코드를 제대로 들여 씁니다. –

+0

나는 Shahbaz 과 Konrad만큼 간단하지 않다고 생각하지만 여기 새로 온다 : P –

답변

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; 
} 

운영자가 친구이고, 따라서 개인 회원에 액세스 할 수 있기 때문에이 작동합니다.

+0

그것은 (어제, 일, 년)에 접근 할 수 없다고 말합니다, 사람들은 이것들이 어리석은 질문이라는 것을 압니다. 그러나 저는 스스로 학습하고 있습니다. 어쨌든 그 변수들은 Date 클래스에서 private입니다. 이 연산자는 대학 수업에 –

+0

아 아빠. 대신 << establisheddate를 사용하거나 날짜 클래스에 public get 함수를 추가하십시오. – cageman

관련 문제