[C++ Basics: 23]: [Important Template] Design and implementation of relational operator overloading: [>, <, >=, <=, !=, ==] overloading [Take Date class as an example]

This series of C++ related articles is only for the author’s study notes, and I will use my own understanding to record the study! The C++ learning series will be divided into three stages: basics, STL, and advanced data structures and algorithms . The relevant key contents are as follows:

  1. Basics : Classes and Objects (involving the three major features of C++, etc.);
  2. STL : Learn to use the STL related libraries provided by C++ ;
  3. High-order data structures and algorithms : Manually implement your own STL library and design and implement high-order data structures , such as B-tree, B+ tree, red-black tree, etc.

Study set:


Contents of this issue: [C++ Basics: 23]: Operator overloading of relational operators [ > , < , >= , <= , != , == ] overloading


Basic understanding of operator overloading: C++ learning:: [Basics: 17]: C++ classes and objects: introduction to operator overloading, operator overloading function (difference between within a class and outside a class) writing method and simple design implementation


Contents:
1. Review of how to write operator overloaded functions
2. Design ideas for six relational operator overloads (important)
3. Implementation of six relational operator overloaded functions [taking the Date class as an example]
- - 3.1 Basic design of the Date class
- - 3.2 Six major Relational operator implementation
4. Common problems involving const in classes!
5. Recommended related articles


[ C++ learning collection link ]


1. Review of how to write operator overloaded functions

  • Operator overloaded function name (fixed!): operator
  • Basic format writing: function return value type operator operator (parameter list) {}
  • Note: The return values ​​of the relational operators in this issue are: bool (Boolean type)

2. Design ideas for overloading of six relational operators

Six relational operators: greater than, less than, greater than or equal to, less than or equal to, not equal to, equal to . Their return results are only: yes/no!


In the coding practice stage, we can implement all six operator overloads one by one, but in actual development, we consider many factors. For example, we should learn to reuse existing functions to implement other functions to prevent a large number of code errors. Highly similar redundant code. Therefore: Combining operator overloading here provides a design idea to solve the problem.


  • First of all, it is known that there are only two possible results for the two comparison objects! Nothing more than: yes or no!
  • Secondly, taking greater than as an example, when comparing two objects , they are either greater than or not greater than! There are many situations where it is not greater than , such as: less than or equal to! And just less than or equal to includes these two situations, that is to say: negating the result of greater than is less than or equal to, that is: you can use the greater than function to achieve less than or equal to [Note: Don’t simply say that if it is not greater than, it is less than !
  • Design plan: implement > and ==, and reuse these two relationships to implement: <, >=, <=, !=

illustrate:

  • "Greater than or equal to" : that is: "greater than or equal to" or "not less than"
  • "Less than or equal to" : that is: "less than or equal to" or "not greater than"
  • "Less than" : that is: "not greater than + not equal to"
  • "Not equal to" : that is: "the negation of equal to"
  • This is the design scheme used by the author as an example in this article. There are actually other combinations. Readers can design and implement them by themselves using code! [Note: The writing method of relational operators is universal!

3. Implementation of overloaded functions of six relational operators

3.1 Basic design of Date class

#include<iostream>
using std::cout;
using std::endl;
using std::cin;

class Date {
public:
	Date(int year,int month, int day) 
		:_year(year),_month(month),_day(day)
	{

	}
	void Print() {
		cout << _year << "年" << _month << "月" << _day << "日" << endl;

	/* 实现两个日期对象的大小比较 */
	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;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main() {


	return 0;
}

3.2 Implementation of six relational operators

The following is how to write overloaded functions (templates) for six relational operators!
Readers can think for themselves:

  1. Other implementation options!
  2. Why use const and references
/* 大于运算符重载 */
bool Date::operator > (const Date& d) const
{
	if (_year > d._year
		|| _year == d._year && _month > d._month
		|| _year == d._year && _month == d._month && _day > d._day) return true;
	return false;
}

/* 等于运算符重载 */
bool Date::operator == (const Date& d)  const {
	return (_year == d._year && _month == d._month && _day == d._day);
}

/* 不等于运算符重载:对等于结果取反 */
bool Date::operator != (const Date& d) const {
	return !(*this == d);
}

/* 小于运算符重载:既不大于也不等于 */
bool Date::operator < (const Date& d) const {
	return !(*this == d) && !(*this > d);
}

/* 大于等于运算符重载:大于或等于 */
bool Date::operator >= (const Date& d) const {
	return (*this > d) || (*this == d);
}

/* 小于等于运算符重载:不大于 */
bool Date::operator <= (const Date& d) const {
	return !(*this > d);
}

4. Recommended related articles

1. C++ learning::[Basics: 11]: Basic use of C++ classes and non-static this pointers (two interview test points): Null pointer issues of classes (can this pointer be null?) | Where does this pointer exist?
2. [C++ Basics: 22]: Const objects and const member functions/methods of classes and common problems involving const in classes!

Guess you like

Origin blog.csdn.net/weixin_53202576/article/details/131172775