运算符重载



所谓重载,就是重新赋予新的含义。 函数重载就是对一个已有的函数赋予新的含义,使之实现新功能,因此,一个函数名就可以用来代表不同功能的函数,也就是 一名多用
 
运算符也可以重载。实际上,我们已经在不知不觉之中使用了运算符重载。例如,大  家都已习惯于用加法运算符 ”+” 对整数、单精度数和双精度数进行加法运算,如 5+8   5.8 +3.67 等,其实计算机对整数、单精度数和双精度数的加法操作过程是很不相同的,  但由于 C++ 已经对运算符 ”+” 进行了重载,所以就能适用于 int, float, double 类型的运算。
 
又如 ”<<“ C++ 的位运算中的位移运算符(左移),但在输出操作中又是与流对  cout  配合使用的流插入运算符, ”>>“ 也是位移运算符 ( 右移),但在输入操作中又是与流对象  cin  配合使用的流提取运算符。这就是运算符重载 (operator overloading) C++ 系统对 ”<<“ ”>>“ 进行了重载,用户在不同的场合下使用它们时,作用是不同  的。对 ”<<“ ”>>“ 的重载处理是放在头文件 stream 中的。因此,如果要在程序中用 ”<< “ ”>>” 作流插入运算符和流提取运算符,必须在本文件模块中包含头文件 stream( 当然还应当包括 ”using namespace std“)
现在要讨论的问题是:用户能否根据自己的需要对 C++ 已提供的运算符进行重载,赋予它们新的含义,使之一名多用。?
 

1 为什么会用运算符重载机制
       用复数类举例
      Complex c3 = c1 + c2;
      原因  Complex 是用户自定义类型,编译器根本不知道如何进行加减
       编译器给提供了一种机制,让用户自己去完成,自定义类型的加减操作。。。。。
       这个机制就是运算符重载机制
2  运算符重载的 本质是一个函数

#include  <iostream>
using  namespace  std ;
 
class  Complex
{
public :
     int  a ;
     int  b ;
public :
     Complex ( int  a= 0 ,  int  b= 0 )
     {
         this -> a  =  a;
         this -> b  =  b;
     }
     void  printCom()
     {
         cout<< a << "  +  "  <<  b  <<  "i"  <<endl;
     }
};
 
//1  定义了全局函数
Complex  myAdd( Complex  &c1,  Complex  &c2)
{
     Complex  tmp(c1. a  +  c2. a ,  c1. b +  c2. b );
     return  tmp;  //
}
 
//2  函数名  升级
Complex  operator+( Complex  &c1,  Complex  &c2)
{
     cout<< "12345上山  打老虎" <<endl;
     Complex  tmp(c1. a  +  c2. a ,  c1. b +  c2. b );
     return  tmp;  //
}
 
int  main()
{
      int  a  =  0 ,  b  =  0 ;
     int  c;
     c  =  a  +  b;  //1  基础数据类型  编译器已经知道了.  如何运算
 
     //  a  +  bi  复数运算规则
     Complex  c1( 1 ,  2 ),  c2( 3 ,  4 );
     Complex  c3;  //2    也是一种数据类型    用户自定义数据类型  C++编译器  是不知道如何进行运算
     //c3  =  c1  +  c2  ;
     //c1--;  --c1
 
     //3  c++编译器应该给我们程序员提供一种机制  ...
     //让自定义数据类型  有机会  进行  运算符操作  ====>  运算符重载机制
 
     //4  运算符重载机制
 
     //步骤1
     //Complex  c4  =  myAdd(c1,  c2);
     //c4.printCom();
 
     //步骤2    //Complex  c4    =  c1  +  c2
     //Complex  c4  =  operator+(c1,  c2);
     //c4.printCom();
 
     //步骤3
     Complex  c4  =  c1  +  c2;
     c4.printCom();
 
     //步骤3
     Complex  c4  =  c1  /  c2;
     c4.printCom();
 
     //总结:  1  运算符重载的本质    函数调用
 
     cout<< "hello..." <<endl;
     return  0 ;
}
 在成员函数里定义:
#include <iostream>
using  namespace  std;
class  complex{
public :
    complex();
    complex( double  real,  double  imag);
public :
     //声明运算符重载
    complex  operator +( const  complex &A)  const ;
     void  display()  const ;
private :
     double  m_real;   //实部
     double  m_imag;   //虚部
};
complex::complex(): m_real(0.0), m_imag(0.0){ }
complex::complex( double  real,  double  imag): m_real(real), m_imag(imag){ }
//实现运算符重载
complex complex:: operator +( const  complex &A)  const {
    complex B;
    B.m_real =  this ->m_real + A.m_real;
    B.m_imag =  this ->m_imag + A.m_imag;
     return  B;
}
void  complex::display()  const {
    cout<<m_real<< " + " <<m_imag<< "i" <<endl;
}
int  main(){
    complex c1(4.3, 5.8);
    complex c2(2.4, 3.7);
    complex c3;
    c3 = c1 + c2;
    c3.display();
     return  0;
}

在全局里定义:
#include <iostream>
using  namespace  std;
class  complex{
public :
    complex();
    complex( double  real,  double  imag);
public :
     void  display()  const ;
     //声明为友元函数
     friend  complex  operator +( const  complex &A,  const  complex &B);
private :
     double  m_real;
     double  m_imag;
};
complex  operator +( const  complex &A,  const  complex &B);
complex::complex(): m_real(0.0), m_imag(0.0){ }
complex::complex( double  real,  double  imag): m_real(real), m_imag(imag){ }
void  complex::display()  const {
    cout<<m_real<< " + " <<m_imag<< "i" <<endl;
}
//在全局范围内重载+
complex  operator +( const  complex &A,  const  complex &B){
    complex C;
    C.m_real = A.m_real + B.m_real;
    C.m_imag = A.m_imag + B.m_imag;
     return  C;
}
int  main(){
    complex c1(4.3, 5.8);
    complex c2(2.4, 3.7);
    complex c3;
    c3 = c1 + c2;
    c3.display();
 
     return  0;
}


 
所谓重载,就是重新赋予新的含义。 函数重载就是对一个已有的函数赋予新的含义,使之实现新功能,因此,一个函数名就可以用来代表不同功能的函数,也就是 一名多用
 
运算符也可以重载。实际上,我们已经在不知不觉之中使用了运算符重载。例如,大  家都已习惯于用加法运算符 ”+” 对整数、单精度数和双精度数进行加法运算,如 5+8   5.8 +3.67 等,其实计算机对整数、单精度数和双精度数的加法操作过程是很不相同的,  但由于 C++ 已经对运算符 ”+” 进行了重载,所以就能适用于 int, float, double 类型的运算。
 
又如 ”<<“ C++ 的位运算中的位移运算符(左移),但在输出操作中又是与流对  cout  配合使用的流插入运算符, ”>>“ 也是位移运算符 ( 右移),但在输入操作中又是与流对象  cin  配合使用的流提取运算符。这就是运算符重载 (operator overloading) C++ 系统对 ”<<“ ”>>“ 进行了重载,用户在不同的场合下使用它们时,作用是不同  的。对 ”<<“ ”>>“ 的重载处理是放在头文件 stream 中的。因此,如果要在程序中用 ”<< “ ”>>” 作流插入运算符和流提取运算符,必须在本文件模块中包含头文件 stream( 当然还应当包括 ”using namespace std“)
现在要讨论的问题是:用户能否根据自己的需要对 C++ 已提供的运算符进行重载,赋予它们新的含义,使之一名多用。?
 

1 为什么会用运算符重载机制
       用复数类举例
      Complex c3 = c1 + c2;
      原因  Complex 是用户自定义类型,编译器根本不知道如何进行加减
       编译器给提供了一种机制,让用户自己去完成,自定义类型的加减操作。。。。。
       这个机制就是运算符重载机制
2  运算符重载的 本质是一个函数

#include  <iostream>
using  namespace  std ;
 
class  Complex
{
public :
     int  a ;
     int  b ;
public :
     Complex ( int  a= 0 ,  int  b= 0 )
     {
         this -> a  =  a;
         this -> b  =  b;
     }
     void  printCom()
     {
         cout<< a << "  +  "  <<  b  <<  "i"  <<endl;
     }
};
 
//1  定义了全局函数
Complex  myAdd( Complex  &c1,  Complex  &c2)
{
     Complex  tmp(c1. a  +  c2. a ,  c1. b +  c2. b );
     return  tmp;  //
}
 
//2  函数名  升级
Complex  operator+( Complex  &c1,  Complex  &c2)
{
     cout<< "12345上山  打老虎" <<endl;
     Complex  tmp(c1. a  +  c2. a ,  c1. b +  c2. b );
     return  tmp;  //
}
 
int  main()
{
      int  a  =  0 ,  b  =  0 ;
     int  c;
     c  =  a  +  b;  //1  基础数据类型  编译器已经知道了.  如何运算
 
     //  a  +  bi  复数运算规则
     Complex  c1( 1 ,  2 ),  c2( 3 ,  4 );
     Complex  c3;  //2    也是一种数据类型    用户自定义数据类型  C++编译器  是不知道如何进行运算
     //c3  =  c1  +  c2  ;
     //c1--;  --c1
 
     //3  c++编译器应该给我们程序员提供一种机制  ...
     //让自定义数据类型  有机会  进行  运算符操作  ====>  运算符重载机制
 
     //4  运算符重载机制
 
     //步骤1
     //Complex  c4  =  myAdd(c1,  c2);
     //c4.printCom();
 
     //步骤2    //Complex  c4    =  c1  +  c2
     //Complex  c4  =  operator+(c1,  c2);
     //c4.printCom();
 
     //步骤3
     Complex  c4  =  c1  +  c2;
     c4.printCom();
 
     //步骤3
     Complex  c4  =  c1  /  c2;
     c4.printCom();
 
     //总结:  1  运算符重载的本质    函数调用
 
     cout<< "hello..." <<endl;
     return  0 ;
}
 在成员函数里定义:
#include <iostream>
using  namespace  std;
class  complex{
public :
    complex();
    complex( double  real,  double  imag);
public :
     //声明运算符重载
    complex  operator +( const  complex &A)  const ;
     void  display()  const ;
private :
     double  m_real;   //实部
     double  m_imag;   //虚部
};
complex::complex(): m_real(0.0), m_imag(0.0){ }
complex::complex( double  real,  double  imag): m_real(real), m_imag(imag){ }
//实现运算符重载
complex complex:: operator +( const  complex &A)  const {
    complex B;
    B.m_real =  this ->m_real + A.m_real;
    B.m_imag =  this ->m_imag + A.m_imag;
     return  B;
}
void  complex::display()  const {
    cout<<m_real<< " + " <<m_imag<< "i" <<endl;
}
int  main(){
    complex c1(4.3, 5.8);
    complex c2(2.4, 3.7);
    complex c3;
    c3 = c1 + c2;
    c3.display();
     return  0;
}

在全局里定义:
#include <iostream>
using  namespace  std;
class  complex{
public :
    complex();
    complex( double  real,  double  imag);
public :
     void  display()  const ;
     //声明为友元函数
     friend  complex  operator +( const  complex &A,  const  complex &B);
private :
     double  m_real;
     double  m_imag;
};
complex  operator +( const  complex &A,  const  complex &B);
complex::complex(): m_real(0.0), m_imag(0.0){ }
complex::complex( double  real,  double  imag): m_real(real), m_imag(imag){ }
void  complex::display()  const {
    cout<<m_real<< " + " <<m_imag<< "i" <<endl;
}
//在全局范围内重载+
complex  operator +( const  complex &A,  const  complex &B){
    complex C;
    C.m_real = A.m_real + B.m_real;
    C.m_imag = A.m_imag + B.m_imag;
     return  C;
}
int  main(){
    complex c1(4.3, 5.8);
    complex c2(2.4, 3.7);
    complex c3;
    c3 = c1 + c2;
    c3.display();
 
     return  0;
}


 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值