目录
前言
日期类非常经典,在日期类里包含了很多运算符的重载,以及构造函数、析构函数等。
构造函数
对于类的对象,我们可能会忘记调用初始化函数来对其初始化,所以有了构造函数,也就是说构造函数是用来初始化函数的。
构造函数是一个特殊的成员函数名字与类名相同,编译器创建类类型对象时会自动调用构造函数,保证每个数据成员都有一个初始值,而且在对象的生命周期内只调用一次。
注意构造函数并没有开辟空间,只是初始化。
特点:
1.函数名与类名相同
2.无返回值
3.编译器自动调用
4.可以重载
5.如果类中没有显示定义构造函数,那编译器会自动生成一个无参的构造函数,如果用户自 己显式定义了那编译器不会再生成。
析构函数
析构函数是用来完成类的资源清理工作的,在对象销毁时编译器会自动调用析构函数。
特点:
1.析构函数的名字是在类名前加 ~
2.没有参数也没有返回值
3.不能重载
4.一个类中有且只有一个析构函数,若未显式定义则自动生成默认的
5.对象生命周期结束时编译器自动调用析构函数
拷贝构造函数
拷贝构造函数又称复制构造函数,是一种特殊的构造函数,它由编译器调用来完成一些基于同一类的其他对象的构造及初始化。
拷贝构造分为浅拷贝和深拷贝。
拷贝构造的使用场景:
1.一个对象作为函数参数,以值传递的方式传入函数体;(函数传参,类类型的值传递)
class fun1{
};
void fun2(fun1 f1){
}
int main(){
fun1 f1;
fun2(f1);
}
2.一个对象作为函数返回值,以值传递的方式从函数返回;(函数是类类型的值返回,从局部对象到临时对象的拷贝构造)
fun1 temp(){
fun1 f;
return f;
}
3.一个对象用于给另外一个对象进行初始化(常称为赋值初始化);(用已有对象去初始化本类的对象)
int main(){
fun1 f1(1,2);
fun1 ff1(f1);//用f1去初始化ff1
}
日期类
class Date
{
public:
// 获取某年某月的天数
//也可以用数组来存储12个月对应的天数的方式来
int GetMonthDay(int year, int month){
if ((year % 100 != 0 && year % 4 == 0) || (year % 400 == 0)){ //闰年
if (month == 2)
return 29;
}
else{ //平年
if (month == 2)
return 28;
}
if (month == 4 || month == 6 || month == 6 || month == 9 || month == 11)
return 30;
else
return 31;
}
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1);
// 拷贝构造函数
Date(const Date& d){
_year = d._year;
_month = d._month;
_day = d._day;
}
// 赋值运算符重载
Date& operator=(const Date& d){
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
// 析构函数
~Date(){
cout << "~Date()" << endl;
}
// 日期+=天数
Date& operator+=(int day){
if (day < 0){
*this -= -day;
}
else{
_day += day;
while (_day > GetMonthDay(_year, _month)){
_day -= GetMonthDay(_year, _month);
_month += 1;
}
while (_month > 12){
_month -= 12;
_year += 1;
}
}
return *this;
}
// 日期+天数
Date operator+(int day){
Date ret(*this);
ret += day;
return ret;
}
// 日期-天数
Date operator-(int day){
Date tem = *this;
tem -= day;
return tem;
}
// 日期-=天数
Date& operator-=(int day){
if (day < 0){
*this += -day;
}
else{
_day -= day;
while (_day <= 0){
--_month;
if (_month == 0){
--_year;
_month == 12;
}
_day += GetMonthDay(_year, _month);
}
//*this -= day;
}
return *this;
}
// 前置++
Date& operator++(){
*this += 1;
return *this;
}
// 后置++
Date operator++(int){
Date ret = *this;
*this += 1;
return ret;
}
// 后置--
Date operator--(int){
Date ret = *this;
*this -= 1;
return ret;
}
// 前置--
Date& operator--(){
*this -= 1;
return *this;
}
// >运算符重载
bool operator>(const Date& d){
if (_year > d._year)
return true;
else if (_year == d._year){
if (_month > d._month)
return true;
else if (_month == d._month){
if (_day > d._day)
return true;
}
}
return false;
}
// ==运算符重载
bool operator==(const Date& d){
return _year == d._year && _month == d._month && _day == d._day;
}
// >=运算符重载
bool operator >= (const Date& d){
return *this > d || *this == d;
}
// <运算符重载
bool operator < (const Date& d){
if (_year < d._year)
return true;
else if (_year == d._year){
if (_month < d._month)
return true;
else if (_month == d._month){
if (_day < d._day)
return true;
}
}
return false;
}
// <=运算符重载
bool operator <= (const Date& d){
return *this < d || *this == d;
}
// !=运算符重载
bool operator != (const Date& d){
return !(*this == d);
}
// 日期-日期 返回天数
int operator-(const Date& d){
Date max = *this;
Date min = d;
int flag = 1;
if (*this < d){ //this小于d那么相减是负数
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max){
++min;
++n;
}
return n*flag;
}
private:
int _year;
int _month;
int _day;
};