01./*
02.*Copyright (c) 2014,烟台大学计算机学院
03.*All gight reserved.
04.*文件名称:temp.cpp
05.*作者:张震刚
06.*完成时间:2015年5月26日
07.*版本号:v1.0
08.*问题描述:分别定义Date和Time类,采用多重继承方式派生出新类DateTime,完成要求。
09.*输入描述:无。
10.*输出描述:无。
11.*/
#include <iostream>
using namespace std;
class Date
{
protected:
int year;
int month;
int day;
public:
Date():year(0),month(0),day(0){}
Date(int y,int m,int d);
int SetDate(int y,int m,int d);
void PrintDate();
};
Date::Date(int y,int m,int d):
year(y),month(m),day(d){}
int Date::SetDate(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
void Date::PrintDate()
{
cout<<year<<"/"<<month<<"/"<<day<<endl;
}
class Time
{
protected:
int hour;
int minuter;
int second;
public:
Time():hour(0),minuter(0),second(0){}
Time(int h,int m,int s);
int SetTime(int h,int m,int s);
void PrintTime();
};
Time::Time(int h,int m,int s):
hour(h),minuter(m),second(s){}
int Time::SetTime(int h,int m,int s)
{
hour=h;
minuter=m;
second=s;
}
void Time::PrintTime()
{
cout<<hour<<":"<<minuter<<":"<<second<<endl;
}
class TimeDate:public Date,public Time
{
public:
TimeDate():Date(),Time(){};
TimeDate(int y,int mo,int d,int h,int mi,int s):
Date(y,mo,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;
}
总结:多继承一般用法觉得已经熟练了,关于这个程序最后输出效果不懂为什么执行了换行。。。