C++繼承性應用實例日期和時間(VC 6.0實現)

#include "stdafx.h"
#include "iostream.h"//輸入輸出頭文件

typedef char string80[80];


//-------基類日期-----------//
class Date
{
public:
 Date() {}//構造函數
 Date(int y, int m, int d){ SetDate(y, m, d);}//帶參數構造函數
 void SetDate(int y, int m, int d)//設置日期的函數
 {
  Year = y;
  Month = m;
  Day = d;
  cout<<"初始化日期了"<<endl;
 }
 void GetStringDate(string80 &Date)//輸出日期的函數
 {
  cout<<"Date:"<<endl;
  cout<<Year<<Month<<Day<<endl;
 }
protected:
 int Year, Month, Day;
};


//-------基類時間-----------//
class Time
{
public:
 Time() {} //構造函數
 Time(int h, int m, int s) { SetTime(h, m, s); } //帶參數構造函數
 void SetTime(int h, int m, int s)//設置時間的函數
 {
  Hours = h;
  Minutes = m;
  Seconds = s;
  cout<<"初始化時間了"<<endl;
 }
 void GetStringTime(string80 &Time) //輸出時間的函數
 {
  cout<<"Time:"<<endl;
  cout<<Hours<<Minutes<<Seconds<<endl;
 }

protected:
 int Hours, Minutes, Seconds;
};

//-------派生類 時間日期-----------//
class TimeDate:public Date, public Time
{
public:
 TimeDate():Date() {} //構造函數
 TimeDate(int y, int mo, int d, int h, int mi, int s):Date(y, mo, d), Time(h, mi, s) {}//帶參數構造函數
 void GetStringDT(string80 &DTstr) //輸出時間日期的函數
 {
  cout<<"TimeDate"<<endl;
  cout<<Year<<Month<<Day<<Hours<<Minutes<<Seconds<<endl;
 }
};

 
//-------main()函數入口----------//
int main(int argc, char* argv[])
{
 TimeDate date1, date2(2008, 9, 10, 8, 30, 30);
 string80 DemoStr;
 date1.SetDate(2000,10,7);
 date1.SetTime(12, 30, 00);

 cout<<"The date1 date and time is :"<<endl;
 date1.GetStringDT(DemoStr);
 date1.GetStringDate(DemoStr);
 date1.GetStringTime(DemoStr);
 cout<<endl;
 
 cout<<"The date2 date and time is:"<<endl;
 date2.GetStringDT(DemoStr);
 date2.GetStringDate(DemoStr);
 date2.GetStringTime(DemoStr);
 cout<<endl;
 return 0;
}

發佈了30 篇原創文章 · 獲贊 10 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章