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 ++
4 ★ | 1 Vote
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
6 ways to fix the error 'PowerPoint found a problem with the content'
Sample painting of Hanoi in my heart, simple landscape
PowerPoint 2016: Working with lists in PowerPoint
How to Paste an Image into a Selection in Photoshop
Europe develops satellite internet system to replace Starlink
Intel releases new driver update package with 6GHz optimization and many other improvements