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. 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:

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

According to Tutorialspoint

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

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

4 ★ | 1 Vote

May be interested

  • Operator in programming COperator in programming C
    operator is a symbol that tells the compiler to perform a certain mathematical operation or logical operation. language c has a lot of operators and provides operator types.
  • Call the function by pointer in C ++Call the function by pointer in C ++
    the method of calling a function by pointer in c ++ passes parameters to a function, copying the addresses of a parameter into the official parameter. inside the function, this address is used to access the actual parameter used in the function call. that is, changes made to the official parameter affect the passed parameter.
  • Call the function by value in C ++Call the function by value in C ++
    the method calls the function by the value of the parameters passed to a function that copies the actual value of a parameter into the official argument of the function. in this case, changes made to the parameter inside the function have no effect on the parameter.
  • Stack Overflow hits the hacker face, no significant damage is recordedStack Overflow hits the hacker face, no significant damage is recorded
    in a brief report released earlier today, stack overflow has reportedly recorded an attack that led hackers to successfully access their production systems.
  • Overload class member access operator (->) in C ++Overload class member access operator (->) in C ++
    the class member access operator (->) can be overloaded, but it's quite complicated.
  • Overload the one-seat operator in C ++Overload the one-seat operator in C ++
    the unary operator in c ++ operates on a single operand.
  • How to chat with Viettel operatorHow to chat with Viettel operator
    viettel customers can access viettel.vn website to chat directly with viettel operator in the fastest way and completely free instead of having to call the switchboard to get assistance. this new way of not only helps you save costs (no call charges) but also helps your phone to easily understand what you are asking.
  • How to Implement a Stack Data Structure in C++How to Implement a Stack Data Structure in C++
    a stack is a basic data structure that is commonly used throughout computer science. for example, a stack is utilized by your web browser to remember the last few pages you were on. developers and programmers should be very familiar with...
  • Class member functions in C ++Class member functions in C ++
    a member function of a class is a function that has its definition or prototype inside the class definition like any other variable. it works on any object of the class that is a member, and has access to all members of a class for that object.
  • How to Become a Full Stack ProgrammerHow to Become a Full Stack Programmer
    a full-stack programmer is a versatile developer who has experience and understanding of front-end and back-end software and hardware. full-stack developers also have a firm grasp of a multitude of programming languages, making them agile...