亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C++ 重载(运算符和函数)

C++ 允许您为 函数 指定多个定义 名称或操作员 在同一个作用域内,称为函数重载运算符重载 分别。

重载声明是在同一范围内与先前声明的声明同名的声明,除了两个声明具有不同的参数和明显不同的定义(实现)。

当你调用一个重载的函数操作员 ,编译器通过将您用于调用函数或运算符的参数类型与定义中指定的参数类型进行比较来确定要使用的最合适的定义。选择最合适的重载函数或运算符的过程称为重载解析 .

C++ 中的函数重载

您可以在同一范围内对同一函数名称进行多个定义。函数的定义必须因参数列表中参数的类型和/或数量而有所不同。不能重载仅返回类型不同的函数声明。

以下是相同函数 print() 的示例 用于打印不同的数据类型 -

现场演示
#include <iostream>
using namespace std;
 
class printData {
   public:
      void print(int i) {
        cout << "Printing int: " << i << endl;
      }
      void print(double  f) {
        cout << "Printing float: " << f << endl;
      }
      void print(char* c) {
        cout << "Printing character: " << c << endl;
      }
};

int main(void) {
   printData pd;
 
   // Call print to print integer
   pd.print(5);
   
   // Call print to print float
   pd.print(500.263);
   
   // Call print to print character
   pd.print("Hello C++");
 
   return 0;
}

当上面的代码编译并执行时,它会产生以下结果 -

Printing int: 5
Printing float: 500.263
Printing character: Hello C++

C++ 中的运算符重载

您可以重新定义或重载 C++ 中可用的大多数内置运算符。因此,程序员也可以使用用户定义类型的运算符。

重载运算符是具有特殊名称的函数:关键字“operator”后跟正在定义的运算符的符号。与任何其他函数一样,重载运算符具有返回类型和参数列表。

Box operator+(const Box&);

声明可用于 add 的加法运算符 两个 Box 对象并返回最终的 Box 对象。大多数重载运算符可以定义为普通的非成员函数或类成员函数。如果我们将上述函数定义为类的非成员函数,那么我们必须为每个操作数传递两个参数,如下所示 -

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

以下是使用成员函数显示运算符重载概念的示例。这里将一个对象作为参数传递,其属性将使用此对象访问,调用此运算符的对象可以使用 this 访问 运算符如下所述 -

现场演示
#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;
      }
      
      // Overload + operator to add two Box objects.
      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;      // Length of a box
      double breadth;     // Breadth of a box
      double height;      // Height of a box
};

// Main function for the program
int main() {
   Box Box1;                // Declare Box1 of type Box
   Box Box2;                // Declare Box2 of type Box
   Box Box3;                // Declare Box3 of type Box
   double volume = 0.0;     // Store the volume of a box here
 
   // box 1 specification
   Box1.setLength(6.0); 
   Box1.setBreadth(7.0); 
   Box1.setHeight(5.0);
 
   // box 2 specification
   Box2.setLength(12.0); 
   Box2.setBreadth(13.0); 
   Box2.setHeight(10.0);
 
   // volume of box 1
   volume = Box1.getVolume();
   cout << "Volume of Box1 : " << volume <<endl;
 
   // volume of box 2
   volume = Box2.getVolume();
   cout << "Volume of Box2 : " << volume <<endl;

   // Add two object as follows:
   Box3 = Box1 + Box2;

   // volume of box 3
   volume = Box3.getVolume();
   cout << "Volume of Box3 : " << volume <<endl;

   return 0;
}

当上面的代码编译并执行时,它会产生以下结果 -

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

可重载/不可重载的操作符

以下是可以重载的运算符列表 -

+ - * / % ^
& | ~ ! , =
< > <= >= ++ --
<< >> == != && ||
+= -= /= %= ^= &=
|= *= <<= >>= [] ()
-> ->* 新 [] 删除 删除[]

以下是不能重载的运算符列表 -

:: .* . ?:

运算符重载示例

以下是各种运算符重载示例,可帮助您理解概念。

Sr.No 运算符和示例
1 一元运算符重载
2 二元运算符重载
3 关系运算符重载
4 输入/输出操作符重载
5 ++ 和 -- 运算符重载
6 赋值运算符重载
7 函数调用()运算符重载
8 下标[]运算符重载
9 类成员访问操作符 -> 重载

C语言

  1. 在 C++ 编程中将数组传递给函数
  2. C++ 类和对象
  3. C++ 内存管理:新建和删除
  4. C++ 公共、受保护和私有继承
  5. C++朋友函数和朋友类
  6. C 结构和功能
  7. Python 运算符重载
  8. C++ 中的运算符示例:什么是、类型和程序
  9. 带有示例的 C++ 运算符重载
  10. 带有程序示例的 C++ 函数
  11. C++ 日期和时间
  12. C++中的多态性