Happy file C++ overloaded operators and overloaded functions

C++ overloaded operators and overloaded functions

Table of contents

C++ overloaded operators and overloaded functions

Function overloading in C++

example

Operator overloading in C++

example

Overloadable operator/non-overloadable operator

Operator overloading examples {#examples}



 

 C++ allows multiple definitions of a function  and operator to be specified in the same scope , known as function overloading  and operator overloading, respectively .

An overloaded declaration is a declaration with the same name as a function or method already declared in the scope, but with a different parameter list and definition (implementation).

When you call an overloaded function  or operator  , the compiler decides to use the most appropriate definition by comparing the parameter types you use with those in the definition. The process of selecting the most appropriate overloaded function or operator is called overload resolution .

Function overloading in C++

In the same scope, several functions with the same name can be declared with similar functions, but the formal parameters (the number, type or order of parameters) of these functions with the same name must be different. You can't overload functions just by differing in return type.

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

example

#include <iostream>
using namespace std;
 
class printData
{
   public:
      void print(int i) {
        cout << "整数为: " << i << endl;
      }
 
      void print(double  f) {
        cout << "浮点数为: " << f << endl;
      }
 
      void print(char c[]) {
        cout << "字符串为: " << c << endl;
      }
};
 
int main(void)
{
   printData pd;
 
   // 输出整数
   pd.print(5);
   // 输出浮点数
   pd.print(500.263);
   // 输出字符串
   char c[] = "Hello C++";
   pd.print(c);
 
   return 0;
}

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


整数为: 5
浮点数为: 500.263
字符串为: Hello C++

Operator overloading in C++

You can redefine or overload most of C++'s built-in operators. This way, you can use operators for custom types.

An overloaded operator is a function with a special name consisting of the keyword operator followed by the symbol of the operator to be overloaded. Like other functions, overloaded operators have a return type and a parameter list.


Box operator+(const Box&);

Declare the addition operator to add two Box objects and return the final Box object. Most overloaded operators can be defined as ordinary 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 the properties of the object   are accessed using the this operator, as follows:

example

#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;
      }
      // 重载 + 运算符,用于把两个 Box 对象相加
      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;      // 长度
      double breadth;     // 宽度
      double height;      // 高度
};
// 程序的主函数
int main( )
{
   Box Box1;                // 声明 Box1,类型为 Box
   Box Box2;                // 声明 Box2,类型为 Box
   Box Box3;                // 声明 Box3,类型为 Box
   double volume = 0.0;     // 把体积存储在该变量中
 
   // Box1 详述
   Box1.setLength(6.0); 
   Box1.setBreadth(7.0); 
   Box1.setHeight(5.0);
 
   // Box2 详述
   Box2.setLength(12.0); 
   Box2.setBreadth(13.0); 
   Box2.setHeight(10.0);
 
   // Box1 的体积
   volume = Box1.getVolume();
   cout << "Volume of Box1 : " << volume <<endl;
 
   // Box2 的体积
   volume = Box2.getVolume();
   cout << "Volume of Box2 : " << volume <<endl;
 
   // 把两个对象相加,得到 Box3
   Box3 = Box1 + Box2;
 
   // Box3 的体积
   volume = Box3.getVolume();
   cout << "Volume of Box3 : " << volume <<endl;
 
   return 0;
}

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


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

Overloadable operator/non-overloadable operator

The following is a list of operators that can be overloaded:

binocular arithmetic operator + (addition), - (subtraction), * (multiplication), / (division), % (modulo)
relational operator == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to)
Logical Operators || (logical or), && (logical and), ! (logical not)
unary operator + (positive), - (negative), * (pointer), & (address)
Increment and decrement operators ++(increment), --(decrement)
bitwise operator | (bitwise OR), & (bitwise AND), ~ (bitwise inversion), ^ (bitwise exclusive OR), << (left shift), >> (right shift)
assignment operator =, +=, -=, *=, /= , % = , &=, |=, ^=, <<=, >>=
Space application and release new, delete, new[ ] , delete[]
other operators () (function call), -> (member access), , (comma), [] (subscript)

The following is a list of non-overloadable operators:

  • .: member access operator
  • .*, ->*: Member pointer access operators
  • ::: field operator
  • sizeof: length operator
  • ?:: Conditional Operator
  • #: Preprocessing symbols

Operator overloading examples {#examples}

Examples of various operator overloading are provided below to help you better understand the concept of overloading.

serial number Operators and instances
1 unary operator overloading
2 binary operator overloading
3 relational operator overloading
4 Input/Output Operator Overloading
5 ++ and -- operator overloading
6 assignment operator overloading
7 function call operator() overloading
8 Subscript operator[] overloaded
9 Class member access operator -> overloaded

Guess you like

Origin blog.csdn.net/weixin_72686492/article/details/129988678