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

C++ 允许在同一作用域中的某个函数运算符指定多个定义,分别称为函数重载运算符重载

重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是它们的参数列表和定义(实现)不相同。

一、函数的重载

1、函数的重载:C++允许用同一函数名定义多个函数,而这些函数的参数个数和参数类型可以不相同。即一个函数名重新赋予它新的含义使一个函数名可以多用。

2、普通成员函数重载举例

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;
}

运行结果:

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

二、构造函数的重载

1、在一个类中可以定义多个构造函数,以便为对象提供不同的初始化的方式,供用户选用。这些构造函数具有相同的名字,而参数的个数或参数的类型不同。

2、用参数初始化表对数据成员初始化

C++提供了一种参数初始化表来实现数据成员初始化的方式

格式:

类名::构造函数([参数表])[:成员初始化表]

{

   [构造函数体]

}

举例:

//用形参h初始化数据成员height;
//用形参w初始化数据成员width;
//用形参len初始化数据成员length;
Box::Box(int h,int w,int len):height(h),width(w),length(len){}

3、构造函数重载举例

同一个类中定义两个构造函数,一个有参数,一个没有参数

class Box
{
   public :
     Box();  //声明无参数构造函数
     Box(int h,int w,int len):height(h),width(w),length(len){}//定义一个有参的构造函数,用参数对数据成员初始化
     int volume();//声明函数成员
   private:
     int height;
     int width;
     int length;
}
Box::Box()  //类外定义无参构造函数
{
  height=10;
  width=10;
  length=10;
}
 Box::volume()  //在类外定义成员函数
{
  return(height*width*length);
}

int main()
{
   Box box1;
   cout<

输出结果:

1000

11250

三、运算符的重载

1、运算符的重载:定义一个重载运算符的函数,使指定的运算符不仅能实现原有的功能,而且能实现在函数中指定的新功能。

运算符重载实质上时函数的重载。

2、运算符重载函数的格式: 

函数类型  opretor 运算符名称 (形参表)

{ 对运算符的重载处理 }

重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。

函数名:由operator和运算符组成  如上面叫做:“对运算符+重载的函数”

3、运算符重载函数的应用

#include 
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();// 6*7*5=210
   cout << "Volume of Box1 : " << volume <

 当上面的代码被编译和执行时,它会产生下列结果:

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

本节参考:C++ 重载运算符和重载函数 | 菜鸟教程

4、重载运算符的规则

(1)C++允许重载的运算符

C++ 重载函数和重载运算符_第1张图片

不可重载的运算符

C++ 重载函数和重载运算符_第2张图片

(2)重载运算符必须和用户定义的自定义类型的对象一起使用,其参数至少应有一个是类对象

5、一元运算符重载

一元运算符只对一个操作数进行操作,下面是一元运算符的实例:

  • 递增运算符( ++ )和递减运算符( -- )
  • 一元减运算符,即负号( - )
  • 逻辑非运算符( ! )

class Distance
{
   private:
      int feet;             // 0 到无穷
      int inches;           // 0 到 12
   public:
      // 所需的构造函数
      Distance(){
         feet = 0;
         inches = 0;
      }
      Distance(int f, int i){
         feet = f;
         inches = i;
      }
      // 显示距离的方法
      void displayDistance()
      {
         cout << "F: " << feet << " I:" << inches <

输出结果:

F: -11 I:-10
F: 5 I:-11

6、关系运算符重载

C++ 语言支持各种关系运算符( < 、 > 、 <= 、 >= 、 == 等等),它们可用于比较 C++ 内置的数据类型。

#include 
using namespace std;
 
class Distance
{
   private:
      int feet;             // 0 到无穷
      int inches;           // 0 到 12
   public:
      // 所需的构造函数
      Distance(){
         feet = 0;
         inches = 0;
      }
      Distance(int f, int i){
         feet = f;
         inches = i;
      }
      // 显示距离的方法
      void displayDistance()
      {
         cout << "F: " << feet << " I:" << inches <

运算结果

D2 is less than D1

你可能感兴趣的:(C++,c++,算法,开发语言)