본문 바로가기

과제모음

[C++]다중상속

반응형

[[확인문제 2]]

 

============================================>>Date.h<<

#ifndef _DATE_H_
#define _DATE_H_

class Date
{
private:
 int year;
 int month;
 int day;
public:
 Date(int _YY, int _MM, int _DD);
 void OutDate();
 int Year();
 int Month();
 int Day();
 ~Date();
};

#endif

 

============================================>>Time.h<<

#ifndef _TIME_H_
#define _TIME_H_

class Time
{
private:
 int hour;
 int min;
 int sec;
public:
 Time(int _HH, int _MI, int _SEC);
 void OutTime();
 int Hour();
 int Min();
 int Sec();
 ~Time(){}
};

#endif

 

============================================>>Now.h<<

#ifndef _NOW_H_
#define _NOW_H_
#include "Time.h"
#include "Date.h"
class Now: public Date, public Time
{
private:
 int msec;
public:
 Now(int _YY, int _MM, int _DD, int _HH, int _MI, int _SEC, int _MSEC);
 void OutNow();

 ~Now(){}
};

#endif

 

============================================>>main.cpp<<

#include <iostream>
#include "Date.h"
#include "Time.h"
#include "Now.h"

int main()
{
 Date d(1994, 3, 5);
 d.OutDate();

 Time t(3, 56, 32);
 t.OutTime();

 Now n(2005, 1, 2, 12, 30, 58, 99);
 n.OutNow();

 return 0;
}

 

============================================>>Date.cpp<<

#include "Date.h"
#include <iostream>
using std::cout; using std::endl;

Date::Date(int _YY, int _MM, int _DD)
{
 year = _YY;
 month = _MM;
 day = _DD;
}
void Date::OutDate()
{
 cout << year << " / " << month << " / " << day << endl;
}
int Date::Year()
{
 return year;
}
int Date::Month()
{
 return month;
}
int Date::Day()
{
 return day;
}

Date::~Date(){}

 

============================================>>Time.cpp<<

#include "Time.h"
#include <iostream>
using std::cout;
using std::endl;

Time::Time(int _HH, int _MI, int _SEC)
{
 hour = _HH;
 min = _MI;
 sec = _SEC;
}
void Time::OutTime()
{
 cout << hour << " : " << min << " : " << sec << endl;
}
int Time::Hour()
{
 return hour;
}
int Time::Min()
{
 return min;
}
int Time::Sec()
{
 return sec;
}

============================================>>Now.cpp<<

#include "Now.h"
#include <iostream>
using std::cout;

Now::Now(int _YY, int _MM, int _DD, int _HH, int _MI, int _SEC, int _MSEC) : Date(_YY,_MM,_DD), Time(_HH,_MI,_SEC)
{
 msec = _MSEC;
}

void Now::OutNow()
{
 cout << Year() << "년 " << Month() << "월 " << Day() << "년 "
  << Hour() << "시 " << Min() << "분 " << Sec() << "초 " << msec << "밀리초\n";
}


반응형

'과제모음' 카테고리의 다른 글

[C Lang]자료의 입출력  (0) 2010.01.22
[C Lang]자료형, 변수, 상수  (0) 2010.01.22
[C++]상속3  (0) 2010.01.22
[C++]Inheritance(상속2)  (0) 2010.01.22
[C++]Inheritance(상속)  (0) 2010.01.22