Detail C++ overloaded operators and overloaded functions

  • 2020-10-23 20:13:52
  • OfStack

C++ allows a function and operator in the same 1 scope to specify multiple definitions, called function overloading and operator overloading, respectively.

An overload declaration is a declaration that has the same name as a function or method previously declared within that scope, but with a different argument list and definition (implementation).

When you call an overloaded function or overloaded operator, the compiler decides to choose the most appropriate definition by comparing the parameter type you are using with the parameter type in the definition. The process of selecting the most appropriate overloaded function or operator is called an overload decision.

Function overloading in C++

Within the same scope, you can declare several functions of the same name that function similarly, but the formal arguments of the functions of the same name (the number, type, or order of arguments) must be different. You cannot override a function simply by returning a different type.

In the following example, the function print() with the same name is used to output different data types:


#include <iostream>
using namespace std;
 
class printData
{
  public:
   void print(int i) {
    cout << " Integer is : " << i << endl;
   }
 
   void print(double f) {
    cout << " Floating-point Numbers for : " << f << endl;
   }
 
   void print(char c[]) {
    cout << " The string for : " << c << endl;
   }
};
 
int main(void)
{
  printData pd;
 
  //  Output an integer 
  pd.print(5);
  //  Output floating point number 
  pd.print(500.263);
  //  Output string 
  char c[] = "Hello C++";
  pd.print(c);
 
  return 0;
}

When the above code is compiled and executed, it produces the following results:

[

The integer is: 5
The float number is: 500.263
The string Hello C++

]

Operator overloading in C++

You can redefine or override most of the C++ built-in operators. In this way, you can use operators of a custom type.

An overloaded operator is a function with a special name, and the function name is made up of the keyword operator and the operator symbol to be overloaded thereafter. Like the other function 1, the overload operator has a return type and an argument list.

[

Box operator+(const Box & );

]

Declare the addition operator used to add two Box objects and return the final Box object. Most overloaded operators can be defined as normal non-member functions or as class member functions. If we define the above function as a non-member function of the class, then we need to pass two parameters for each operation, as follows:

[

Box operator+(const Box & , const Box & );

]

The following example demonstrates the concept of operator overloading using member functions. Here, the object is passed as a parameter and its properties are accessed using the this operator, as shown below:


#include <iostream>
using namespace std;
 
class Box
{
  public:
 
   double getVolume(void)
   {
     return length * breadth * height;
   }
   void setLength( double len )
   {
     length = len;
   }
 
   void setBreadth( double bre )
   {
     breadth = bre;
   }
 
   void setHeight( double hei )
   {
     height = hei;
   }
   //  overloading  +  Operator, used to put two  Box  Objects together 
   Box operator+(const Box& b)
   {
     Box box;
     box.length = this->length + b.length;
     box.breadth = this->breadth + b.breadth;
     box.height = this->height + b.height;
     return box;
   }
  private:
   double length;   //  The length of the 
   double breadth;   //  The width of the 
   double height;   //  highly 
};
//  The main function of the program 
int main( )
{
  Box Box1;        //  The statement  Box1 That type of  Box
  Box Box2;        //  The statement  Box2 That type of  Box
  Box Box3;        //  The statement  Box3 That type of  Box
  double volume = 0.0;   //  Store the volume in this variable 
 
  // Box1  detailed 
  Box1.setLength(6.0); 
  Box1.setBreadth(7.0); 
  Box1.setHeight(5.0);
 
  // Box2  detailed 
  Box2.setLength(12.0); 
  Box2.setBreadth(13.0); 
  Box2.setHeight(10.0);
 
  // Box1  The volume of the 
  volume = Box1.getVolume();
  cout << "Volume of Box1 : " << volume <<endl;
 
  // Box2  The volume of the 
  volume = Box2.getVolume();
  cout << "Volume of Box2 : " << volume <<endl;
 
  //  Add the two objects and you get  Box3
  Box3 = Box1 + Box2;
 
  // Box3  The volume of the 
  volume = Box3.getVolume();
  cout << "Volume of Box3 : " << volume <<endl;
 
  return 0;
}

When the above code is compiled and executed, it produces the following results:

[

Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400

]

Reloadable/unreloadable operators

Here is a list of operators that can be overloaded:

双目算术运算符 + (加),-(减),*(乘),/(除),% (取模)
关系运算符 ==(等于),!= (不等于),< (小于),> (大于>,<=(小于等于),>=(大于等于)
逻辑运算符 ||(逻辑或),&&(逻辑与),!(逻辑非)
单目运算符 + (正),-(负),*(指针),&(取地址)
自增自减运算符 ++(自增),--(自减)
位运算符 | (按位或),& (按位与),~(按位取反),^(按位异或),,<< (左移),>>(右移)
赋值运算符 =, +=, -=, *=, /= , % = , &=, |=, ^=, <<=, >>=
空间申请与释放 new, delete, new[ ] , delete[]
其他运算符 ()(函数调用),->(成员访问),,(逗号),[](下标)

The following is a list of non-overloaded operators:

. : Member access operator .*, - > * : Member pointer access operator The :: : Domain operator sizeof: Length operator The & # 63; : : Conditional operator # : Preprocess the symbol

The above is the details of C++ overload operators and overload functions, more information about C++ overload operators and overload functions please pay attention to other related articles on this site!


Related articles: