Overloaded functions and operators

Overloaded function: in the same scope, similar functions can be declared several functions with the same name, but the same name as a function of these parameters form (refer to the number of parameters, the type or order) must be different. You can not only by overloaded functions return different types.

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Calculation
 5 {
 6 public:
 7     static int GetSum(int a, int b)
 8     {
 9         return a + b;
10     }
11     static int GetSum(int a, int b ,int c)
12     {
13         return a + b + c;
14     }
15      / * static Double GetSum (A int, int B) // here because only changes the return type, and not overloaded functions, it will be given
 16      {
 . 17          return A + B;
 18 is      } * / 
. 19      static  Double GetSum ( Double A, Double B)
 20 is      {
 21 is          return A + B;
 22 is      }
 23 is      static  Double GetSum ( Double A, Double B, Double C)
 24      {
 25          return A + B + C;
 26 is      }    
27 };
28 
29 int main()
30 {
31     cout << Calculation::GetSum(5, 6) << endl
32         << Calculation::GetSum(1.1, 2.1 ,3.3) << endl;
33     return 0;
34 }

Results:  11 6.5 

Overload operator: overloaded operator is a special function with the name, function name followed by the key operator and the operator to override symbols. As with other functions, overloaded operators have a return type and an argument list. Most overloaded operator may be defined as a normal or non-member function is defined as a class member function. Suppose we overloaded "+", if we define a class of non-member functions, then we need to pass two parameters for each operation, if it is a class member function only needs to pass a. Operator overloading reason I feel very bad master, and so have the opportunity to come back again in depth about it, now temporarily know like. Said the following about the way overloaded operators: the return type of operator Operator (parameters) {} For realization of chestnuts:  BOOL opreator - () {return false;}  This function is overloaded member function, and why? Because "-" this operator we need at least an argument, but it is not one it can only be its own class.

. 1 #include <the iostream>
 2  the using  namespace STD;
 . 3  
. 4  class Studnet
 . 5  {
 . 6  public :
 . 7      int SUM = 0 ;
 . 8      int SUM2 = 0 ;
 . 9      int divisor = 0 ;
 10      int dividend The = 0 ;
 . 11    // this overload function of two variables to achieve their arguments summing
 12 is      Studnet operator / ( const Studnet & D)      // below to explain & const and with use. Here return type is a class 
13     {
 14          Studnet STD;
 15          std.sum + = d.dividend d.divisor; // variables two parameters are added
 16          std.sum2 = the this -> + dividend The the this -> divisor; // class itself two variables addition
 . 17          return STD;
 18 is      }
 . 19  };
 20 is  
21 is  
22 is  int main ()
 23 is  {
 24      Studnet ST1;
 25      Studnet ST2;
 26 is      Studnet ST3;
 27  
28      st1.dividend = . 5 ;
 29      st1.divisor = . 6 ;
30     st2.dividend = 10;
31     st2.divisor = 20;
32 
33     st3 = st2 / st1;
34     cout << st3.sum << endl
35         << st3.sum2 << endl;
36     return 0;
37 }

We look at the results:  . 11 30  , we find that parameter variables and d are variables st1 and (SUM), the description is overloaded functions when the class member function, its value is the parameter to pass this parameter behind it (st1). And this means that st2. Why is it, if we

int A = 10 ;
 int B = 10 ; 
a.add (B) is equivalent to A + B

This naturally very clear. If a non-member function is the same reason, but became two parameters. Here Come talk const and & mix used: & passed by reference to, copy-prevention data value transfer, const is to protect data from being altered. const & copy constructor function call overhead while preventing traditional values. Because when we use the value passed, parameter argument is a copy (copy).

Guess you like

Origin www.cnblogs.com/xiaodangxiansheng/p/10986146.html