当前位置: 首页 > news >正文

C++学习之路,从0到精通的征途:类和对象(中)

目录

一.类的默认成员函数

二.构造函数

1.构造函数的定义

2.构造函数的特点

三.析构函数  

1.析构函数的定义

2.析构函数的特点     

四.拷贝构造函数 

1.拷贝构造函数的定义

2.拷贝构造函数的特点

五.赋值运算符重载 

1.运算符重载

2.赋值运算符重载

2.1赋值运算的定义 

2.2赋值运算符重载的特点

3.日期类实现 

Date.h

Date.cpp

test.cpp

六.取地址运算符重载

1.const成员函数

2.取地址运算符重载


一.类的默认成员函数

        默认函数就是用户没有显式定义,编译器自动生成的函数,在C++98标准中,一个类中,当我们不写时,编译器会生成以下6个默认成员函数:

        本期我们介绍的是前四种默认成员函数,并从两个方面去学习:

        1.我们不写时,编译器默认生成的函数行为是什么,是否满足我们的需求。 

        2.编译器默认生成的函数不满足我们的需求,我们需要自己实现,那么如何自己实现?

二.构造函数

1.构造函数的定义

        构造函数是特殊的成员函数,构造函数虽命名为构造,但其主要任务并非开空间创建对象,而是在对象实例化时对其进行初始化,它能够通过自身自动调用的特性完美代替Stack和Date类中的Init函数。

2.构造函数的特点

(1)函数名与类名相同

(2)无返回值(无需写void)

(3)对象实例化时会自动调用对应的构造函数        

(4)构造函数可以重载

        可以看到这里由于d1,d2传参的不同,调用了对应的构造函数。

        要注意的是d1传参并不能写成如下这种方式,与函数的声明无法区分:

(5)类中没有显式定义构造函数的情况下,编译器会自动生成一个无参的默认构造函数,用户一旦显式定义,编译器则不再生成

(6)无参构造函数、全缺省构造函数、我们不写构造时编译器默认生成的构造函数,都叫做默认构造函数。但是这三个函数有且只有一个存在,不能同时存在。即不传实参就调用的构造叫做默认构造。

(7) 我们不写,编译器默认生成的构造,对内置类型成员变量的初始化没有要求,也就是说是否初始化是不确定的,取决于编译器
        
        C++把类型分成内置类型(基本类型)和自定义类型。内置类型就是语言提供的原生数据类型,
如:int/char/double/指针等,自定义类型就是我们使用class/struct等关键字自己定义的类型。

        在vs2022的编译环境下,可以看到在我们没有显示定义构造时,编译器对内置类型并未作处理,打印的日期为随机数。 

        对于自定义类型成员变量,要求调用这个成员变量的默认构造函数初始化。

        这里用MyQueue类举例,MyQueue的底层实现逻辑可以参考用栈实现队列

        由于MyQueue并未显式定义构造函数,编译器自动生成默认构造函数,并且MyQueue的成员变量均为自定义类型,编译器生成的默认构造函数调用Stack的默认构造并将pushst和popst初始化,通过调试也可观察调用过程。      

        如果这个成员变量,没有默认构造函数,那么就会报错,我们要初始化这个成员变量,需要⽤初始化列表才能解决,初始化列表,我们下个章节再细细讲解。

        总结,对MyQueue这样含有自定义类型成员的类,我们不需要显式写构造函数,对于Date类这样含有内置类型的成员变量,我们需要显式写构造函数。 

三.析构函数  

1.析构函数的定义

        析构函数与构造函数的功能相反,析构函数不是完成对对象本身的销毁,而是清理释放对象申请的资源,析构函数的功能可以类比Stack中实现的Destory。

2.析构函数的特点     

(1)析构函数名是在类名前加上字符 ~。(~也代表按位取反)

(2)无参数无返回值。(无需写void)

(3)一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。

(4)对象生命周期结束时,系统会自动调用析构函数。

        以Stack为例:

(5)跟构造函数类似,我们不写编译器自动生成的析构函数对内置类型成员不做处理,自定类型成员会调用他的析构函数。

        以Date类为例:

        由于Date类的成员变量全是内置类型,编译器自动生成的函数对内置类型不做处理,打印结果不变。

        以MyQueue类为例:

        由于MyQueue并未显式写析构函数,在局部对象生命周期结束时,调用编译器生成的默认析构,由于MyQueue的成员变量为自定义类型,默认析构调用Stack的析构函数,将pushst和popst销毁,通过调试也可以看到调用过程。

(6)还需要注意的是我们显式写析构函数,对于自定义类型成员也会调用他的析构,也就是说自定义类型成员无论什么情况都会自动调用析构函数。

        我们显式写了MyQueue的析构函数后,在调试过程中先进入了~MyQueue函数,之后再进入了~Stack函数,说明自定义类型成员无论是否写析构函数,都会调用自身的析构。

(7)如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,如Date;如果默认生成的析构就可以用,也就不需要显示写析构,如MyQueue;但是有资源申请时,一定要自己写析构,否则会造成资源泄漏,如Stack。

        

        Stack类如果不写析构,编译器默认生成的析构函数由于不对内置类型作处理,_a所指向的空间在对象出生命周期后不会被释放,从而造成内存泄漏。

(8)一个局部域的多个对象,C++规定后定义的先析构。 

四.拷贝构造函数 

1.拷贝构造函数的定义

        如果一个构造函数的第一个参数是自身类类型的引用,且任何额外的参数都有默认值,则此构造函数也叫做拷贝构造函数,也就是说拷贝构造是⼀个特殊的构造函数。

2.拷贝构造函数的特点

(1)拷贝构造函数是构造函数的一个重载。

(2)拷贝构造函数的第一个参数必须是类类型对象的引用,使用传值方式编译器直接报错,因为语法逻辑上会引发无穷递归调用。拷贝构造函数也可以多个参数,但是第一个参数必须是类类型对象的引用,后面的参数必须有缺省值。

        这里拷贝构造形参使用const的原因为避免出现以下拷贝对象写错将d2传给d1,用const可以避免权限放大的情况:

 (3)C++规定,自定义类型的传值传参和传值返回都必须先进行拷贝构造。

        分析:为何使用传值的方式会发生无穷递归调用

        首先我们需要了解自定义类型的传值传参的方式,假设此时Date类型的拷贝构造函数是正确的引用传参:

        这里调用func后,得先对d1进行拷贝构造,将自定义类型拷贝传给func的形参d1后。再进入func。

        即自定义类型在传值传参前先要进行拷贝构造。

        那么当形参为传值传参的情况下,想要初始化d2,就得调用拷贝构造,调用之前得先传值传参,自定义的传值传参又得调用新的拷贝构造,从而构成无穷递归。

        逻辑上这样传值传参会造成无穷递归,但编译器会强行检查并报错:

(4)若未显式定义拷贝构造,编译器会生成自动生成拷贝构造函数。自动生成的拷贝构造对内置类型成员变量会完成值拷贝/浅拷贝(一个字节一个字节的拷贝),对自定义类型成员变量会调用他的拷贝构造。

        对内置类型成员变量,以Date类为例:

        可以看到,在不显式写拷贝构造的情况下,编译器自动生成的拷贝构造将d1中的内置类型浅拷贝给d2,其打印结果相同。

        对自定义类型成员变量,以MyQueue为例:        

        在我们显式定义Stack的拷贝构造下,调试中也可以看到调用了Stack的拷贝构造的过程:

(5)如果一个类显式实现了析构并释放资源,那么他就需要显式写拷贝构造,否则就不需要。

        像Date这样的类成员变量全是内置类型且没有指向什么资源,编译器自动生成的拷贝构造就可以完成需要的拷贝,所以不需要我们显式实现拷贝构造。

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	
	//编译器自动生成拷贝构造

	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

        像Stack这样的类,虽然也都是内置类型,但是_a指向了资源:

        当没有显式定义Stack的拷贝构造时,编译器会自动生成拷贝构造,并将st1的成员变量逐字节拷贝给st2,从而导致两个对象中的数组指向同一块空间,在程序结束自动调用析构时,这块空间会被析构两次,从而发生报错:

        所以对于Stack的拷贝构造我们需要自己定义,对_a实现深拷贝:

        这样就能让两个对象中的数组不同时占用一块空间了。

        像MyQueue这样的类型内部主要是自定义类型Stack成员,编译器自动生成的拷贝构造会调用Stack的拷贝构造,也不需要我们显式实现MyQueue的拷贝构造。

typedef int STDataType;
class Stack
{
public:
	Stack(int n = 4)
	{
		_a = (STDataType*)malloc(sizeof(STDataType) * n);
		if (nullptr == _a)
		{
			perror("malloc申请空间失败");
			return;
		}
		_capacity = n;
		_top = 0;
	}

	Stack(const Stack& st)
	{
		_a = (STDataType*)malloc(sizeof(STDataType) * st._capacity);
		if (nullptr == _a)
		{
			perror("malloc申请空间失败!!!");
			return;
		}
		memcpy(_a, st._a, sizeof(STDataType) * st._top);
		_top = st._top;
		_capacity = st._capacity;
	}

	~Stack()
	{
		free(_a);
		_a = nullptr;
		_top = _capacity = 0;
	}
private:
	STDataType* _a;
	size_t _capacity;
	size_t _top;
};


class MyQueue
{
public:
private:
	Stack pushst;
	Stack popst;
};

        总结,如果一个类中成员变量申请了资源,就需要显式写析构函数释放资源,那么再拷贝构造时就需要深拷贝,就需要我们显式写拷贝构造函数。 

(6)传值返回会产生一个临时对象调用拷贝构造,若为传值引用返回,返回的是返回对象的别名(引用),没有产生拷贝。

        func传值返回:

        输出打印结果,传值返回调用拷贝函数。 

        func传引用返回:

        未打印,传引用返回并未调用拷贝构造函数。 

        但传引用返回与引用传参不同并不能大用特用:

        如果返回对象是⼀个当前函数局部域的局部对象,函数结束就销毁了,那么使用引用返回是有问题的,这时的引用相当于⼀个野引用,类似⼀个野指针一样。传引用返回可以减少拷贝,但是一定要确保返回对象,在当前函数结束后还在,才能用引用返回。

五.赋值运算符重载 

1.运算符重载

(1)当运算符被用于类类型的对象时,C++语言允许我们通过运算符重载的形式指定新的含义。C++规定类类型对象使用运算符时,必须转换成调用对应运算符重载,若没有对应的运算符重载,则会编译报错。

(2)运算符重载是具有特殊名字的函数,他的名字是由operator和后面要定义的运算符共同构成。和其他函数一样,它也具有其返回类型和参数列表以及函数体。

        以重载==为例:

        若将opertor定义为全局函数

        为了解决重载为全局的面临对象访问私有成员变量的问题有以下几种方法可以解决:

        1.成员放共有

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
//private:
	int _year;
	int _month;
	int _day;
};

bool operator==(const Date& d1, const Date& d2)
{
	return d1._year == d2._year
		&& d1._month == d2._month
		&& d1._day == d2._day;
}

        2.Date提供getxxx函数

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

	int GetYear()
	{
		return _year;
	}

	int GetMonth()
	{
		return _month;
	}

	int GetDay()
	{
		return _day;
	}
private:
	int _year;
	int _month;
	int _day;
};

bool operator==(Date& d1, Date& d2)
{
	return d1.GetYear() == d2.GetYear()
		&& d1.GetMonth() == d2.GetMonth()
		&& d1.GetDay() == d2.GetDay();
}

        3.友元函数

        4.重载为成员函数(推荐)

        注意这里重载为成员函数后,参数包含隐含的this指针,所以形参个数减少一个

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

	bool operator==(const Date& d)
	{
		return _year == d._year
			&& _month == d._month
			&& _day == d._day;
	}
private:
	int _year;
	int _month;
	int _day;
};

        运算符重载也可以显式调用:

(3)重载运算符函数的参数个数和该运算符作用的运算对象数量一样多。一元运算符有一个参数,二元运算符有两个参数,二元运算符的左侧运算对象传给第一个参数,右侧运算对象传给第二个参数。

(4)如果一个重载运算符函数是成员函数,则它的第一个运算对象默认传给隐式的this指针,因此运算符重载作为成员函数时,参数比运算对象少一个。

(5)运算符重载以后,其优先级和结合性与对应的内置类型运算符保持一致。 

(6)不能通过连接语法中没有的符号来创建新的操作符:比如operator@。

(7)注意以下5个运算符不能重载。

.* :: sizeof ?: . 

        .*是C++特有的运算符,用来调用成员函数:

(8)重载操作符至少有⼀个类类型参数,不能通过运算符重载改变内置类型对象的含义如:

int operator+(int x, int y)

(9)一个类需要重载哪些运算符,是看哪些运算符重载后有意义,比如Date类重载operator-就有意义,但是重载operator*就没有意义。

(10)重载++运算符时,有前置++和后置++,运算符重载函数名都是operator++,无法很好的区分。C++规定,后置++重载时,增加一个int形参,跟前置++构成函数重载,方便区分。

// 前置++
Date& operator++();
// 后置++
Date operator++(int);

(11)重载<<和>>时,需要重载为全局函数,因为重载为成员函数,this指针默认抢占了第一个形参位置,第一个形参位置是左侧运算对象,调用时就变成了对象<<cout,不符合使用习惯和可读性。重载为全局函数把ostream/istream放到第一个形参位置就可以了,第二个形参位置当类类型对象。

2.赋值运算符重载

2.1赋值运算的定义 

        赋值运算符重载是一个默认成员函数,用于完成两个已经存在的对象直接的拷贝赋值,这里要注意跟拷贝构造区分,拷贝构造用于一个对象拷贝初始化给另一个要创建的对象。

	Date d1;

	//拷贝构造
	Date d2(d1);

	Date d3;

	//赋值重载
	d3 = d2;

	//拷贝构造
	Date d4 = d3;

2.2赋值运算符重载的特点

(1)赋值运算符重载是一个运算符重载,规定必须重载为成员函数。赋值运算重载的参数建议写成 const 当前类类型引用,否则会传值传参会有拷贝 

(2)有返回值,且建议写成当前类类型引用,引用返回可以提高效率,有返回值目的是为了支持连续赋值场景

(3)没有显式实现时,编译器会自动生成⼀个默认赋值运算符重载,默认赋值运算符重载行为跟默认拷贝构造函数类似,对内置类型成员变量会完成值拷贝/浅拷贝(一个字节一个字节的拷贝),对自定义类型成员变量会调用他的赋值重载函数

(4) 像Date这样的类成员变量全是内置类型且没有指向什么资源,编译器自动生成的赋值运算符重载就可以完成需要的拷贝,所以不需要我们显式实现赋值运算符重载。像Stack这样的类,虽然也都是内置类型,但是_a指向了资源,编译器自动生成的赋值运算符重载完成的值拷贝/浅拷贝不符合我们的需求,所以需要我们自己实现深拷贝(对指向的资源也进行拷贝)。像MyQueue这样的类型内部主要是自定义类型Stack成员,编译器自动生成的赋值运算符重载会调用Stack的赋值运算符重载,也不需要我们显式实现MyQueue的赋值运算符重载。这里还有一个小技巧,如果一个类显式实现了析构并释放资源,那么他就需要显式写赋值运算符重载,否则就不需要。

3.日期类实现 

Date.h

#pragma once
#include<iostream>
#include<assert.h>
using namespace std;

class Date
{
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month)
	{
		//assert(month > 0 && month < 13);
		static int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		// 判断是否为闰年
		if (month == 2 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
		{
			return 29;
		}
		else
		{
			return monthDayArray[month];
		}
	}

	// 全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	// 拷贝构造函数
	// d2(d1)
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	// 赋值运算符重载
	// d2 = d3 -> d2.operator=(&d2, d3)
	Date& operator=(const Date& d);

	// 日期+=天数
	Date& operator+=(int day);

	// 日期+天数
	Date operator+(int day);

	// 日期-天数
	Date operator-(int day);

	// 日期-=天数
	Date& operator-=(int day);

	// 前置++
	Date& operator++();

	// 后置++
	Date operator++(int);

	// 后置--
	Date operator--(int);

	// 前置--
	Date& operator--();

	// >运算符重载
	bool operator>(const Date& d);

	// ==运算符重载
	bool operator==(const Date& d);

	// >=运算符重载
	bool operator >= (const Date& d);

	// <运算符重载
	bool operator < (const Date& d);

	// <=运算符重载
	bool operator <= (const Date& d);

	// !=运算符重载
	bool operator != (const Date& d);

	// 日期-日期 返回天数
	int operator-(const Date& d);

	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

Date.cpp

#include"Date.h"

Date& Date::operator=(const Date& d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	return *this;
}

Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		return *this -= -day;
	}
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
		if (_month == 13)
		{
			_year += 1;
			_month = 1;
		}
	}
	return *this;
}

Date Date::operator+(int day)
{
	Date tmp(*this);
	tmp += day;
	return tmp;
}

Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += -day;
	}
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

Date Date::operator-(int day)
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}

Date& Date::operator++()
{
	*this += 1;
	return *this;
}

Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}


Date Date::operator--(int)
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}


Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

bool Date::operator>(const Date& d)
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year && _month > d._month)
	{
		return true;
	}
	else if (_year == d._year && _month == d._month && _day > d._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Date::operator==(const Date& d)
{
	return _year == d._year 
		&& _month == d._month 
		&& _day == d._day;
}

bool Date::operator >= (const Date& d)
{
	return *this > d || *this == d;
}

bool Date::operator < (const Date& d)
{
	return !(*this >= d);
}

bool Date::operator <= (const Date& d)
{
	return !(*this > d);
}

bool Date::operator != (const Date& d)
{
	return !(*this == d);
}

int Date::operator-(const Date& d)
{
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++n;
		++min;
	}
	return n * flag;
}

test.cpp

#include"Date.h"

void TestDate1()
{
	Date d1(2025, 3, 12);
	d1.Print();
	Date d2 = d1 + 100;
	d2.Print();
}

void TestDate2()
{
	Date d1(2025, 3, 13);
	d1.Print();
	Date d2 = d1 - (- 100);
	d2.Print();
}

void TestDate3()
{
	Date d1(2025, 3, 12);
	d1.Print();
	--d1;
	d1.Print();
	Date d2 = d1--;
	d2.Print();
	d1.Print();
}

void TestDate4()
{
	Date d1(2025, 3, 12);
	Date d2(d1);
	Date d3(2025, 2, 11);
	Date d4(2026, 3, 12);
	if (d1 <= d4)
	{
		cout << "<=" << endl;
	}
}

void TestDate5()
{
	Date d1(2005, 9, 25);
	Date d2(2025, 3, 13);
	cout << d2 - d1 << endl;
}

int main()
{
	//TestDate1();
	//TestDate2();
	//TestDate3();
	TestDate5();
	return 0;
}

六.取地址运算符重载

1.const成员函数

        将const修饰的成员函数称之为const成员函数,const修饰成员函数放到成员函数参数列表的后面。

void Print() const

        由于成员函数的参数隐式包含了this指针,导致const不能直接修饰this,所以const放在成员函数参数列表后,实际修饰成员函数所含的this指针,表明在该成员函数中不能对类的任何成员进行修改。 const 修饰Date类的Print成员函数,Print隐含的this指针由 Date* const this 变为 const Date* const this

//Print原型: void Print(const Date* const this)
void Print() const
{
	cout << _year << "-" << _month << "-" << _day << endl;
}

        当用const修饰成员函数时,声明和定义都要加上const。

        继续完善日期类:

Date.h

#include<iostream>
#include<assert.h>
using namespace std;

class Date
{
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month)
	{
		//assert(month > 0 && month < 13);
		static int monthDayArray[13] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		// 判断是否为闰年
		if (month == 2 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)))
		{
			return 29;
		}
		else
		{
			return monthDayArray[month];
		}
	}

	// 全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	// 拷贝构造函数
	// d2(d1)
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	// 赋值运算符重载
	// d2 = d3 -> d2.operator=(&d2, d3)
	Date& operator=(const Date& d);

	// 日期+=天数
	Date& operator+=(int day);

	// 日期+天数
	Date operator+(int day) const;

	// 日期-天数
	Date operator-(int day) const;

	// 日期-=天数
	Date& operator-=(int day);

	// 前置++
	Date& operator++();

	// 后置++
	Date operator++(int);

	// 后置--
	Date operator--(int);

	// 前置--
	Date& operator--();

	// >运算符重载
	bool operator>(const Date& d) const;

	// ==运算符重载
	bool operator==(const Date& d) const;

	// >=运算符重载
	bool operator >= (const Date& d) const;

	// <运算符重载
	bool operator < (const Date& d) const;

	// <=运算符重载
	bool operator <= (const Date& d) const;

	// !=运算符重载
	bool operator != (const Date& d) const;

	// 日期-日期 返回天数
	int operator-(const Date& d) const;

	void Print() const
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

Date.cpp

#include"Date.h"

Date& Date::operator=(const Date& d)
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	return *this;
}

Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		return *this -= -day;
	}
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
		if (_month == 13)
		{
			_year += 1;
			_month = 1;
		}
	}
	return *this;
}

Date Date::operator+(int day) const
{
	Date tmp(*this);
	tmp += day;
	return tmp;
}

Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += -day;
	}
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

Date Date::operator-(int day) const
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}

Date& Date::operator++()
{
	*this += 1;
	return *this;
}

Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}


Date Date::operator--(int)
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}


Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

bool Date::operator>(const Date& d) const
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year && _month > d._month)
	{
		return true;
	}
	else if (_year == d._year && _month == d._month && _day > d._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Date::operator==(const Date& d) const
{
	return _year == d._year 
		&& _month == d._month 
		&& _day == d._day;
}

bool Date::operator >= (const Date& d) const
{
	return *this > d || *this == d;
}

bool Date::operator < (const Date& d) const
{
	return !(*this >= d);
}

bool Date::operator <= (const Date& d) const
{
	return !(*this > d);
}

bool Date::operator != (const Date& d) const
{
	return !(*this == d);
}

int Date::operator-(const Date& d) const
{
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}
	int n = 0;
	while (min != max)
	{
		++n;
		++min;
	}
	return n * flag;
}

2.取地址运算符重载

        取地址运算符重载分为普通取地址运算符重载和const取地址运算符重载,一般这两个函数编译器自动生成的就可以够我们用了,不需要去显式实现。除非一些很特殊的场景,比如我们不想让别人取到当前类对象的地址,就可以自己实现⼀份,胡乱返回⼀个地址。

 

相关文章:

  • gdal-linux-whl文件安装下载地址
  • 常用的Python库
  • 【时延】空口资源计算
  • 5G核心网实训室搭建方案:轻量化部署与虚拟化实践
  • 京瓷初期的按职能划分的组织
  • k8s系统学习路径
  • Next.js项目MindAI教程 - 第四章:用户认证系统
  • Modbus RTU转DeviceNet构建AB 1756-DNB PLC与电能表的冗余通信链路
  • 【八股文】ArrayList和LinkedList的区别
  • 如何用AI制作PPT,轻松实现高效演示
  • Windows 11 安装Docker Desktop环境
  • 使用 Java 获取咸鱼(微店)商品详情接口(micro.item_get)的完整指南
  • 剑指 Offer II 081. 允许重复选择元素的组合
  • [思考记录]关于AI辅助独立思考
  • 深度解析国产推理大模型DeepSeek:从入门到本地化部署!
  • (六)运算符 条件判断 类型转换
  • ShenNiusModularity项目源码学习(16:ShenNius.Admin.Mvc项目分析-1)
  • 鸿蒙开发:实现AI打字机效果
  • 【“以退为进“、“不得已而为之“与“风险对冲“的协同机制】
  • 大模型训练全流程深度解析
  • 智利观众也喜欢上海的《好东西》
  • 2025年4月份CPI环比由降转涨,核心CPI涨幅稳定
  • 中俄弘扬正确二战史观:缅怀历史,重拾初心,阻止悲剧重演
  • 近4小时会谈、3项联合声明、20多份双边合作文本,中俄元首今年首次面对面会晤成果颇丰
  • 俄罗斯今日将举行“胜利日”阅兵,有何看点?
  • 重庆党政代表团在沪考察,陈吉宁龚正与袁家军胡衡华共商两地深化合作工作