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
How to Integrate ChatGPT for Siri on iPhone
How to get data from web into Excel
Top best apps to turn men into women, boys into girls
How to blur background images in PowerPoint - Adjust background image display mode
Instructions for creating PowerPoint background color effects - Create a new background for slides
7 Best Pinterest Alternatives