Load the operator stack to call the function () in C ++
C ++ function call () can be overloaded for objects of class type. When you overload (), you are not creating a new way to call a function.
C ++ function call () can be overloaded for objects of class type. When you overload (), you are not creating a new way to call a function. Rather, you are creating an operator function that can be passed an arbitrary number of parameters.
The following example illustrates how to overload the operator to call the function () in C ++:
#include using namespace std ; class KhoangCach { private : int met ; int centimet ; public : // phan khai bao cac constructor can thiet KhoangCach (){ met = 0 ; centimet = 0 ; } KhoangCach ( int m , int c ){ met = m ; centimet = c ; } // nap chong toan tu goi ham () KhoangCach operator ()( int x , int y , int z ) { KhoangCach K ; // bay gio, dat phep tinh bat ky K . met = x + y + 5 ; K . centimet = y - z + 20 ; return K ; } // Phuong thuc de hien thi khoang cach void hienthiKC () { cout << "nDo dai bang m la: " << met << "nVa do dai bang cm la: " << centimet << endl ; } }; int main () { KhoangCach K1 ( 24 , 36 ), K2 ; cout << "Khoang cach dau tien la: " ; K1 . hienthiKC (); K2 = K1 ( 15 , 15 , 15 ); // trieu hoi toan tu () cout << "n--------------------------n" ; cout << "Khoang cach thu hai la: " ; K2 . hienthiKC (); return 0 ; }
Compiling and running the above C ++ program will produce the following results:
According to Tutorialspoint
Previous lesson: Load the assignment operator stack in C ++
Next lesson: Load the subscript operator [] in C ++
Update 25 May 2019
You should read it
- Overload subscript operator [] in C ++
- Overload class member access operator (->) in C ++
- Overload binary operators in C ++
- Operator in PHP
- Load the stack of assignment operators in C ++
- Boolean operators AND and OR in SQL
- Operator in C ++
- Overload relational operator in C ++
- ! = and! == What is the difference in JavaScript?
- Operator overloading in C #
- C Shell operators
- Basic Shell operators
Maybe you are interested
5 simple ways to help you remember everything in life black hole, white hole, deep hole Detecting the tremors of the 'twin brothers' of the solar system, the key to deciphering early life 4 cosmic phenomena have the speed of traveling through the speed of light Science test: Potatoes can be grown on Mars Learn about extraterrestrial super-Earth - Gliese 581c