C++初阶——类和对象(五)运算符重载

C++初阶——类和对象(五)

一、运算符重载

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

  • 函数名字为:关键字operator后面接需要重载的运算符符号。
  • 函数原型:返回值类型operator操作符(参数列表)

注意事项

  • 不能通过连接其他符号来创建新的操作符:比如operator@
  • 重载操作符必须有一个类类型参数
  • 用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不能改变其含义
  • 作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this
  • - .* :: sizeof ?: . 注意以上5个运算符不能重载

二、日期类的实现

为了更好的理解运算符重载,我们以日期类为例,实现对日期的+、-、+=、-=、==、!=等运算符的重载。
示例

构造函数

示例

1.日期大小比较

(1)==

示例1
函数定义还是简单易懂的,但是这里有两个const值得注意。其中括号里的const表示==号右边的对象不能被改变,而括号外的const修饰的是this指针,因为this指针不能再形参列表中显式地写,因此const只能写到外面,这是语法规定。至于这里为什么处处都要使用const,因为==是判断两边的日期是否相等,不需要做出任何的变化,使用const是将权限缩小,更为稳妥。

(2)!=

示例2
有了==,!=就更简单了,直接覆用即可。这里使用const还是一样的道理。

(3)<

示例3
按年月日依次比较。

(4)<=

示例4
有了== 和<,后面的比较就很简单了,直接覆用即可。

(5)>

示例5

(6)>=

示例6

2.日期的简单运算

准备工作:获取每月的天数

示例7
注意闰年的判断。

(1)+=

示例8
返回的是左值变化后的大小,因此需要对this指针解引用。

(2)+

示例9

(3) -=

示例10

(4) -

示例11

(5)前置++和后置++

示例11

(6)前置 -- 和后置 --

示例12

(7)日期相减

示例13

三、声明和定义分离

1.头文件:Date.h

#pragma once
#include<iostream>
#include<assert.h>

using namespace std;

class Date
{
public:
	Date(int year = 2025, int month = 3, int day = 17);
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	bool operator==(const Date& x) const;
	bool operator!=(const Date& x) const;
	bool operator<(const Date& x) const;
	bool operator<=(const Date& x) const;
	bool operator>(const Date& x) const;
	bool operator>=(const Date& x) const;

	int GetMonthDay(int year, int month);

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

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

	Date& operator++();
	Date operator++(int);

	Date& operator--();
	Date operator--(int);

	int operator-(const Date& d) const;
private:
	int _year;
	int _month;
	int _day;
};

2.源文件:Date.c

#include"Date.h"

Date::Date(int year, int month, int day)
{
	if (month > 0 && month < 13 && day>0 && day <= GetMonthDay(year, month))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "非法日期" << endl;
		assert(false);
	}
}

int Date::GetMonthDay(int year, int month)
{
	static int daysArr[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;
	}
	else
	{
		return daysArr[month];
	}
}

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

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

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

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

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

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

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

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

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

本期总结+下期预告

本期内容实现了几个运算符重载,简单易懂。下期内容将为大家继续介绍类和对象的剩余内容。

感谢大家的关注,我们下期再见!
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值