C++——类和对象(中)

一、类的默认成员函数

默认成员函数就是⽤⼾没有显式实现,编译器会⾃动⽣成的成员函数称为默认成员函数。

⼀个类,我们不写的情况下编译器会默认⽣成以下6个默认成员函数,需要注意的是这6个中最重要的是前4个,最后两个取地址重载不重要,只需要了解

①初始化功能的构造函数
②清理功能的析构函数
③使用同类对象初始化创建对象的拷贝构造
④把一个对象赋值给另一个对象的赋值重载
⑤对普通对象取地址重载
⑥对const对象取地址重载

默认成员函数很重要,也⽐较复杂,我们要从两个⽅⾯去学习:

• 第⼀:我们不写时,编译器默认⽣成的函数⾏为是什么,是否满⾜我们的需求。
• 第⼆:编译器默认⽣成的函数不满⾜我们的需求,我们需要⾃⼰实现,那么如何⾃⼰实现?

二、构造函数

构造函数是特殊的成员函数,需要注意的是,构造函数虽然名称叫构造, 但是构造函数的主要任务并不是开空间创建对象(我们常使⽤的局部对象是栈帧创建时,空间就开好了),⽽是对象实例化时初始化对象。

构造函数的特点:

  1. 函数名与类名相同。
  2. ⽆返回值。(返回值啥都不需要给,也不需要写void,不要纠结,C++规定如此)
  3. 对象实例化时系统会⾃动调⽤对应的构造函数。
  4. 构造函数可以重载。
  5. 如果类中没有显式定义构造函数,则C++编译器会⾃动⽣成⼀个⽆参的默认构造函数,⼀旦⽤⼾显式定义编译器将不再⽣成。
  6. ⽆参构造函数、全缺省构造函数、我们不写构造时编译器默认⽣成的构造函数,都叫做默认构造函数。但是这三个函数有且只有⼀个存在,不能同时存在。 ⽆参构造函数和全缺省构造函数虽然构成函数重载,但是调⽤时会存在歧义。要注意默认构造函数并非是编译器默认⽣成那个叫默认构造,实际上⽆参构造函数、全缺省构造函数也是默认构造,总结⼀下就是不传实参就可以调⽤的构造就叫默认构造。
  7. 我们不写,编译器默认⽣成的构造,对内置类型成员变量的初始化没有要求,也就是说是是否初始化是不确定的,看编译器。对于⾃定义类型成员变量,要求调⽤这个成员变量的默认构造函数初始化。如果这个成员变量,没有默认构造函数,那么就会报错,我们要初始化这个成员变量,需要⽤初始化列表才能解决,后面再提到初始化列表。

说明:C++把类型分成内置类型(基本类型)和⾃定义类型。内置类型就是语⾔提供的原⽣数据类型。
如:int/char/double/指针等,⾃定义类型就是我们使⽤class/struct等关键字⾃⼰定义的类型。

对于内置类型:

#include <iostream>
using namespace std;

class Date
{
public:
	//无参构造函数
	Date()
	{
		_year = 2000;
		_month = 6;
		_day = 1;
	}
	//带参构造函数
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	//全缺省构造函数
	//与无参构造函数矛盾,这两个只能用一个
	/*Date(int year = 2024, int month = 7, int day = 1)
	{
		_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();
	Date d2(2024, 7, 14);
	d2.print();
	return 0;
}

在这里插入图片描述
对于自定义类型:

#include<iostream>
using namespace std;
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;
	} 
private:
	STDataType * _a;
	size_t _capacity;
	size_t _top;
};
// 两个Stack实现队列
class MyQueue
{
public:
	//编译器默认生成MyQueue的构造函数调用了Stack的构造,完成了两个成员的初始化
private:
	Stack pushst;
	Stack popst;
};

int main()
{
	MyQueue mq;
	return 0;
}

在这里插入图片描述

三、析构函数

析构函数与构造函数功能相反,析构函数不是完成对对象本⾝的销毁,⽐如局部对象是存在栈帧的,函数结束栈帧销毁,他就释放了,不需要我们管,C++规定对象在销毁时会⾃动调⽤析构函数,完成对象中资源的清理释放⼯作。
析构函数的功能类⽐我们之前Stack实现的Destroy功能,⽽像Date没有Destroy,其实就是没有资源需要释放,所以严格说Date是不需要析构函数的。

析构函数的特点:

  1. 析构函数名是在类名前加上字符~。
  2. ⽆参数⽆返回值。(这⾥跟构造类似,也不需要加void)
  3. ⼀个类只能有⼀个析构函数。若未显式定义,系统会⾃动⽣成默认的析构函数。
  4. 对象⽣命周期结束时,系统会⾃动调⽤析构函数。
  5. 跟构造函数类似,我们不写编译器⾃动⽣成的析构函数对内置类型成员不做处理,⾃定类型成员会调⽤他的析构函数。
  6. 还需要注意的是我们显⽰写析构函数,对于⾃定义类型成员也会调⽤他的析构,也就是说⾃定义类型成员⽆论什么情况都会⾃动调⽤析构函数。
  7. 如果类中没有申请资源时,析构函数可以不写,直接使⽤编译器⽣成的默认析构函数,如Date;如果默认⽣成的析构就可以⽤,也就不需要显⽰写析构,如MyQueue;但是有资源申请时,⼀定要⾃⼰写析构,否则会造成资源泄漏,如Stack。
  8. ⼀个局部域的多个对象,C++规定后定义的先析构。
#include<iostream>
using namespace std;
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()
	{
		cout << "~Stack()" << endl;
		free(_a);
		_a = nullptr;
		_top = _capacity = 0;
	}
private:
	STDataType* _a;
	size_t _capacity;
	size_t _top;
};
// 两个Stack实现队列
class MyQueue
{
public:
	//编译器默认生成MyQueue的析构函数调用了Stack的析构,释放的Stack内部的资源
	// 显示写析构,也会自动调用Stack的析构
	~MyQueue()
	{
		cout << "~MyQueue()" << endl;
	}
private:
	Stack pushst;
	Stack popst;
};

int main()
{
	MyQueue mq;
	return 0;
}

在这里插入图片描述

四、拷⻉构造函数

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

拷⻉构造的特点:

  1. 拷⻉构造函数是构造函数的⼀个重载。
  2. 拷⻉构造函数的第一个参数必须是类类型对象的引⽤,使⽤传值⽅式编译器直接报错,因为语法逻辑上会引发⽆穷递归调⽤。
    在这里插入图片描述
  3. C++规定⾃定义类型对象进⾏拷⻉⾏为必须调⽤拷⻉构造,所以这⾥⾃定义类型传值传参和传值返回都会调⽤拷⻉构造完成。
  4. 若未显式定义拷⻉构造,编译器会⽣成⾃动⽣成拷⻉构造函数。⾃动⽣成的拷⻉构造对内置类型成员变量会完成值拷⻉/浅拷⻉(⼀个字节⼀个字节的拷⻉),对⾃定义类型成员变量会调⽤他的拷⻉构造。
  5. 像Date这样的类成员变量全是内置类型且没有指向什么资源,编译器⾃动⽣成的拷⻉构造就可以完成需要的拷⻉,所以不需要我们显⽰实现拷⻉构造。像Stack这样的类,虽然也都是内置类型,但是_a指向了资源,编译器⾃动⽣成的拷⻉构造完成的值拷⻉/浅拷⻉不符合我们的需求,所以需要
    我们⾃⼰实现深拷⻉(对指向的资源也进⾏拷⻉)。像MyQueue这样的类型内部主要是⾃定义类型
    Stack成员,编译器⾃动⽣成的拷⻉构造会调⽤Stack的拷⻉构造,也不需要我们显⽰实现 MyQueue的拷⻉构造。这⾥还有⼀个⼩技巧,如果⼀个类显⽰实现了析构并释放资源,那么他就 需要显⽰写拷⻉构造,否则就不需要。
  6. 传值返回会产⽣⼀个临时对象调⽤拷⻉构造,传值引⽤返回,返回的是返回对象的别名(引⽤),没 有产⽣拷⻉。但是如果返回对象是⼀个当前函数局部域的局部对象,函数结束就销毁了,那么使⽤ 引⽤返回是有问题的,这时的引⽤相当于⼀个野引⽤,类似⼀个野指针⼀样。传引⽤返回可以减少拷⻉,但是⼀定要确保返回对象,在当前函数结束后还在,才能⽤引⽤返回。
#include <iostream>
using namespace std;

class Date
{
public:
	//无参构造函数
	Date()
	{
		_year = 2000;
		_month = 6;
		_day = 1;
	}
	//带参构造函数
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	//拷贝构造函数 - 传值
	//error C2652: “Date”: 非法的复制构造函数: 第一个参数不应是“Date”

	/*Date(Date d)
	{
		
	}*/

	//拷贝构造函数 - 传引用
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	//这个不是拷贝构造函数,只是一个构造函数
	Date(Date* d)
	{
		_year = d->_year;
		_month = d->_month;
		_day = d->_day;
	}

	void print()
	{
		cout << _year << "年" << _month << "月" << _day << "日" << endl;
	}

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

Date& run()
{
	Date  tmp(2024, 8, 1);
	return tmp;
}


int main()
{
	Date d1(2024, 7, 14);

	//这里可以完成拷⻉,但是不是拷⻉构造,只是⼀个普通的构造
	Date dk(&d1);
	dk.print();

	//这样写才是拷⻉构造,通过同类型的对象初始化构造,而不是指针
	Date d2(d1);
	d2.print();

	//也可以这样写,这里也是拷⻉构造
	Date d3 = d1;
	d3.print();

	// run返回了一个局部对象tmp的引用作为返回值
	// run函数结束,tmp对象就销毁了,相当于了一个野引用
	Date ret = run();
	ret.print();

	return 0;
}

在这里插入图片描述

#include<iostream>
using namespace std;
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;
	}
	void Push(STDataType x)
	{
		if (_top == _capacity)
		{
			int newcapacity = _capacity * 2;
			STDataType* tmp = (STDataType*)realloc(_a, newcapacity *
				sizeof(STDataType));
			if (tmp == NULL)
			{
				perror("realloc fail");
				return;
			} 
			_a = tmp;
			_capacity = newcapacity;
		} 
		_a[_top++] = x;
	}
	~Stack()
	{
		free(_a);
		_a = nullptr;
		_capacity = 0;
		_top = 0;
		cout << "~Stack()" << endl;
	}
	Stack(const Stack& st)
	{
		cout << "Stack(const Stack& st)" << endl;

		// 需要对_a指向资源创建同样大的资源再拷贝值
		_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;
	}
private:
	STDataType* _a;
	size_t _capacity;
	size_t _top;
};

// 两个Stack实现队列
class MyQueue
{
public:
private:
	Stack pushst;
	Stack popst;
};
int main()
{
	Stack st1;
	st1.Push(1);
	st1.Push(2);
	// Stack不显示实现拷⻉构造,用自动生成的拷⻉构造完成浅拷⻉
	// 会导致st1和st2里面的_a指针指向同一块资源,析构时会析构两次,程序崩溃
	Stack st2 = st1;
	MyQueue mq1;
	// MyQueue自动生成的拷⻉构造,会自动调用Stack拷⻉构造完成pushst/popst
	// 的拷⻉,只要Stack拷⻉构造自己实现了深拷⻉,他就没问题
	MyQueue mq2 = mq1;
	return 0;
}

在这里插入图片描述

五、赋值运算符重载

1. 运算符重载

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

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

重载运算符函数的参数个数和该运算符作⽤的运算对象数量⼀样多。⼀元运算符有⼀个参数,⼆元运算符有两个参数,⼆元运算符的左侧运算对象传给第⼀个参数,右侧运算对象传给第⼆个参数。

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

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

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

注意以下5个运算符不能重载。
在这里插入图片描述

• 重载操作符⾄少有⼀个类类型参数,不能通过运算符重载改变内置类型对象的含义,如: int operator+(int x, int y)

⼀个类需要重载哪些运算符,是看哪些运算符重载后有意义,⽐如Date类重载operator-就有意义,但是重载operator+就没有意义。

• 重载++运算符时,有前置++和后置++,运算符重载函数名都是operator++,⽆法很好的区分。
C++规定,后置++重载时,增加⼀个int形参,跟前置++构成函数重载,⽅便区分。

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

#include <iostream>
using namespace std;

class Date
{
public:
	//无参构造函数
	Date()
	{
		_year = 2000;
		_month = 6;
		_day = 1;
	}
	//带参构造函数
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}

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

	//++d
	Date& operator++()
	{
		cout << "前置++" << endl;
		//...
		return *this;
	}

	//d++
	Date operator++(int)
	{
		cout << "后置++" << endl;
		Date tmp = *this;
		//...
		return tmp;
	}

	void print()
	{
		cout << _year << "年" << _month << "月" << _day << "日" << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};


int main()
{
	Date d1;
	d1.print();
	Date d2(2024, 7, 14);
	d2.print();
	//运算符重载函数可以显示调用
	d1.operator==(d2);
	//编译器会转换成 d1.operator==(d2);
	d1 == d2;
	cout << (d1 == d2) << endl;
	//编译器会转换成 d1.operator++();
	++d1;
	//编译器会转换成 d2.operator++(0);
	d2++;
	return 0;
}

在这里插入图片描述

2. 赋值运算符重载

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

赋值运算符重载的特点:

  1. 赋值运算符重载是⼀个运算符重载,规定必须重载为成员函数。赋值运算重载的参数建议写成const当前类类型引⽤,否则会传值传参会有拷⻉。
  2. 有返回值,且建议写成当前类类型引⽤,引⽤返回可以提⾼效率,有返回值⽬的是为了⽀持连续赋值场景。
  3. 没有显式实现时,编译器会⾃动⽣成⼀个默认赋值运算符重载,默认赋值运算符重载⾏为跟默认拷贝构造函数类似,对内置类型成员变量会完成值拷⻉/浅拷⻉(⼀个字节⼀个字节的拷⻉),对⾃定义类型成员变量会调⽤他的赋值重载。
  4. 像Date这样的类成员变量全是内置类型且没有指向什么资源,编译器⾃动⽣成的赋值运算符重载就可以完成需要的拷⻉,所以不需要我们显⽰实现赋值运算符重载。像Stack这样的类,虽然也都是内置类型,但是_a指向了资源,编译器⾃动⽣成的赋值运算符重载完成的值拷⻉/浅拷⻉不符合我们的需求,所以需要我们⾃⼰实现深拷⻉(对指向的资源也进⾏拷⻉)。像MyQueue这样的类型内部主要是⾃定义类型Stack成员,编译器⾃动⽣成的赋值运算符重载会调⽤Stack的赋值运算符重载,也不需要我们显⽰实现MyQueue的赋值运算符重载。这⾥还有⼀个⼩技巧,如果⼀个类显⽰实现了析构并释放资源,那么他就需要显⽰写赋值运算符重载,否则就不需要。
#include <iostream>
using namespace std;

class Date
{
public:
	//无参构造函数
	Date()
	{
		_year = 2000;
		_month = 6;
		_day = 1;
	}
	//带参构造函数
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	//拷贝构造
	Date(Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	//赋值运算符重载
	Date& operator=(Date& d)
	{
		// 不要检查自己给自己赋值的情况
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		} 
		// d1 = d2表达式的返回对象应该为d1,也就是 * this
		return *this;
	}

	void print()
	{
		cout << _year << "年" << _month << "月" << _day << "日" << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};


int main()
{
	// 赋值重载完成两个已经存在的对象直接的拷⻉赋值
	// 拷⻉构造用于一个对象拷⻉初始化给另一个要创建的对象
	Date d1(2024, 7, 14);
	//拷贝构造
	Date d2(d1);
	//拷贝构造
	Date d3 = d2;
	Date d4(2025, 5, 5);
	//赋值重载
	d3 = d4;
	d3.print();
	return 0;
}

在这里插入图片描述

六、取地址运算符重载

const成员函数

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

const实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进⾏修改。
const 修饰Date类的Print成员函数,Print隐含的this指针由 Date* const this 变为 const Date* const this

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	} 
	// void Print(const Date* const this) const
	void Print() const
	{
		cout << _year << "年" << _month << "月" << _day << "日" << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	// 这里非const对象也可以调用const成员函数是⼀种权限的缩小
	Date d1(2024, 7, 14);
	d1.Print();
	const Date d2(2024, 8, 1);
	d2.Print();
	return 0;
}

在这里插入图片描述

取地址运算符重载

取地址运算符重载分为普通取地址运算符重载const取地址运算符重载,⼀般这两个函数编译器⾃动⽣成的就可以够我们⽤了,不需要去显⽰实现。
除⾮⼀些很特殊的场景,⽐如我们不想让别⼈取到当前类对象的地址,就可以⾃⼰实现⼀份,胡乱返回⼀个地址。

#include <iostream>
using namespace std;

class Date
{
public :

	Date()
	{
		_year = 2024;
		_month = 7;
		_day = 14;
	}

	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	Date* operator&()
	{
		//return this;
		//return nullptr;
		return (Date* )0x1256EF7;

	}
	const Date* operator&()const
	{
		//return this;
		//return nullptr;
		return (Date*)0x1256EA9;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1;
	cout << &d1 << endl;
	const Date d2(2024, 8, 1);
	cout << &d2 << endl;
	return 0;
}

在这里插入图片描述

七、应用:⽇期类实现

Date.h

#pragma once

#include <iostream>
using namespace std;

//日期类
class Date
{
public:
	Date(int year = 2000, int month = 1, int day = 1);

	//拷贝构造
	Date(const Date& d);
	//赋值运算符重载构造
	Date& operator=(const Date& d);

	~Date()
	{
		_year = 0;
		_month = 0;
		_day = 0;
	}

	void print() const;
	bool CheckDate() const;
	static int GetMonthDay(int year,int month)
	{
		int arr[13] = { 0,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 arr[month] + 1;
		}
		return arr[month];
	}
	Date operator+(int day) const;
	Date& operator+=(int day);

	Date operator-(int day) const;
	Date& operator-=(int day);

	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;

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

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

	//日期相减  d1 - d2
	int operator-(const Date& d) const;

	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);

	//取地址运算符重载
	Date* operator&()
	{
		//return this;
		//return nullptr;
		return (Date*)0x11451EF6;
	}
	const Date* operator&() const
	{
		//return this;
		//return nullptr;
		return (Date*)0x1145EE6;

	}

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

ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);

Date.cpp

#include "Date.h"

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (!CheckDate())
	{
		cout << "日期错误:";
		print();
	}
}

//拷贝构造
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

//赋值运算符重载构造
Date& Date::operator=(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
	return *this;
}

void Date::print() const
{
	cout << _year << " " << _month << " " << _day << endl;
}

bool Date::CheckDate() const
{
	if (_month < 1 || _month > 12 || _day <= 0 || _day > GetMonthDay(_year, _month))
	{
		return false;
	}
	return true;
}

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

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)
		{
			_month = 1;
			_year++;
		}
	}
	return *this;
}

Date Date::operator-(int day) const
{
	Date tmp = *this;
	tmp.operator-=(day);
	return tmp;
}
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;
}

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

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 _year == d._year && _month == d._month && _day == d._day;
}

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

//++d 前置++
Date& Date::operator++()
{
	return *this += 1;
}

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

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

ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}

istream& operator>>(istream& in, Date& d)
{
	while (1)
	{
		cout << "输入年 月 日" << endl;
		in >> d._year >> d._month >> d._day;
		if (!d.CheckDate())
		{
			cout << "日期错误:";
			d.print();
		}
		else
		{
			break;
		}
	}
	return in;
}

test.cpp

#include "Date.h"

void test01()
{
	Date d1;
	Date d2(2024, 7, 12);
	d1.print();
	d2.print();
	d2 += 30000;
	d2.print();
	Date d3 = d1 + 10000;
	d3.print();
}

void test02()
{
	Date d1;
	Date d2(2024, 7, 13);
	d1.print();
	d2.print();
	d2 -= 30000;
	d2.print();
	//d2 = d1 - 100;
	//d2.print();
}

void test03()
{
	Date d1(2024, 7, 13);
	Date d2(2024, 7, 19);
	
	cout << (d1 < d2) << endl;
	cout << (d1 <= d2) << endl;
	cout << (d1 > d2) << endl;
	cout << (d1 >= d2) << endl;
	cout << (d1 == d2) << endl;
	cout << (d1 != d2) << endl;
}

void test04()
{
	Date d1(2024, 7, 13);
	Date d2(2024, 7, 13);
	Date tmp1 = d1++;
	tmp1.print();
	d1.print();
	Date tmp2 = ++d2;
	tmp2.print();
	d2.print();
}

void test05()
{
	Date d1(2024, 7, 13);
	Date d2(2000, 7, 20);
	cout << d1 - d2 << endl;
	Date d3(2024, 6, 31);
}

void test06()
{
	Date d1(2024, 7, 13);
	Date d2(2000, 7, 20);
	cout << d1 << d2 << endl;
	cin >> d1 >> d2;
	cout << d1 << d2 << endl;
	
}

void test07()
{
	Date d1(2024, 7, 13);
	Date d2(d1);
	d2.print();
	Date d3 = d1;
	d3.print();

	Date d4(2024, 6, 5);
	Date d5(2012, 3, 14);
	d4 = d5;
	d4.print();
}

void test08()
{
	Date d1(2024, 7, 13);
	const Date d2(2000, 8, 19);
	cout << &d1 << endl;
	cout << &d2 << endl;
}

void test09()
{
	Date d1;
	Date d2(2024, 7, 14);
	cout << (d2 += (-500)) << endl;
	cout << (d1 -= (-500)) << endl;
}

int main()
{
	//test02();
	//test01();
	//test03();
	//test04();
	//test05();
	//test06();
	//test07();
	//test08();
	test09();
	return 0;
}
  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值