一、类的继承例子

#include<iostream>
using namespace std;
class DataType {
private:
	int year, month, day;
public:
	DataType(int year_ = 1997, int month_=10, int day_ = 6) {
		year = year_;
		month = month_;
		day = day_;
		cout << "DataType的构造函数" << endl;
	}
	//也可以这样写
	/*DataType(int year = 1997, int month = 10, int day = 6) {
		this->year = year;
		this->month = month;
		this->day = day;
		cout << "DataType的构造函数" << endl;
	}*/
	void display() {
		cout << year << "年" << month << "月" << day << "日" << endl;
	}
	~DataType() {
		cout << "DataType的析构函数" << endl;
	}
};
class TimeType {
private:
	int h, m, s;
public:
	TimeType(int h_ = 12, int m_ = 30, int s_ = 30) {
		h = h_;
		m = m_;
		s = s_;
		cout << "TimeType的构造函数" << endl;
	}
	void display() {
		cout << h << "时" << m << "分" <<s << "秒" << endl;
	}
	~TimeType() {
		cout << "TimeType的析构函数" << endl;
	}
};
class DataTimeType :public DataType, public TimeType {
public:
	DataTimeType(int year_,int month_,int day_,int h_,int m_,int s_):TimeType(h_,m_,s_),DataType(year_,month_,day_){}
	void display() {
		DataType::display();
		TimeType::display();
	}
};
int main() {
	DataTimeType dt(2014,5,12,17,2,10);
	dt.display();
	//system("pause");
	return 0;
}
上一篇:buger/jsonparser 源码简析


下一篇:Java数组笔记