C++初学之 4.生成类以及类的派生(两个基类,一个多重派生类)

#include <iostream>
using namespace std;
void main(){
	class timetype{
		int second,minute,hour;
	public:
		timetype(int s=0,int m=0,int h=0){second=s;minute=m;hour=h;}
		void timeset(int s,int m,int h){second=s;minute=m;hour=h;}
		void display(){
			cout<<"The current time is: "<<hour<<':'<<minute<<':'<<second<<endl;}
	};
	class datetype{
		int day,month,year;
	public:
		datetype(int d=1,int mo=1,int y=2000){day=d;month=mo;year=y;}
		void dateset(int d,int mo,int y){day=d;month=mo;year=y;}
		void display(){
			cout<<"The current date is: "<<day<<'/'<<month<<'/'<<year<<endl;}
	};
	class datetimetype:public datetype,public timetype{//多重派生
	public:
		datetimetype(int s=0,int m=0,int h=0,int d=1,int mo=1,int y=2000):timetype(s,m,h),datetype(d,mo,y){};//派生类新增加成员的初始化;
		void display(){
			datetype::display();
			timetype::display();
		}
	};
	datetimetype t1;
	cout<<"The initial time is: "<<endl;
	t1.display();
	t1.timeset(56,20,0);//由于timeset 在基类timetype中为public,而且派生datetimetype时也是public派生故可直接访问基类成员函数 member function
	t1.dateset(21,7,1988);
	cout<<"The modified time is: "<<endl;
	t1.display();
}

  

posted @ 2012-09-11 13:50  LeeKing  阅读(516)  评论(0编辑  收藏  举报