多继承实例

#include <iostream>
#include <string>
#include <stdlib>
using namespace std;


//定义一些列的类
class Date
{
private:
int year,month,day;
public:
Date(int,int,int);
void GetDate(char *);
~Date();
};
//构造函数
Date::Date(int y,int m,int d)
{
year = y;
month = m;
day = d;
cout<<"Date's constructor called!";
}


void Date::GetDate(char* s)
{
char t[20];
itoa(year,s,10);
stract(s,"/");
itoa(month,t,10);
stract(s,t);
stract(s,"/");
itoa(day,t,10);
stract(s,t);
}


class Time
{
private:
int hour,minute,second;
public:
Time(int h,int m,int s);
void GetTime(char *);
~Time();

};


Time::Time(int h,int m,int s)
{
hour = h;
month = m;
second = s;
cout<<"Time's constructor called!\n";
}


void Time::GetTime(char *s)
{
char t[20];
itoa(hour,s,10);
stract(s,":");
itoa(minute,t,10);
stract(s,t);
stract(s,":");
itoa(second,t,10);
stract(s,t);
}
//多继承  
class DateTime:public Date,public Time
{
public:
//多继承构造函数
DateTime(int y,int m,int d,int h,int min,int s):Date(y,m,d),Time(h,min,s)
{
cout<<"DateTime constructor called!\n";
};
void GetDateTime(char *);
~DateTime();

};


void GetDateTime(char* s)
{
char s1[100],s2[100];
GetDate(s1);
GetTime(s2);
strcpy(s,"日期和时间分别是:");
stract(s,s1);
stract(s,":");
stract(s,s2);
}


int main(int argc, char const *argv[])
{
/* code */
char s[50];
DateTime dt(2006,12,16,15,50,20);
dt.GetDateTime(s);
cout<<s<<"\n";
return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值