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
- 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 ++
May be interested
- Load the Input / Output operator stack in C ++c ++ is able to input and output existing data types by using thread extraction operators >> and thread insertion operators
- The '+' operator in SQL Serverlearn how to use the + operator in sql server to join two or more strings into a string.
- How to fix CPU overload error 99%, 100% simple, effective!how to fix cpu overload error 99%, 100%? this article will help you solve the problem of computer overload quickly and effectively!
- Indexer in C #indexer in c # helps index objects, such as an array. when you define an indexer for a class, this class operates similarly to a virtual array. then you can access the instance of this class with the array access operator in c # ([]).
- Overload relational operator in C ++there are many different relational operators supported by c ++, such as: (, =, ==, ...) that can be used to compare the data types available in c ++.
- Fix CPU and 99% CPU overload errors on Windowsif your computer's fan is constantly running at high speed, your cpu is overloaded. this means your computer will slow down. that is really annoying if you don't fix it.
- How to load Au Mix, load Au Diamond mixhere's how to top up the au mix game, including how to top up the diamonds and top up the au mix packages.
- Load balancing in Web applications (end part)the previous section we studied the load balancing method based on dns round robin. however, this method can balance a number of users on each server, but it does not completely balance server load. therefore this section we will consider load balancing d
- How to Get Compensation from a BUI Accidentif you were injured in a boating accident, then you can sue the boat operator who caused the injury. for example, the operator may have hit your own boat, or you may have been in the boat with the operator who was intoxicated. either way,...
- How to fix Safari error can not load websites on Macever encountered the problem of not being able to load pages on safari on a mac? obviously your network connection is extremely stable, but a website can't keep loading on safari. here is how to fix this phenomenon.