• 【C++】学习笔记——类和对象_4



    二、类和对象

    13.运算符重载

    赋值运算符重载

    我们之前学了一个拷贝构造函数,本质上就是创建一个对象,该对象初始化为一个已经存在的对象的数据。

    // 拷贝构造
    Date d1(d2);
    
    • 1
    • 2

    而赋值运算符重载则是重载一个赋值运算符 “=” ,然后让两个已经存在的对象,一个拷贝赋值给另一个。

    // 赋值运算符重载
    d1 = d2;
    
    • 1
    • 2

    普通赋值运算符是支持连续赋值的,所以我们重载后的也需要连续赋值,即函数需要返回左值。
    赋值运算符的实现:

    #include
    using namespace std;
    
    class Date
    {
    public:
    	Date(int year = 1111, int month = 1, int day = 1)
    	{
    		_year = year;
    		_month = month;
    		_day = day;
    	}
    
    	~Date() {}
    
    	void Print()
    	{
    		cout << _year << "-" << _month << "-" << _day << endl;
    	}
    
    	// 拷贝构造函数
    	Date(const Date& d)
    	{
    		_year = d._year;
    		_month = d._month;
    		_day = d._day;
    	}
    
    	// 赋值运算符重载
    	Date& operator=(const Date& d)
    	{
    		// 自己给自己赋值没意义
    		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(2222, 2, 2);
    	Date d2(d1);
    	d1 = d2;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    赋值运算符重载函数也是6个默认的成员函数之一,所以我们不写编译器也会自动生成。它跟拷贝构造函数很相似,对内置类型进行浅拷贝处理,对自定义类型去调用它的赋值运算符重载函数。由于也是浅拷贝,所以涉及到堆上的空间开辟时,不能使用编译器自动生成的赋值运算符重载函数。

    14. 日期类的实现

    这个日期类的实现将会将之前学的所有知识进行融合。我们来进行标准的声明和定义分离。

    Date.h头文件

    头文件里只包括声明

    #pragma once
    #include
    #include
    using namespace std;
    
    class Date
    {
    public:
    	// 构造函数的声明
    	Date(int year = 1111, int month = 1, int day = 1);
    
    	// 重载运算符的声明
    	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);
    
    	// 日期加天数
    	Date& operator+=(int day);
    	Date operator+(int day);
    	Date operator-(int day);
    	Date& operator-=(int day);
    
    	// ++d1
    	Date& operator++();
    	// d1++  为了跟前置++区分,后置++强行增加了一个int形参,构成重载区分
    	Date operator++(int);
    	Date& operator--();
    	Date operator--(int);
    
    	// 日期减日期
    	int operator-(const Date& d);
    
    	int GetMonthDay(int year, int month)
    	{
    		// 断言月份错误
    		assert(month >= 1 && month <= 12);
    
    		// 静态变量只初始化一次
    		static int monthDays[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 29;
    		}
    
    		return monthDays[month];
    	}
    
    	// 打印日期的声明
    	void Print();
    	
    private:
    	int _year;
    	int _month;
    	int _day;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    Date.cpp源文件

    这个文件里是各个函数的定义。

    #include"Date.h"
    
    // 构造函数的定义
    Date::Date(int year, int month, int day)
    {
    	_year = year;
    	_month = month;
    	_day = day;
    }
    
    // 重载运算符的定义
    bool Date::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)
    		{
    			return _day < d._day;
    		}
    	}
    	return false;
    }
    
    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 _year == d._year
    		&& _month == d._month
    		&& _day == d._day;
    }
    
    bool Date::operator!=(const Date& d)
    {
    	return !(*this == d);
    }
    
    Date& Date::operator+=(int day)
    {
    	_day += day;
    	while (_day > GetMonthDay(_year, _month))
    	{
    		_day -= GetMonthDay(_year, _month);
    		++_month;
    		if (_month == 13)
    		{
    			++_year;
    			_month = 1;
    		}
    	}
    
    	return *this;
    }
    
    Date Date::operator+(int day)
    {
    	// 拷贝构造,避免修改原来的日期
    	Date tmp(*this);
    	// 使用重载后的+=
    	tmp += day;
    
    	// 局部对象不能引用返回
    	return tmp;
    }
    
    Date Date::operator-(int day)
    {
    	// tmp是刚创建的对象,所以这是拷贝构造而不是赋值
    	Date tmp = *this;
    	tmp -= day;
    
    	return tmp;
    }
    
    Date& Date::operator-=(int day)
    {
    	_day -= day;
    	while (_day <= 0)
    	{
    		--_month;
    		if (_month == 0)
    		{
    			--_year;
    			_month = 12;
    		}
    		_day += GetMonthDay(_year, _month);
    	}
    
    	return *this;
    }
    
    // ++d1
    Date& Date::operator++()
    {
    	*this += 1;
    
    	return *this;
    }
    
    // d1++
    Date Date::operator++(int)
    {
    	Date tmp = *this;
    	*this += 1;
    
    	return tmp;
    }
    
    Date& Date::operator--()
    {
    	*this -= 1;
    
    	return *this;
    }
    
    Date Date::operator--(int)
    {
    	Date tmp = *this;
    	*this -= 1;
    
    	return tmp;
    }
    
    int Date::operator-(const Date& d)
    {
    	int flag = 1;
    	Date max = *this;
    	Date min = d;
    	if (*this < d)
    	{
    		flag = -1;
    		max = d;
    		min = *this;
    	}
    	int n = 0;
    	while (max != min)
    	{
    		++min;
    		++n;
    	}
    
    	return flag * n;
    }
    
    // 打印日期的定义
    void Date::Print()
    {
    	cout << _year << "-" << _month << "-" << _day << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170

    test.cpp源文件

    #include"Date.h"
    
    int main()
    {
    	Date d1(2222, 2, 2);
    	d1.Print();
    	Date d2 = d1 + 10;
    	d2.Print();
    	cout << (d1 > d2) << endl;
    	Date d3;
    	d3.Print();
    	d3 += 10000;
    	d3.Print();
    	--d3;
    	d3.Print();
    	cout << (d1 - d3) << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    结果:
    在这里插入图片描述


    未完待续

  • 相关阅读:
    如何查看员工电脑操作记录
    CEC2018:动态多目标测试函数DF6~DF9的PS及PF
    webpack基础版及其常用插件分享超详细~~
    数据结构——树型结构二叉树与堆的代码功能讲解(1)
    用python编写远程控制程序
    Oracle 逻辑备份(数据迁移)
    1分钟快速实现Redis数据对比
    JSP教学评估管理系统myeclipse开发mysql数据库bs框架java编程web网页结构
    PRD文档
    十个关于商业智能商业智能BI的观点,你认同几个?
  • 原文地址:https://blog.csdn.net/m0_69828905/article/details/138087441