In classes and objects (2)

insert image description here

1. Operator overloading

1. Reasons for operator overloading

  In C language, we can only compare with >, <, = >, <, =><= to compare the sizes of built-in types, while the sizes of custom types cannot be compared. In this regard, C++ introduces operator overloading to enhance the readability of the code. Operator overloading is a function with a special function name, and also has its return value type, function name, and parameter list. Its return value type and parameter list are similar to ordinary functions.

Format:
The keyword operator is followed by the operator symbol that needs to be overloaded Function
prototype: return value type + operator operator (parameter list)

Not all operators can be overloaded

Note:
1. New operators cannot be created by connecting other symbols: for example, the operator@
overloaded operator must have a class type parameter
2. The meaning of the operator used for the built-in type cannot be changed, for example: the built-in integer type +, cannot change its meaning
3. When overloaded as a class member function, its formal parameter appears to be 1 less than the number of operands, because the first parameter of the member function is hidden this
4. .* :: sizeof ?: . Note The above 5 operators cannot be overloaded.This often appears in written multiple choice questions

2. Implement operator overloading globally and in classes

  When we write it as a global function, we need to set the member variables such as _year in the class as public and can only be accessed outside the class. Here you will find that operator overloading requires member variables to be public, so the question arises, how to ensure encapsulation? In fact, it can be solved by using the friends we will learn later.

Implement it in a class:

3. Assignment operator overloading

1. Why can't assignment operator overloading be written globally?

  Assignment operator overloading is different from other operator overloading; when we write code, there are continuous assignment operations, so assignment operator overloading must return *this

Format:
Parameter type: const T&, passing reference can improve the efficiency of parameter passing Return
value type: T&, returning reference can improve the efficiency of returning, return *this: It must conform to the meaning of continuous assignment to
detect whether to assign a value to itself

  If the assignment operator is not explicitly implemented, the compiler will generate a default one. At this time, if the user implements a global assignment operator overload outside the class, it will conflict with the default assignment operator overload generated by the compiler in the class, so the assignment operator overload can only be a member function of the class

2. When do you need to implement assignment operator overloading yourself?

  When the user does not explicitly implement, the compiler will generate a default assignment operator overload,copy byte by value. Note: Built-in type member variables are directly assigned, while custom type member variables need to call the assignment operator overload of the corresponding class to complete the assignment.That is, if resource management is not involved in the class, the assignment operator can be implemented or not; once resource management is involved, it must be implemented

4. Pre-++ and post-++ overloading

  Pre ++ returns the value after +, and post ++ returns the value before +, so how do pre ++ and post ++ constitute overloading?
  C++ stipulates that an additional parameter of int type is added when the post-position ++ is overloaded, but the parameter does not need to be passed when calling the function, the compiler automatically passes it, and the pre-position ++ can return a reference, while the post-position temporary variable temp cannot return a reference

5. Advantages of operator overloading

Advantages:
1. Make the program more readable: through operator overloading, you can implement custom operations for operators in the class, making the program code more concise, intuitive, and in line with people's habits, thereby enhancing the readability of the program sex.
2. Realize the versatility and generalization of the program: Through operator overloading, some similar operations can be processed in a unified manner, so as to realize the versatility and generalization of the program, avoid a large number of repeated codes in the program, and improve the efficiency of the program. Maintainability and scalability.
3. Improve the efficiency of the program: By overloading the operator, the efficiency of the program can be improved without increasing the complexity of the program, thereby increasing the running speed of the program.
4. Realize the naturalness and readability of the program: operator overloading can make the program closer to human natural language, thus making the program easier to understand and debug.

Two, const member function

  The const-modified "member function" is called a const member function. The const-modified class member function actually modifies the implicit this pointer of the member function, indicating that any member of the class cannot be modified in the member function.

1. Two examples of common const permission amplification errors

Error example 1:

  When we implement a certain function without modifying this pointer, we can modify this pointer with const,But the this pointer does not allow display transferSo we can only add a const like this

grammar:
void Display() const

  Permissions can be reduced, but not enlarged. Therefore, when the this pointer does not need to be modified outside, we should try to add const to modify this to avoid the following situation. Error
example 2:

2. Advantages of const member functions

Advantages:
1. Declaring a member function as const can ensure that the function will not modify the data members of the object, thereby improving the robustness and security of the program.
2. The const member function can be called by the const object, thus restricting the illegal operation on the const object and protecting the data members of the object from being accidentally modified.
3. The const member function can be overloaded. Using different const member functions can access different member variables of the object or perform different operations, improving the flexibility of the program.

Summary:
  Welcome to Xiaobai's article. If there are any mistakes, please point them out severely. In the next blog, we will use the knowledge of this blog to implement the date class, and look forward to meeting us in the next blog! ! !

Guess you like

Origin blog.csdn.net/Front123456/article/details/130556606