/*
*Copyright(c)2014,烟台大学计算机学院
*All rights reserved.
*文件名称:test.cpp
*作者:曾晓
*完成日期:2015年 5月 26日
*版本号:v1.0
*/
#include <iostream>
using namespace std;
class Date
{
public:
Date(int y=0,int m=0,int d=0);
void SetDate(int y,int m,int d);
void PrintDate();
protected:
int year;
int month;
int day;
};
Date::Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
void Date::SetDate(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
void Date::PrintDate()
{
cout<<year<<"/"<<month<<"/"<<day<<endl;
}
class Time
{
public:
Time(int h=0,int mi=0,int s=0);
void SetTime(int h,int mi,int s);
void PrintTime();
protected:
int hour;
int minute;
int second;
};
Time::Time(int h,int mi,int s)
{
hour=h;
minute=mi;
second=s;
}
void Time::SetTime(int h,int mi,int s)
{
hour=h;
minute=mi;
second=s;
}
void Time::PrintTime()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
class TimeDate:public Date,public Time
{
public:
TimeDate():Date(),Time(){};
TimeDate(int y,int m,int d,int h,int mi,int s):Date(y,m,d),Time(h,mi,s){}
void PrintDate_Time();
};
void TimeDate::PrintDate_Time()
{
PrintDate();
cout<<" ";
PrintTime();
}
int main()
{
TimeDate dt_a,dt_b(2010,4,16,9,30,0);
cout<<"dt_a: ";
dt_a.PrintDate_Time();
cout<<endl;
cout<<"dt_b: ";
dt_b.PrintDate_Time();
dt_a.SetTime(20,00,00);
dt_a.SetDate(2008,8,7);
cout<<endl;
cout<<"dt_after uptate: ";
dt_a.PrintDate_Time();
return 0;
}
运行结果:
抛开那些模板 自己写出来的代码更加让人觉得有成就感 虽然每次都是十几个错误><