실습과제 모음

CPP 주말 과제 Time 클래스

TanSanC 2012. 7. 27. 15:19

#include <iostream>

#include <string>

using namespace std;

class Time

{

public :

   Time();

   Time(const int hour, const int min, const int sec);

   void SetHour(const int hour);

   void SetMin(const int min);

   void SetSec(const int sec);

   int GetHour();

   int GetMin();

   int GetSec();

   int CalSec();

   void ShowTime();

 

private :

   int hour, min, sec;

   int t_sec;

};

 

Time::Time()

{

   hour = 0;

   min = 0;

   sec = 0;

}

Time::Time(const int hour, const int min, const int sec)

{

   this->hour = hour;

   this->min = min;

   this->sec = sec;

}

void Time::SetHour(const int hour)

{

   this->hour = hour;

}

void Time::SetMin(const int min)

{

   this->min = min;

}

void Time::SetSec(const int sec)

{

   this->sec = sec;

}

int Time::GetHour()

{

   return hour;

}

int Time::GetMin()

{

   return min;

}

int Time::GetSec()

{

   return sec;

}

int Time::CalSec()

{

   // , ,

   // 단위로 변환

   int totalSec = 0;

   totalSec += 3600 * hour;

   totalSec += 60 * min;

   totalSec += sec;

   return totalSec;

}

void Time::ShowTime()

{

   cout << hour << " " << min << " " << sec << " 입니다." << endl;

}

 

int main()

{

   Time t1;

   Time t2(1,20,40);

   t1.SetHour(2);

   t1.SetMin(20);

   t1.SetSec(30);

 

  

// Time t3 = t1 + t2;

// Time t4 = t1 - t2;

// bool b1 = t1 > t2;

 

   cout << "t1 :" << t1.CalSec() << endl;

   cout << "t2 :" << t2.CalSec() << endl;

   t1.ShowTime();

   t2.ShowTime();

   return 0;

}