(一)构造函数允许使用构造函数初始值列表,来为变量初始化 适用于成员变量有构造函数的任何情况
代码示例:
#include<iostream>
using namespace std;
class Date
{
public:
//构造函数初始化列表
Date(int y, int m, int d) :_year(y), _month(m), _day(d){}
//默认1970年1月1日 委托构造函数
Date() :Date(1970, 1, 1){}
//只给年初始化 月、日都为1 委托构造函数
Date(int y) :Date(y,1,1){}
//拷贝构造函数
Date(const Date & d) :_year(d._year),_month(d._month),_day(d._day){}
void Display()
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
~Date()
{
cout << "析构函数!" << endl;
}
/*bool operator==(const Date& d)
{
return this->_year == d._year&&this->_month == d._month&&this->_day == d._day;
}*/
friend bool operator==(const Date &d1, const Date &d2);
private:
int _year;
int _month;
int