C++--类和对象中(补充)

赋值运算符重载 
  运算符重载特点:
  1.当运算符被⽤于类类型的对象时,C++语⾔允许我们通过运算符重载的形式指定新的含义。C++规 定类类型对象使⽤运算符时,必须转换成调⽤对应运算符重载,若没有对应的运算符重载,则会编
译报错。
  2.运算符重载是具有特殊名字的函数,他的名字是由operator和后⾯要定义的运算符共同构成。和其他函数⼀样,它也具有其返回类型和参数列表以及函数体。
  3.重载运算符函数的参数个数和该运算符作⽤的运算对象数量⼀样多。⼀元运算符有⼀个参数,⼆元运算符有两个参数,⼆元运算符的左侧运算对象传给第⼀个参数,右侧运算对象传给第⼆个参数。
 4.如果⼀个重载运算符函数是成员函数,则它的第⼀个运算对象默认传给隐式的this指针,因此运算符重载作为成员函数时,参数⽐运算对象少⼀个。
 5.运算符重载以后,其优先级和结合性与对应的内置类型运算符保持⼀致。
⼀个类需要重载哪些运算符,是看哪些运算符重载后有意义,⽐如Date类重载operator-就有意
义,但是重载operator+就没有意义。
  6.重载++运算符时,有前置++和后置++,运算符重载函数名都是operator++,⽆法很好的区分。
C++规定,后置++重载时,增加⼀个int形参,跟前置++构成函数重载,⽅便区分。
  7.重载<<和>>时,需要重载为全局函数,因为重载为成员函数,this指针默认抢占了第⼀个形参位 置,第⼀个形参位置是左侧运算对象,调⽤时就变成了 对象<<cout,不符合使⽤习惯和可读性。 重载为全局函数把ostream/istream放到第⼀个形参位置就可以了,第⼆个形参位置当类类对
象。
  
  这是实现了一个日期类==的重载:
#include<iostream>

using namespace std;

class Date
{

public:
	Date(int year = 2024, int month = 10, int day = 20)//构造函数
	{
		_year = year;
		_month = month;
		_day = day;
	}

	Date(const Date&date)//拷贝构造
	{
		_year = date._year;
		_month = date._month;
		_day = date._day;
	}
	// 重载为全局的⾯临对象访问私有成员变量的问题
// 有⼏种⽅法可以解决:
// 1、成员放公有
// 2、Date提供getxxx函数
// 3、友元函数
// 4、重载为成员函数
//这里实现的是重载为成员函数

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

private:
	int _year;
	int _month;
	int _day;

};

int main()
{

	Date d1(1945,8,1);
	Date d2;

	if (d2 == d1)
	{
		cout << "d2==d1"<<endl;
	}
	else
		cout << "d2!=d1" << endl;

	return 0;

}

  运行结果

以下是一个日期类的实现:

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

using namespace std;


class Date
{

	// 友元函数声明
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);

public:
	Date(int year = 2024, int month = 10, int day = 20);

	void Print() const;
	// 直接定义类⾥⾯,他默认是inline
	// 频繁调用
		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 };
		// 365天 5h +
		if (month == 2 && (year % 4 == 0 && year % 100 != 0) || (year
			% 400 == 0))
		{
			return 29;
		}
		else
		{
			return monthDayArray[month];
		}
	}
	bool CheckDate();
	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;
	// d1 += 天数
	Date& operator+=(int day);
	Date& operator-=(int day);
	Date operator+(int day) const;
	// d1 -= 天数
	Date operator-(int day) const;
	// d1 - d2
	int operator-(const Date& d) const;
	// ++d1 -> d1.operator++()
	Date& operator++();
	// d1++ -> d1.operator++(0)
	// 为了区分,构成重载,给后置++,强⾏增加了⼀个int形参
	// 这⾥不需要写形参名,因为接收值是多少不重要,也不需要⽤
	// 这个参数仅仅是为了跟前置++构成重载区分
	Date operator++(int);
private:
	int _year;
	int _month;
	int _day;

};

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


//date.cpp
#include<iostream>
#include"date.h"

using namespace std;


	Date::Date(int year, int month, int day)//构造函数
	{
		_year = year;
		_month = month;
		_day = day;
		if (!CheckDate())
		{
			cout << "⽇期⾮法" << endl;
		}
	}

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

	void Date::Print() const
	{
		cout << _year << '-' << _month << '-' << _day<<endl;
	
	}

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

	bool Date::operator>(const Date& d2)const
	{
		if (_year > d2._year)
			return true;
		else if (_year == d2._year)

		{
			if (_month > d2._month)
				return true;
			else if (_month == d2._month)

			{
				if (_day > d2._day)
					return true;
			}
		}
		else
			return false;
	}
	bool Date::operator>=(const Date& d2)const
	{
		return (*this > d2 || *this == d2);

	}

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


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

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

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

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

	Date Date::operator-(int day) const
	{
		Date tmp = *this;
		tmp -= day;
		return tmp;
	}
	Date Date::operator++(int)//前置++
	{
		Date tmp = *this;
		tmp += 1;
		return tmp;
	}

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


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

	std::istream& operator>>(std::istream& in, Date& d)
	{
		std::cout << "请依次输入年月日:>";
		in >> d._year >> d._month >> d._day;
		if (!d.CheckDate())
		{
			std::cout << "日期非法" << std::endl;
		}
		return in;
	}

int main()
{

	Date d2(2025,1,1);
	d2-=500;
	d2.Print();
	d2 += 500;
	cout << d2;
	cin >> d2;
	cout << d2;

	return 0;

}

运行示例如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值