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<<box1.volume()<<endl;
   Box box2(15,30,25);
   cout<<box2.volume()<<endl;
}

输出结果:

1000

11250

三、运算符的重载

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

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

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

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

{ 对运算符的重载处理 }

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

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

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

#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();// 6*7*5=210
   cout << "Volume of Box1 : " << volume <<endl;
 
   // Box2 的体积
   volume = Box2.getVolume();//12*13*10=1560
   cout << "Volume of Box2 : " << volume <<endl;
 
   // 把两个对象相加,得到 Box3
   Box3 = Box1 + Box2;//
 
   // Box3 的体积
   volume = Box3.getVolume();//(12+6)*(7+13)+(5+15)=5400
   cout << "Volume of Box3 : " << volume <<endl;
 
   return 0;
}

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

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

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

4、重载运算符的规则

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

不可重载的运算符

(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 <<endl;
      }
      // 重载负运算符( - )
      Distance operator- ()  
      {
         feet = -feet;
         inches = -inches;
         return Distance(feet, inches);
      }
}

int main()
{
   Distance D1(11, 10), D2(-5, 11);
 
   -D1;                     // 取相反数
   D1.displayDistance();    // 距离 D1
 
   -D2;                     // 取相反数
   D2.displayDistance();    // 距离 D2
 
   return 0;
}

输出结果:

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

6、关系运算符重载

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

#include <iostream>
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 <<endl;
      }
      // 重载负运算符( - )
      Distance operator- ()  
      {
         feet = -feet;
         inches = -inches;
         return Distance(feet, inches);
      }
      // 重载小于运算符( < )
      bool operator <(const Distance& d)
      {
         if(feet < d.feet)
         {
            return true;
         }
         if(feet == d.feet && inches < d.inches)
         {
            return true;
         }
         return false;
      }
};
int main()
{
   Distance D1(11, 10), D2(5, 11);
 
   if( D1 < D2 )
   {
      cout << "D1 is less than D2 " << endl;
   }
   else
   {
      cout << "D2 is less than D1 " << endl;
   }
   return 0;
}

运算结果

D2 is less than D1
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值