The implementation of operator overloading in C++, focusing on the input and output overloading of cout and cin (super complete!!! A must-read for C++ novices)

Table of contents

1 Operator overloading

2. Statement of date

3. Definition of date

4.cout/cin input and output reloading

41.How does cout/cin implement input and output of built-in types?

4.2 Why overload?

4.3 How to overload custom type input/output


1 Operator overloading

C++ introduces operator overloading in order to enhance the readability of the code. Operator overloading is a function with a special function name. It also has its return value type, function name and parameter list. Its return value type and parameter list are the same as ordinary ones. The functions are similar.
The function name is: the keyword operator followed by the operator symbol that needs to be overloaded.
Function prototype: Return value type operator operator (parameter list)
Note:
You cannot create new symbols by concatenating them with other symbols. Operator: For example, operator@
The overloaded operator must have a class type parameter
for built-in type operators, and their meaning cannot be changed, for example: The built-in integer type + cannot change its meaning
When overloaded as a class member function, its formal parameters appear to be 1 less than the number of operands because the first parameter of the member function is implicit a>
hides this
.* :: sizeof ?: . Note that the above five operators cannot be overloaded. This often appears in multiple-choice questions in written examinations
.

In other words, operator overloading is essentially a variant of function overloading, except that there is an additional keyword operator. The function of the keyword is equivalent to equating the function name to these operators + - * /.

2. Statement of date

#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. Definition of date

//全缺省构造函数
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 input and output reloading

41.How does cout/cin implement input and output of built-in types?

 

This is a picture from the official c++ website (cplusplus.com/reference/). Here you can know that cout and cin are ostrem and istrem respectively. Object of class

In the ostrem class, the << operator overloads the parameters of the built-in type, so the cout object can output the parameters of the built-in type, and the same is true for the cin object.

4.2 Why overload?

Why is it possible to input built-in type 1, but not output custom type y1? Because the compiler doesn't know how you want to output your custom type, and how the built-in types output and input has been written for us in the c/c++ library, we can just use it directly.

4.3 How to overload custom type input/output

Note: When an operator with two operands is overloaded, the order in which the operands are passed must be ensured, so here we define the overloading of << as global, so that it can be better Locally adjust the order of passing parameters

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

	return out;
}

But there is a problem with this, that is, we cannot access the member variables modified byprivate in the object d of the Date class, which is needed here By using the friend function, the friend function can access the privateprivate of the object corresponding to the class. a>Member variable

This is successful, and cin is used in the same way.        

Guess you like

Origin blog.csdn.net/qq_73955920/article/details/134067288
Recommended