目录

C++ 重载

C ++允许您为同一范围内的function名称或operator指定多个定义,分别称为function overloadingoperator overloading

重载声明是声明,声明与同一作用域中先前声明的声明具有相同的名称,除了两个声明具有不同的参数和明显不同的定义(实现)。

当您调用重载functionoperator ,编译器通过将用于调用函数或运算符的参数类型与定义中指定的参数类型进行比较,确定要使用的最合适的定义。 选择最合适的重载函数或运算符的过程称为overload resolution

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

Overloadable/Non-overloadableOperators

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

+-*/%^
&|~!,=
<><=>=++--
<<>>==!=&&||
+=-=/=%=^=&=
|=*=<<=>>=[]()
->->*new new [] deletedelete []

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

::.*.?:

运算符重载示例

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

Sr.No 运算符和示例
1 一元运算符重载
2 二进制运算符重载
3 关系运算符重载
4 输入/输出运算符重载
5 ++和 - 运算符重载
6 赋值运算符重载
7 函数call()运算符重载
8 Subscripting [] Operator Overloading
9 类成员访问运算符 - >重载
<上一篇.C++ Inheritance
C++ Polymorphism.下一篇>
↑回到顶部↑
WIKI教程 @2018