继承中的构造函数
程序员文章站
2022-05-07 21:53:39
...
#include<iostream>
using namespace std;
class Date
{ private: int year,month,day;
public:
Date(int y=2000,int m=1,int d=1){
year=y; month=m; day=y;
}
void print_Date()
{ cout<<year<<" "<<month<<" "<<day<<endl; }
};
class Time
{ private: int hour,minute,second;
public:
Time(int h=12,int m=0,int s=0)
{ hour=h; minute=m; second=s;
}
void print_Time()
{ cout<<hour<<":"<<minute<<":"<<second<<endl; }
};
class Date_Time:public Date,public Time
{ public: Date_Time(int y,int m,int d,int h,int mi,int s):Date(y,m,d),Time(h,mi,s)
{ }
void print(){ print_Date(); print_Time();}
};
int main(){
Date_Time x(2018,7,11,16,50,59);
x.print();
}
上一篇: 父类的构造函数