拷贝构造和运算符重载
1.拷贝构造的介绍
拷贝构造是用一个已经存在的对象来赋值给另一个正在创建对象
日期类的拷贝构造实现
//Date(const Date& d); 拷贝构造第一个参数必须是引用 Date::Date(const Date& d) {_year = d._year;_month = d._month;_day = d._day; }
2.运算符重载的介绍
运算符重载就是为了实现自定义类型直接的运算符操作 关键字为operator
日期类的运算符重载实现
Date& Date::operator+=(int day) {if (day < 0){return *this -= -day;}_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month += 1;if (_month > 12){_month = 1;_year+=1;}}return *this; } // 日期+天数 Date Date::operator+(int day) const {Date tmp = *this;tmp += day;return tmp; }
完