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:

Picture 1 of Load the operator stack to call the function () in C ++

According to Tutorialspoint

Previous lesson: Load the assignment operator stack in C ++

Next lesson: Load the subscript operator [] in C ++

« PREV POST
READ NEXT »