/* *Copyright (c) 2013, 烟台大学计算机学院 * All rights reserved. * 作 者:马广明 * 完成日期:2014 年 5 月 19 日 * 版 本 号:v1.0 * 问题描述:日期时间类 */ #include <iostream> using namespace std; class Date { public: Date(int y,int m,int d) { year=y; month=m; day=d; } void SetDate(int y,int m,int d) { year=y; month=m; day=d; } void PrintDate() { cout<<year<<"/"<<month<<"/"<<day<<'\t'; } protected: int year,month,day; }; class Time { public: Time(int h,int m,int s) { hour=h; minute=m; second=s; } void SetTime(int h,int m,int s) { hour=h; minute=m; second=s; } void PrintTime() { cout<<hour<<":"<<minute<<":"<<second<<endl; } protected: int hour,minute,second; }; class TimeDate:public Date,public Time { public: TimeDate(int y=0,int mon=0,int d=0,int h=0,int min=0,int s=0): Date(y,mon,d),Time(h,min,s){} void PrintDate_Time() { PrintDate(); PrintTime(); } }; int main() { TimeDate dt_a,dt_b(2014,5,20,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; }
时间类--多重继承
时间: 2024-10-16 17:05:00