C++————类与对象<二>(类的六个默认成员函数)

发布于:2022-12-10 ⋅ 阅读:(714) ⋅ 点赞:(0)

目录

一、类的六个默认成员函数

 二、构造函数

1、概念

2、特性

三、析构函数

1、概念

2、特性

四、拷贝构造函数

1、概念

2、特性

五、运算符重载

1、运算符重载概念

2、运算符重载特性

3、如何定义和使用

类外定义(需要将成员变量改为共有)

类内定义(需要注意this指针)

4、赋值运算符重载

概念

六、取地址操作符重载 

1、const修饰类的成员函数

 2、取地址运算符重载

七、日期类的实现

1、前言

2、获取某月的天数

3、打印日期

4、用全缺省的构造函数来初始化日期

5、拷贝构造函数

6、赋值运算符重载

7、日期+=天数

8、日期+天数

9、日期-天数

10、日期-=天数

11、++日期

12、日期++

13、日期--

14、<运算符重载

15、==运算符重载

16、<=运算符重载

17、>=运算符重载

18、!=运算符重载

19、日期-日期

完整代码


 

一、类的六个默认成员函数

如果一个类中什么成员都没有,简称为空类。空类中什么都没有吗?并不是的,任何一个类在我们不写的情况下,都会自动生成下面6个默认成员函数。

class Date {};

 二、构造函数

1、概念

日期类:


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

int main()
{
	Date d1;
	d1.Init(2001, 3, 27);
	d1.Print();
	return 0;
}

当我们写像如上这种日期类代码的时候,首先肯定要为其对象的属性进行初始化,不仅日期类要进行初始化,需要初始化的类非常多,在C++中,大佬就设计了一个可以自动初始化的特殊函数,叫构造函数,使我们创建对象时可以自动调用。

构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,保证每个数据成员 都有 一个合适的初始值,并且在对象的生命周期内只调用一次。

2、特性

构造函数是特殊的成员函数,需要注意的是,构造函数的虽然名称叫构造,但是需要注意的是构造函数的主 要任务并不是开空间创建对象,而是初始化对象。 其特征如下:

1. 函数名与类名相同。

2. 无返回值。

3. 对象实例化时编译器自动调用对应的构造函数。

4. 构造函数可以重载

5.如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定义编译器将不再生成。
6.无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认成员函数。


如何自动调用?

 从上述代码可看出,我们定义了一个与类名相同的Date的构造函数,并且在main函数中只是创建了d1对象,但从运行结果可知,已经自动调用了此构造函数。


支持重载又体现在哪里?

 从上述代码我们可以看出来第二个构造函数与第一个形成了函数重载,并且第二个构造函数使用了全缺省。

注意:当对象没有参数时我们需要调用不能在后边加(),构造函数之所以被称为特殊的构造函数就是因为她与普通函数的调用是不同的,如果我们在对象后面加了()会被认为是函数的声明。

错误示范:


无参函数与全缺省同时存在时

虽然我们的构造函数是可以支持函数重载和缺省函数的,但是如果无参的普通函数和全缺省的函数同时存在时,会产生二义性,也就是编译器并不知道应该调用哪个函数。

因此,他们在语法上虽然可以同时存在,但是却不能在一起使用,所以当我们需要写构造函数时还是推荐直接写全缺省的构造函数,无论是参数个数还是初始化都比较灵活。

代码示例如下:

class Date
{
public:
//普通的构造函数
	Date()
	{
		_year = 1;
		_month = 1;
		_day = 1;
	}
//全缺省的构造函数
	Date(int year = 2001, int month = 3, int day = 27)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1;//编译器将出错:由于不知道应该调用哪个构造函数
	d1.Print();
}

默认拷贝构造

没有定义构造函数,对象也可以创建成功,因此此处调用的是编译器生成的默认构造函数。

可是既然系统给我们默认生成了构造函数,为什么对象所对应的属性得到的却是随机值呢,因此我们需要把C++的默认拷贝构造搞明白!

C++内的变量分为两种:

1、内置类型/基本类型:int/char/double/指针等等

2、自定义类型:class、struct去定义的类型对象

C++默认生成的构造函数对于内置类型成员变量不做处理,对于自定义类型的成员变量才会处理,这也就能很好的说明了为什么刚才没有对年月日进行处理(初始化),因为它们是内置类型(int类型的变量)。

那如何验证C++对自定义类型的成员变量处理而对内置类型不处理呢?


class A
{
public:
	A()
	{
		cout << "A()" << endl;
		_a = 0;
	}
private:
	int _a;
};


class Date
{
public:
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
	A _aa;
};
int main()
{
	Date d;
	d.Print();
}

通过调试我们发现:

 Date中的内置类型并没有进行处理,由_aa被初始化可以发现自定义类型A进行了处理。

三、析构函数

1、概念

前面通过构造函数的学习,我们知道一个对象时怎么来的,那一个对象又是怎么没呢的?

析构函数:与构造函数功能相反,析构函数不是完成对象的销毁,局部对象销毁工作是由编译器完成的。而对象在销毁时会自动调用析构函数,完成类的一些资源清理工作。

2、特性

析构函数是特殊的成员函数。 其特征如下:

1. 析构函数名是在类名前加上字符 ~。

2. 无参数无返回值。

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

4. 对象生命周期结束时,C++编译系统系统自动调用析构函数。

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;
	}
	~Date()
	{
		cout << "~Date()" << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1;
	return 0;
}

我们先用构造函数 初始化了d1,出了作用域之后又用析构函数进行了析构,所以输出了析构函数中的~Date()。

由于日期类并没有在堆上申请空间,而是出了函数栈帧就自动销毁了,因此日期类并不需要析构函数出马,像malloc、new出来的空间则需要析构函数来处理。


析构的顺序

int main()    
{
    Stack st1;
    Stack st2;
}

 构造的顺序肯定是st1先构造,st2后构造。

那么析构的顺序应该是怎样的呢?

有序st1和st2是在栈上开的空间,跟我们在数据结构中学习的栈是一样的,,st1先压栈,st2后压栈,所以当我们需要资源清理的时候就必须先清理st2后清理st1。


默认析构函数

class Stack
{
public:
	//构造函数
	Stack(int capacity = 10)
	{
		_a = (int*)malloc(sizeof(int) * capacity);
		assert(_a);
		_top = 0;
		_capacity = capacity;
	}
	//析构函数
	~Stack()
	{
		cout << "~Stack():" << this << endl;
		free(_a);
		_a = nullptr;
		_top = _capacity = 0;
	}
private:
	int* _a;
	int _top;
	int _capacity;
 
};
class MyQueue
{
public:
	//默认生成的构造函数可以用
	//默认生成的析构函数也可以用
	void push(int x)
	{}
 
	int pop()
	{}
private:
	Stack _S1;
	Stack _s2;
};
int main()
{
	MyQueue q;
}

由于有两个自定义成员所以需要析构两次:

四、拷贝构造函数

1、概念

那在创建对象时,可否创建一个与一个对象一模一样的新对象呢?

构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象 创建新对象时由编译器自动调用。

2、特性

拷贝构造函数也是特殊的成员函数,其特征如下:

1.拷贝构造函数是构造函数的一个重载形式。
2.拷贝构造函数的参数只有一个且必须使用引用传参使用传值方式会引发无穷递归调用
3.若未显示定义,系统生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝。


为什么传值调用会引发无穷递归调用呢?


Date(Date d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}


Date d2(d1);

就像上述代码一样,如果想完成d1拷贝给d2,第一步肯定是先把d1拷贝给d,那么此时这个过程也是一个拷贝构造就需要再调用Date拷贝构造函数,由于每次传参都需要调用拷贝构造函数,拷贝构造又要传参,就会产生无穷递归调用。

如何解决?

其实我们在c语言就学过怎么解决,我们这里可以不传值传参,而是进行传引用传参。

代码如下:


Date(Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}


Date d2(d1);

这样的话d是d1的别名,将d拷贝给d2也就是将d1拷贝给d2,而且不需要经过拷贝构造。


默认生成的拷贝构造函数

class Date
{
public:
	Date(int year = 2022, int month = 9, int day = 22)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Pint()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1(2001, 3, 27);
	Date d2(d1);
	d2.Pint();
	return 0;
}

为什么我这里没有写拷贝构造函数,它也会自动完成拷贝构造呢?由此我们要深思,拷贝构造与构造和析构是不一样的,构造和析构都是针对自定义类型才会处理而内置类型不会处理,而默认拷贝构造针对内置类型的成员会完成值拷贝,浅拷贝也就是像把d1的内置类型成员按字节拷贝给d2。

既然如此,那么我们岂不是不用自己实现拷贝构造函数就行了,实际上是不行的,由上述解释可得,默认拷贝构造函数只能完成浅拷贝,那么什么时候不能使用默认的呢?

class Stack
{
public:
	//构造函数
	Stack(int capacity = 10)
	{
		_a = (int*)malloc(sizeof(int) * capacity);
		assert(_a);
		_top = 0;
		_capacity = capacity;
	}
	//析构函数
	~Stack()
	{
		cout << "~Stack():" << this << endl;
		free(_a);
		_a = nullptr;
		_top = _capacity = 0;
	}
private:
	int* _a;
	int _top;
	int _capacity;
};
int main()
{
	Stack st1(10);
	Stack st2(st1);
}

 可见程序崩溃了!

我们通过监视可以发现:

st1的地址和st2的地址是一样的,可见如果析构的话会先析构st2,但是再析构st1时就会出错,因为此时st1已经被析构了,也就是同一块空间被两个栈空间指向了,如图所示:

 综上所述,日期类的话我们可以使用默认生成的拷贝构造函数,因为浅拷贝对于日期类并没有影响,如果有malloc开的堆空间的话就不能使用浅拷贝了。

五、运算符重载

1、运算符重载概念

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类 型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。

函数名字为:关键字operator后面接需要重载的运算符符号。

函数原型:返回值类型 operator操作符(参数列表)。

2、运算符重载特性

1. 不能通过连接其他符号来创建新的操作符:比如operator@

2. 重载操作符必须有一个类类型或者枚举类型的操作数

3. 用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不 能改变其含义

4. 作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的 操作符有一个默认的形参this,限定为第一个形参

5. .* 、:: 、sizeof 、?: 、. 注意以上5个运算符不能重载。这个经常在笔试选择题中出现。

3、如何定义和使用

类外定义(需要将成员变量改为共有)


class Data
{
public:
	Data(int year = 0, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
 
//private:
	int _year;
	int _month;
	int _day;
};
//函数名 operator操作符
//返回类型:看操作符运算后返回值是什么
//参数:操作符有几个操作数,他就有几个参数
bool operator>(const Data& d1, const Data& d2)
{
	if (d1._year > d2._year)
	{
		return true;
	}
	else if (d1._year == d2._year && d1._month > d2._month)
	{
		return true;
	}
	else if (d1._year == d2._year && d1._month == d2._month && d1._day > d2._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}


int main()
{
	Data d1(2022, 1, 16);
	Data d2(2022, 1, 31);

	//默认情况下,C++是不支持自定义类型对象使用运算符
	d1 > d2;//编译器看到这个就会将其转换为operator>(d1,d2),就像函数调用一样
	cout << (d1 > d2) << endl;
	cout << operator>(d1, d2) << endl;
	return 0;
}

类内定义(需要注意this指针)

class Date
{
public:
	Date(int year = 0, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//bool operator>(const Date& d1, const Date& d2)
	//按照这样去写,是错误的,操作数过多,因为隐含了一个this指针
	//d1.operator > (d2); bool operator>(const Date* this, const Date& d2)  
	//d1就传给了this,d2就传给了d2
	bool operator>(const Date& d2)
	{
		if (_year > d2._year)
		{
			return true;
		}
		else if (_year == d2._year && _month > d2._month)
		{
			return true;
		}
		else if (_year == d2._year && _month == d2._month && _day > d2._day)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
private:
	int _year;
	int _month;
	int _day;
};
 
 
int main()
{
	Date d1(2022, 1, 16);
	Date d2(2022, 1, 31);
	d1 > d2;//先去类看有没有这个运算符,没有去全局找  运算符重载一般重载为成员函数,为了解决访问私有变量的问题
	//d1.operator > (d2);类里面转换为这样
	
	return 0;
}

4、赋值运算符重载

概念

赋值运算符重载实现的是两个自定义类型的对象之间的赋值,它和拷贝构造函数不同的是:

        1.拷贝构造是用一个已存在的对象去初始化一个对象;

        2.赋值运算符重载是两个已存在的对象进行赋值操作;

class Date
{
public:
    //构造函数
	Date(int year = 0, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
    //赋值运算符重载
	//d1 = d3;
	Date operator=(const Date& d)
    //&为了防止传值传参调用拷贝构造
    //const防止传入的值发生改变
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
 
private:
	int _year;
	int _month;
	int _day;
};
 
 
int main()
{
	Date d1(2022, 1, 16);
	Date d2(2022, 1, 31);
	Date d3(2022, 2, 26);
 
	//一个已经存在的对象拷贝初始化一个马上创建实例化的对象
	Date d4(d1);//拷贝构造
	Date d5 = d1;
 
	//两个已经存在的对象,之间进行赋值拷贝
	d2 = d1 = d3;
	//d1.operator=(d3);
	//d2.operator=(d1.operator=(d3));
 
	return 0;
}

由于赋值运算符重载实在类内定义的,参数中默认有this指针的,所以只需要给定一个参数就行了。

赋值重载和拷贝构造一样,我们不写,它会对内置类型完成值拷贝,而像栈这样的就不能不写了,因为我们要写一个深拷贝的赋值重载才可以,理由和拷贝构造类似。 具体实现等真正谈到深拷贝再来。

注意:

void TestDate2()
{
	Date d1(2022, 5, 18);
    Date d2(2022, 5, 20);
	Date d3 = d1; //等价于 Date d3(d1); 是拷贝构造
    d2 = d1; //两个已经存在的对象才是赋值
}

Date d3 = d1 是拷贝构造 而不是赋值,拿一个已知的对象去初始化另一个对象才叫赋值。

六、取地址操作符重载 

1、const修饰类的成员函数

将const修饰的类成员函数称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this 指针,表明在该成员函数中不能对类的任何成员进行修改。

 2、取地址运算符重载

​​​​​​​这两个默认成员函数一般不用重新定义 ,编译器默认会生成。

class Date
{
public :
 Date* operator&()
 {
 return this ;
 }

 const Date* operator&()const
 {
 return this ;
 }
private :
 int _year ; // 年
 int _month ; // 月
 int _day ; // 日
};

 这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比 如想让别人获取到指定的内容!

七、日期类的实现

1、前言

当我们需要知道100天后是几月几号时,或者从出生到现在一共有多少天时,或者再有多少天就要过生日了,我们可以在日历上进行查询或者有些app可以实现这些操作。那么今天我们就用刚学过的一些特殊的成员函数来实现这些吧!

2、获取某月的天数

首先我们知道,从一月到十二月,每个月的天数并不是固定的,甚至到闰年的时候二月比平常还多了一天,所以我们可以先实现一个函数来通过输入指定的年和月来得到当前的天数。

int Date::GetMonthDay(int year, int month)
{
	static int monthDayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
    //将非闰年的12个月对应的天放入一个数组里面方便索引
	int day=monthDayArray[month]; 
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
    //判断如果是二月份并且满足闰年的判断条件的时候天数+1
	{
		day += 1;
	}
	return day;
}

3、打印日期

//打印日期
void Date::Print()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}

4、用全缺省的构造函数来初始化日期

// 全缺省的构造函数
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (!(year >= 0
		&& (month > 0 && month < 13)
		&& (day > 0 && day <= GetMonthDay(year, month))))
    //判断是否合法
	{
		cout << "非法日期->";
		Print();
	}
}

5、拷贝构造函数

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

6、赋值运算符重载

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

7、日期+=天数

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

8、日期+天数

Date Date::operator+(int day)
{
	Date ret(*this);//拷贝构造,拿d1去初始化ret
	ret += day;
	return ret;
}

9、日期-天数

// 日期-天数
Date Date::operator-(int day)
{
	Date ret(*this);
	ret -= day;
	return ret;
}

10、日期-=天数

Date& Date::operator-=(int day)
{
	//如果减去的天数是负数,要单独处理,直接调用+=运算符重载
	if (day < 0)
	{
		return *this += -day;
	}
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			_month = 12;
			--_year;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

11、++日期

// ++d1
Date& Date::operator++()
{
	*this += 1;

	return *this;
}

12、日期++

//d1++;后置为了跟前置++,进行区分
//增加一下参数占位,跟前置++,构成函数重载
Date Date::operator++(int a)
{
	Date ret(*this);
	*this += 1;

	return ret;
}

13、日期--

//前置--
Date& Date::operator--() //无参的为前置
{
	*this -= 1; //直接复用-=
	return *this;
}

14、<运算符重载


// < 运算符重载
bool Date::operator<(const Date& d)
{
	if (_year < d._year ||
		_year == d._year && _month < d._month ||
		_year == d._year && _month == d._month && _day < d._day)
		return true;
	else
		return false;
}

15、==运算符重载

// == 运算符重载
bool Date::operator==(const Date& d)
{
	return _year == d._year &&
		_month == d._month &&
		_day == d._day;
}

16、<=运算符重载

// <=运算符重载
bool Date::operator>(const Date& d)
{
	return (*this < d) || (*this == d);
}

17、>=运算符重载

// >=运算符重载
bool Date::operator>=(const Date& d)
{
	return !(*this < d);
}

18、!=运算符重载

// !=运算符重载
bool Date::operator != (const Date & d)
{
	return !(*this == d);
}

19、日期-日期

//日期 - 日期
int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
    int flag=1;//用来判断正负
	if (*this<d)
	{
		min = *this;
		max = d;
        flag=-1;//由于第一个数小所以确保结果为负
	} //确保max是大的,min是小的
	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}//算出min和max之间绝对值差距
	return n*flag; //如果d1大,结果为正,d2大结果为负
}

完整代码

Date.h代码如下

#pragma once
#include<iostream>
using namespace std;
class Date
{
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month);

	//打印日期
	void Print();

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


	// 拷贝构造函数
    // d2(d1)
	Date(const Date& d);

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

	// 析构函数
	//~Date();

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


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


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


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


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

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

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

	// <运算符重载
	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);

private:
	int _year;
	int _month;
	int _day;
};

Date.cpp代码如下:

#include"Date.h"

// 获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
	static int monthDayArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	int day=monthDayArray[month]; 
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		day += 1;
	}
	return day;
}

//打印日期
void Date::Print()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}


// 全缺省的构造函数
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (!(year >= 0
		&& (month > 0 && month < 13)
		&& (day > 0 && day <= GetMonthDay(year, month))))
	{
		cout << "非法日期->";
		Print();
	}
}

// 拷贝构造函数
// d2(d1)
//const防止原数据改变
//&是为了防止传值方式引发无穷递归调用
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}


// 赋值运算符重载
	// d2 = d3 -> d2.operator=(&d2, d3)
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)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
		if (_month == 13)
		{
			_month = 1;
			_year++;
		}
	}
	return *this;
}


// 日期+天数
Date Date::operator+(int day)
{
	Date ret(*this);//拷贝构造,拿d1去初始化ret
	ret += day;
	return ret;
}

// 日期-天数
Date Date::operator-(int day)
{
	Date ret(*this);
	ret -= day;
	return ret;
}

// 日期-=天数

Date& Date::operator-=(int day)
{
	//如果减去的天数是负数,要单独处理,直接调用+=运算符重载
	if (day < 0)
	{
		return *this += -day;
	}
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			_month = 12;
			--_year;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

// ++d1
Date& Date::operator++()
{
	*this += 1;

	return *this;
}

//d1++;后置为了跟前置++,进行区分
//增加一下参数占位,跟前置++,构成函数重载
Date Date::operator++(int a)
{
	Date ret(*this);
	*this += 1;

	return ret;
}

//前置--
Date& Date::operator--() //无参的为前置
{
	*this -= 1; //直接复用-=
	return *this;
}

//后置--
Date Date::operator--(int a) //有参数的为后置
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}

// < 运算符重载
bool Date::operator<(const Date& d)
{
	if (_year < d._year ||
		_year == d._year && _month < d._month ||
		_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);
}

//日期 - 日期
int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
    int flag=1;//用来判断正负
	if (*this<d)
	{
		min = *this;
		max = d;
        flag=-1;//由于第一个数小所以确保结果为负
	} //确保max是大的,min是小的
	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}//算出min和max之间绝对值差距
	return n*flag; //如果d1大,结果为正,d2大结果为负
}

test.cpp(测试代码,我只测试了一个《日期-日期》)代码如下:

#include"Date.h"
void TestDate2(Date d1,Date d2)
{
	int a = d1.operator-(d2);
	cout << a << endl;
}
int main()
{
	Date d1(2001, 3, 27);
	Date d2(2022, 9, 21);
	TestDate2(d1,d2);
	return 0;
}

本文含有隐藏内容,请 开通VIP 后查看