继承练习(两个基类派生出一个类)

作者在 2014-05-02 22:43:44 发布以下内容
#include <iostream>
using namespace std;
typedef char string80 [80];
class Data
{
public:
	Data() {}
	Data(int y, int m, int d)
	{
		SetData(y,m,d);
	}
	void SetData(int y,int m,int d)
	{
		Year=y;
		Month=m;
		Day=d;
	}
	string80 &GetStringData(string80 &Data)
	{
		sprintf(Data,"%d/%d/%d",Year,Month,Day);
		return Data;
	}
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;
	}
	string80 &GetStringTime(string80 &Time)
	{
		sprintf(Time,"%d:%d:%d",Hours,Minutes,Seconds);
		return Time;
	}
protected:
	int Hours,Minutes,Seconds;
};
class TimeData:public Data,public Time
{
public:
	TimeData():Data() {}
	TimeData(int y,int mo,int d,int h,int mi,int s):Data(y,mo,d),Time(h,mi,s)
	{}
	string80 &GetStringDT(string80 &DTstr)
	{
		sprintf(DTstr,"%d/%d/%d;%d:%d:%d",Year,Month,Day,Hours,Minutes,Seconds);
		return DTstr;
	}
};
void main()
{
	TimeData date1,date2(2008,6,5,10,15,05);
	string80 DemoStr;
	date1.SetData(2008,9,10);
	date1.SetTime(19,07,30);
	cout<<"The date1 data and time is:"<<date1.GetStringDT(DemoStr)<<endl;

	cout<<"The date1 time is"<<date1.GetStringData(DemoStr)<<endl;
	cout<<"The date2 date and time is"<<date2.GetStringDT(DemoStr)<<endl;
}
c++ | 阅读 1631 次
文章评论,共0条
游客请输入验证码
浏览37866次
最新评论