29.默认构造函数

构造函数的任务是 初始化类对象的数据成员,创建对象。无论何时只要类的对象被创建,就会自动执行构造函数!

不同于其他的成员函数,构造函数不能被声明为const。当我们为类创建一个const对象的时候,直到构造函数执行完初始化过程,对象才算真正的获得常量属性。因此,构造函数可以在const对象创建的过程中向其写值!


默认构造函数又称为合成构造函数!

对于大多数的类来说,执行合成构造函数按照如下规则初始化成员:

如果成具有类内初始值,那用它来直接初始化成员;否则默认初始化


当我们自定义的类中没有给出自定义的构造函数,那么在创建对象的时候,系统会为我们自动合成合成构造函数!一旦我们定义了其他的构造函数,系统将不会为我们再次合成构造函数!(但通过 =default 可以实现 合成构造函数和自定义构造函数同时存在


对于数据成员,构造函数初始化列表执行初始化,构造函数体执行赋值

### 完整的C++日期类实现 以下是一个完整的C++程序,实现了包含私有成员变量 `year`、`month` 和 `day` 的日期类。该类包含带参数的构造函数、拷贝构造函数、前置和后置 `++` 运算符重载、友元函数 `-` 运算符重载以及输出函数。 ```cpp #include <iostream> using namespace std; class Date { private: int _year; int _month; int _day; // 辅助函数:检查日期是否合法 bool isValidDate(int year, int month, int day) const { if (month < 1 || month > 12) return false; int getMonthDay[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { getMonthDay[2] = 29; // 闰年 } if (day < 1 || day > getMonthDay[month]) return false; return true; } public: // 带参数的构造函数 Date(int year = 1900, int month = 1, int day = 1) { if (isValidDate(year, month, day)) { _year = year; _month = month; _day = day; } else { cout << "Invalid date. Setting default date: 1900-01-01" << endl; _year = 1900; _month = 1; _day = 1; } } // 拷贝构造函数 Date(const Date& d) { _year = d._year; _month = d._month; _day = d._day; } // 前置++运算符重载 Date& operator++() { int getMonthDay[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0)) { getMonthDay[2] = 29; // 闰年 } if (_day < getMonthDay[_month]) { _day++; } else { _day = 1; if (_month < 12) { _month++; } else { _month = 1; _year++; } } return *this; } // 后置++运算符重载 Date operator++(int) { Date temp = *this; ++(*this); return temp; } // 友元函数 - 运算符重载 friend Date operator-(const Date& lhs, const Date& rhs); // 输出函数 void show() const { cout << _year << "-" << _month << "-" << _day << endl; } }; // 友元函数 - 运算符重载实现 Date operator-(const Date& lhs, const Date& rhs) { // 简单示例:假设只处理同年的日期差值 if (lhs._year != rhs._year) { cout << "Year mismatch. Cannot calculate difference." << endl; return lhs; } int daysInMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if ((lhs._year % 4 == 0 && lhs._year % 100 != 0) || (lhs._year % 400 == 0)) { daysInMonth[2] = 29; // 闰年 } int diff = 0; for (int i = 1; i < lhs._month; ++i) { diff += daysInMonth[i]; } diff += lhs._day; for (int i = 1; i < rhs._month; ++i) { diff -= daysInMonth[i]; } diff -= rhs._day; return Date(lhs._year, 1, abs(diff) + 1); // 返回一个表示天数差异的日期对象 } int main() { Date d1(2023, 11, 15); Date d2(d1); // 使用拷贝构造函数 d1.show(); d2.show(); ++d1; // 前置++ d1.show(); Date d3 = d2++; // 后置++ d2.show(); d3.show(); Date d4 = d1 - d2; // 使用友元函数- d4.show(); return 0; } ``` ### 代码说明 1. **构造函数** 构造函数用于初始化日期对象,并检查输入日期是否合法[^1]。如果输入日期不合法,则设置默认日期为 `1900-01-01`。 2. **拷贝构造函数** 拷贝构造函数用于创建新对象时从现有对象进行初始化。它确保了对象的深拷贝[^1]。 3. **前置++运算符重载** 前置++运算符将日期递增一天。如果当前日期是当月最后一天,则自动进入下一个月;如果是年末,则进入下一年[^4]。 4. **后置++运算符重载** 后置++运算符返回原日期的一个副本,然后将原日期递增一天[^4]。 5. **友元函数-运算符重载** 友元函数用于计算两个日期之间的天数差异。这里假设两个日期在同一年内[^3]。 6. **输出函数** 输出函数以 `YYYY-MM-DD` 格式显示日期[^1]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值