Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to master C++ classes and objects

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces "how to master C++ classes and objects" related knowledge, in the actual case operation process, many people will encounter such a dilemma, then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

Assignment operator overload function 1. Operator overloading

C++ introduces operator overloading to enhance the readability of the code. operator overloading is a function with a special function name, but also has its return value type, function name and argument list, which is similar to a parameter list and an ordinary function.

Its function name is: operator + operator symbols (parameter list) that need to be overloaded.

It is important to note that:

(1) you cannot create a new operator by concatenating other symbols, such as operator$. That is, operator can only overload existing operators, and other symbols cannot be created through this function.

(2) the overloaded operator must have an Operand of a class type or an enumerated type.

(3) the meaning of operators for built-in types cannot be changed. For example, the type of a built-in integer cannot be changed.

(4) the number of parameters of the operator overloaded function is the Operand of the operator. For example, for the = = operator, its Operand is two, then the function parameter of the operator overloaded should be 2, that is, operator== (int x int y).

(5) for an operator overloaded function that is a member of a class, its argument appears to be one less than the Operand of the operator, because the function implies a formal parameter this, and the this pointer is limited to the first parameter.

(6) Grammar stipulates that there are five operators that cannot be overloaded, namely. * (pointer dereference in a member),:: (scope qualifier),? : (ternary operator),. (member (object) selection), sizeof (length operator). It is important to note that. (dereferencing operator) can be overloaded, and the operator that cannot be overloaded is the. * operator in s1.*ptr, that is, accessing the pointer member of the class and dereferencing.

Take the familiar date class as an example, for example, we want to implement the overloaded function of the = = operator:

/ / class Date {public: / / get the number of days in a certain year and month in the header file Date.h int GetDay (int year, int month) {assert (month > 0 & & month)

< 13); int Day[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; } return Day[month]; } //全缺省的构造函数 Date(int year = 0, int month = 1, int day = 1) { _year = year; _month = month; _day = day; //判断初始化的日期是否合理 if (_month >

= 13 | | _ day > GetDay (_ year, _ month) | | _ month

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report