c++中运算符重载的实现,着重讲解cout、cin的输入输出重载讲解(超全!!!c++小白必看)

目录

1运算符重载

2.日期的声明

3.日期的定义

4.cout/cin输入输出重载

41.cout/cin怎么实现内置类型的输入输出的?

4.2为什么要重载?

4.3怎么重载自定义类型的输入/输出


1运算符重载

C++ 为了增强代码的可读性引入了运算符重载 , 运算符重载是具有特殊函数名的函数 ,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
函数名字为:关键字 operator 后面接需要重载的运算符符号 。
函数原型: 返回值类型  operator 操作符 ( 参数列表 )
注意:
不能通过连接其他符号来创建新的操作符:比如 operator@
重载操作符必须有一个类类型参数
用于内置类型的运算符,其含义不能改变,例如:内置的整型 + ,不 能改变其含义
作为类成员函数重载时,其形参看起来比操作数数目少 1 ,因为成员函数的第一个参数为隐
藏的 this
.*   ::   sizeof   ?:    . 注意以上 5 个运算符不能重载。这个经常在笔试选择题中出
现。

也就是说,运算符重载实质上是函数重载的一种变形,只是多了个关键字operator,关键字的作用相当于把函数名等价于 + - * / 这些运算符。

2.日期的声明

#include<iostream>
using namespace std;

class Date
{
public:
	//全缺省构造函数
	Date(int year = 1970, int month = 1, int day = 1);
	//析构函数
	~Date();

	//拷贝构造函数(特殊的构造函数,跟构造函数重载)
	Date(const Date& dd);
	//赋值运算符重载
	Date& operator=(const Date& dd1);

	//每个月有多少天
	int GetMonthDay(int year,int month);
	//运算符重载
	Date& operator+=(int day);
	Date operator+(int day);
	Date& operator-=(int day);
	Date operator-(int day);

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

	bool operator==(const Date& dd);
	bool operator!=(const Date& dd);
	bool operator>(const Date& dd);
	bool operator>=(const Date& dd);
	bool operator<(const Date& dd);
	bool operator<=(const Date& dd);


	void Print();
private:
	int _year = 2023;
	int _month = 10;
	int _day = 7;
};

3.日期的定义

//全缺省构造函数
Date::Date(int year,int month,int day)
{
	_year = year;
	_month = month;
	_day = day;
}

//析构函数
Date::~Date()
{
	_year = 0, _month = 0; _day = 0;
}

//拷贝构造函数(特殊的构造函数,跟构造函数重载)
Date::Date(const Date& dd)
{
	_year = dd._year;
	_month = dd._month;
	_day = dd._day;
}

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

Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year,_month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
		//由12月到13月的情况
		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)
{
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		//由1月到0月的情况
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDay(_year,_month);
	}
	return *this;
}
Date Date::operator-(int day)
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}

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

bool Date::operator==(const Date& dd)
{
	return _year == dd._year && _month == dd._month && _day == dd._day;
}
bool Date::operator!=(const Date& dd)
{
	return !(*this == dd);
}
bool Date::operator>(const Date& dd)
{
	if (_year > dd._year)
	{
		return true;
	}
	else if (_year == dd._year && _month > dd._month)
	{
		return true;
	}
	else if (_year == dd._year && _month == dd._month &&_day>dd._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool Date::operator>=(const Date& dd)
{
	return *this > dd && *this == dd;
}
bool Date::operator<(const Date& dd)
{
	return !(*this >= dd);
}
bool Date::operator<=(const Date& dd)
{
	return *this < dd && *this == dd;
}


//获取某年某月的天数
int Date::GetMonthDay(int year ,int month)
{
	int MonthArray[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	//判断是否为闰年的2月
	if ((month == 2) && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		return 29;
	}
	return MonthArray[month];
}

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

4.cout/cin输入输出重载

41.cout/cin怎么实现内置类型的输入输出的?

 

这是c++官网(cplusplus.com/reference/)里面的图,这里可以知道cout和cin分别是ostrem和istrem类的对象

在ostrem这个类中,<< 的运算符重载了内置类型的参数,所以cout对象可以输出内置类型的参数,cin对象同理

4.2为什么要重载?

为什么输入内置类型1可以,而输出自定义类型 y1 不可以呢?因为编译器不知道你要怎么输出你的自定义类型,而内置类型怎么输出输入的在c/c++库里面已经帮我们写好了,我们直接用就行.

4.3怎么重载自定义类型的输入/输出

注意:有两个操作数的操作符在重载时,要保证操作数传参的顺序,所以我们在这里把<<的重载定义为全局的,这样才能更好地调整传参的顺

//在函数体外定义,有两个操作数的操作符要保证传参顺序!!!
ostream& operator<<(ostream& out,const Date d)
{
	out << d._year << "-" << d._month << "-" << d._day << endl;

	return out;
}

但是这样有个问题,那就是我们不能访问Date类的对象d里面的private修饰的成员变量,这里就需要用到友元函数了,友元函数就可以访问该类所对应的对象的private成员变量了

这样就成功了,cin也是一样的用法        

猜你喜欢

转载自blog.csdn.net/qq_73955920/article/details/134067288