Operator overload and Load overlap in C ++
C ++ allows you to define more than one definition for a function name or an operator in the scope, respectively called Load overloading (function overloading) and Load operator overloading in C ++. .
A overloaded declaration is a declaration that has been declared with the same name as a previously declared declaration in the same scope, except that: both declarations have different parameters and different definitions. .
When you call a overloaded function or a overloaded operator, the compiler decides the most appropriate definition to use by comparing the parameter types you used to call the function or operator with the parameter types. defined in the definition. This process of choosing the best overloading function or overloading operator is called overloading overload .
Load overlay in C ++
You can have multiple definitions for the same function name in the same scope. The definition of the function must be different from each other in terms of type and / or number of parameters in the parameter list. You cannot overload function declarations but only in return types.
In the following example, the same ham function is used to print different data types:
#include using namespace std ; class inDuLieu { public : void hamIn ( int i ) { cout << "In so nguyen: " << i << endl ; } void hamIn ( double f ) { cout << "In so thuc: " << f << endl ; } void hamIn ( char * c ) { cout << "In ky tu: " << c << endl ; } }; int main ( void ) { inDuLieu idl ; // Goi ham hamIn de in so nguyen idl . hamIn ( 12345 ); // Goi ham hamIn de in so thuc idl . hamIn ( 6677.02 ); // Goi ham hamIn de in ky tu idl . hamIn ( "Hoc C++ co ban va nang cao tai QTM.com" ); return 0 ; }
Overload operator in C ++
You can redefine or overload most of the built-in operators in C ++. Therefore, a programmer can use operators with self-defined type (user-defined).
Operator overloading in C ++ functions with special names: Function name is the operator keyword followed by the symbol of the operator being defined. Like any other function, a overloaded operator has a return type and a parameter list.
Box operator +( const Box &);
Declare the + operator to add two Box objects and return the last Box object. Most overloading operators can be defined as: non-member functions or class member functions. In the above case, we define the function as a non-member of a class, then we have to pass two parameters for each operand, as follows:
Box operator +( const Box &, const Box &);
The following example illustrates the concept of operator overloading using a member function. Here, an object is passed as a parameter whose properties will be accessed using this object, the object that will call this operator can be accessed using the this operator. as follows:
#include using namespace std ; class Box { public : double tinhTheTich ( void ) { return chieudai * chieurong * chieucao ; } void setChieuDai ( double dai ) { chieudai = dai ; } void setChieuRong ( double rong ) { chieurong = rong ; } void setChieuCao ( double cao ) { chieucao = cao ; } // Nap chong toa tu + de cong hai doi tuong Box. Box operator +( const Box & b ) { Box box ; box . chieudai = this -> chieudai + b . chieudai ; box . chieurong = this -> chieurong + b . chieurong ; box . chieucao = this -> chieucao + b . chieucao ; return box ; } private : double chieudai ; // chieu dai cua mot box double chieurong ; // Chieu rong cua mot box double chieucao ; // Chieu cao cua mot box }; // ham main cua chuong trinh int main ( ) { Box Box1 ; // Khai bao Box1 la cua kieu Box Box Box2 ; // Khai bao Box2 la cua kieu Box Box Box3 ; // Khai bao Box3 la cua kieu Box double thetich = 0.0 ; // Luu giu the tich cua mot box tai day // thong tin chi tiet cua box 1 Box1 . setChieuDai ( 2.0 ); Box1 . setChieuRong ( 3.0 ); Box1 . setChieuCao ( 4.0 ); // thong tin chi tiet cua box 2 Box2 . setChieuDai ( 5.0 ); Box2 . setChieuRong ( 6.0 ); Box2 . setChieuCao ( 7.0 ); // the tich cua box 1 thetich = Box1 . tinhTheTich (); cout << "The tich cua Box1 la: " << thetich << endl ; // the tich cua box 2 thetich = Box2 . tinhTheTich (); cout << "The tich cua Box2 la: " << thetich << endl ; // cong hai doi tuong nhu sau: Box3 = Box1 + Box2 ; // the tich cua box 3 thetich = Box3 . tinhTheTich (); cout << "The tich cua Box3 la: " << thetich << endl ; return 0 ; }
Compiling and running the above C ++ program will produce the following results:
The operator can overload and cannot overload in C ++
The following table lists the operators that can be overloaded in C ++:
+ - * /% ^ & | ~! , = <> <=> = ++ - << >> ==! = && || + = - = / =% = ^ = & = | = * = << = >> = [] () -> -> * new new [] delete delete []Here is a list of operators that cannot be overloaded in C ++:
::. *. ?:Example of operator overloading in C ++
Here are the various examples illustrating Operator overloading in C ++, thereby helping you understand this concept more deeply. Click on the link to see an example:
STATE DEBT AND Example1Overload the unary operator in C ++
2Overload binary operators in C ++
3Overload relational operator in C ++
4Load the Input / Output operator stack in C ++
5Load operator ++ and - in C ++
6Load the stack of assignment operators in C ++
7Load the operator stack to call the function () in C ++
8Overload the [] operator in C ++
9Overload class member access operator -> in C ++
According to Tutorialspoint
Previous article: Calculating inheritance in C ++
Next lesson: Overloading a one-operator operator in C ++
You should read it
- Overload the one-seat operator in C ++
- Overload subscript operator [] in C ++
- Load operator ++ and - in C ++
- Operator overloading in C #
- Load the stack of assignment operators in C ++
- Load the Input / Output operator stack in C ++
- Operator in programming C
- Overload binary operators in C ++
- Load the operator stack to call the function () in C ++
- Overload class member access operator (->) in C ++
- The '+' operator in SQL Server
- Stack operator in Python
Maybe you are interested
Difference between function and formula in Excel
8 little-known Excel functions that can save you a lot of work
How to use the NORMDIST function in Excel - Function that returns the distribution in Excel
Date functions in Excel, DAY, WEEKDAY, MONTH
How to use the SUMIF function in Excel to calculate the sum based on conditions
How to use the Round function in Excel to round numbers and process data