典型运算符重载

一.重载++ 与- -

在这里插入图片描述
例 成员函数重载++

#include<iostream>
using namespace std;
class Increase
{
public :
    Increase ( )
    {
        value=0;
    }
    void display( ) const
    {
        cout<<value<<'\n';
    } ;
    Increase operator ++ ( ) ;
    Increase operator ++ ( int ) ;
private:
    unsigned value ;
};
Increase Increase :: operator ++ ( )
{
    value ++ ;
    return *this ;
}
Increase Increase :: operator ++ ( int )
{
    Increase temp;
    temp.value = value ++ ;
    return temp;
}
int main( )
{
    Increase a, b, n ;
    int i ;
    for ( i = 0 ; i < 10 ; i ++ )
        a = n ++ ;
    cout <<"n= " ;
    n.display( ) ;
    cout <<"a= " ;
    a.display( ) ;
    for ( i = 0 ; i < 10 ; i ++ )
        b = ++ n ;
    cout << "n= " ;
    n.display( ) ;
    cout << "b= " ;
    b.display( ) ;
}

例 友元函数重载++

#include<iostream>
using namespace std;
class Increase
{
public :
    Increase ( )
    {
        value=0;
    }
    void display( ) const
    {
        cout<<value<<'\n';
    } ;
    friend Increase operator ++ ( Increase & ) ; // 前置
    friend Increase operator ++ ( Increase &, int ) ;
private:
    unsigned value ;
};
Increase operator ++ ( Increase & a )
{
    a.value ++ ;
    return a ;
}
Increase operator ++ ( Increase & a, int )
{
    Increase temp(a);
    a.value ++ ;
    return temp;
}
int main( )
{
    Increase a, b, n ;
    int i ;
    for ( i = 0 ; i < 10 ; i ++ )
        a = n ++ ;
    cout <<"n= " ;
    n.display( ) ;
    cout <<"a= " ;
    a.display( ) ;
    for ( i = 0 ; i < 10 ; i ++ )
        b = ++ n ;
    cout << "n= " ;
    n.display( ) ;
    cout << "b= " ;
    b.display( ) ;
}

二.重载赋值运算符

赋值运算符重载用于对象数据的复制
operator= 必须重载为成员函数
重载函数原型为 类名 & 类名 :: operator= (const 类名& ) ;

#include<iostream>
#include<cstring>
using namespace std;
class Name
{
public :
    Name ( char *pN ) ;
    Name( const Name & Obj ) ;
    Name& operator=( const Name &Obj ) ;
    ~ Name() ;
protected :
    char *pName ;
    int size ;
} ;
Name::Name ( char *pN )
{
    cout <<" Constructing " << pN << endl ;
    pName = new char[ strlen( pN ) + 1 ] ;
    if( pName != 0 )
        strcpy( pName,pN ) ;
    size = strlen( pN ) ;
}
Name::Name( const Name & Obj )
{
    cout << " Copying " << Obj.pName << " into its own block\n";
    pName = new char[strlen( Obj.pName ) + 1 ] ;
    if ( pName != 0 )
        strcpy( pName, Obj.pName ) ;
    size = Obj.size;
}
Name & Name::operator= ( const Name & Obj )

{
    cout << " reload "<<this->pName<<"="<<Obj.pName<<endl;
    if(this == &Obj)
        return *this;
    delete []pName;
    pName = new char[ strlen( Obj.pName ) + 1 ] ;
    if ( pName != 0 )
        strcpy( pName, Obj.pName ) ;
    size = Obj.size ;
    return *this ;
}
Name::~ Name()
{
    cout << " Destructing " << pName << endl ;
    delete []pName ;
    size = 0;
}
int main()
{
    Name Object1( "lily" ) ;
    Name Object2 = Object1 ;
    Name Object3( "NoName" ) ;
    Object3 = Object2 = Object1 ;
}

三. 重载运算符[]和( )

1.二者是二元运算符
2.只能用成员函数重载,不能用友元函数重载

在重载下标运算符函数时应该注意:
(1) 该函数只能带一个参数,不可带多个参数。
(2) 不得重载为友元函数,必须是非static的成员函数。
在这里插入图片描述在这里插入图片描述

四.重载流插入和流提取运算符

1.istreamostream 是C++ 的预定义流类
2. cin 是istream 的对象,cout 是ostream 的对象
3.运算符 << 由ostream 重载为插入操作,用于输出基本类型数据
4.运算符 >> 由 istream 重载为提取操作,用于输入基本类型数据
5.用友元函数重载 << 和 >> ,输出和输入用户自定义的数据类型
在这里插入图片描述
在这里插入图片描述

五.重载关系运算符

可以重载关系运算符,重载后的关系运算符用于比较类的对象。
下面的实例演示了如何重载< 运算符:

bool operator <(const Distance& d)
 { //重载小于运算符(< )
if(feet < d.feet) { return true; }
if(feet == d.feet && inches < d.inches) { return true; }
return false;
}

类似地,可以重载其他的关系运算符。
例,

#include <iostream>
#include<iostream>
using namespace std;
class Distance
{
private:
    int feet, inches;
public:
    Distance(int f=0, int i=0)
    {
        feet = f;
        inches = i;
    }
    void displayDistance()
    {
        cout << feet << inches <<endl;
    }
    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;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值