operator重载的使用

C++的大多数运算符都可以通过operator来实现重载。

 

简单的operator+

#include <iostream>
using namespace std;

class A
{
public:
    A(int x){i=x;}
    int i;
    A &operator+(A &p)
    {
        this->i+=p.i;
        return *this;
    }
};

void main()
{
    A a(10),b(20),c(0);
    c=a+b;
    cout<<c.i<<endl;
    system("pause");
}

 

简单的operator++

#include <iostream>
using namespace std;

class A
{
public:
    A(int x){i=x;}
    int i;
    A &operator++()
    {
        this->i++;
        return *this;
    }
};

void main()
{
    A a(10);
    a++;
    cout<<a.i<<endl;
    system("pause");
}

 

深层operator=

#include <iostream>
using namespace std;

class A
{
public:
    A(int x){i=new int;*i=x;}
    ~A(){delete i;i=0;}
    A(const A&p)
    {
        i=new int;
        *i=*(p.i);
    }
    int *i;
    A &operator=(A &p)
    {
        *i=*(p.i);
        return *this;
    }
    int pp(){return *i;}
};

void main()
{
    A a(10),b(0);
    a=b;
    cout<<a.pp()<<endl;
    system("pause");
}

 

简单的输出运算符operator<<

注意:由于ostream类没有公有的复制构造函数,因此函数无法调用该类的复制构造函数来复制对象,必须按照引用的方式来接收与返回ostream对象。

#include <iostream>
using namespace std;

class A
{
public:
    A(int x,int y){rx=x;ry=y;}
    int rx;
    int ry;
};

ostream &operator << (ostream &s,const A &c)//cout是ostream的对象
{
    s<<c.rx;//使用cout输出对象的值,语句可以直接翻译成cout<<c.rx
    s<<c.ry;//使用cout输出对象的值,语句可以直接翻译成cout<<c.ry
    return s;//返回cout
}

void main()
{
    A a(3,4),b(5,6);
    cout<<a<<b;//(ostream &s,const A &c)==(cout,a)
    system("pause");
}

 

 

简单的输出运算符-使用友元的方式operator<<

#include <iostream>
using namespace std;

class A
{
public:
    A(int x,int y){rx=x;ry=y;}
    friend ostream &operator << (ostream &s,const A &c)
    {
        s<<c.rx;
        s<<c.ry;
        return s;
    }
private:
    int rx;
    int ry;
};

void main()
{
    A a(3,4),b(5,6);
    cout<<a<<b;
    system("pause");
}

 

operator++(++i与i++的实现)

#include <iostream>
using namespace std;

class A
{
public:
    A(int x){rx=x;}
    int operator++()          //++i
    {                        
        this->rx++;           //其实这里this指针可以不用写因为你直接写rx++效果也是一样的,编译器会在编译的时候自动的为你加上this
        return rx;            //但是刚开始学语言的时候建议写一下可以加深this指针的概念
    }
    int operator++(int)       //i++
    {
        int i=0;
        i=this->rx;
        this->rx++;
        return i;
    }
    friend ostream &operator << (ostream &s,const A &c)
    {
        s<<c.rx;
        return s;
    }
private:
    int rx;
};

void main()
{
    A a(0),b(0);
    cout<<a++<<endl;
    cout<<++b<<endl;
    system("pause");
}

 

operator输入输出,自增运算符

#include <iostream>
using namespace std;

class A
{
public:
    A(int x){rx=x;}
    int operator++()        //++i
    {                        
        this->rx++;            //其实这里this指针可以不用写因为你直接写rx++效果也是一样的,编译器会在编译的时候自动的为你加上this
        return rx;            //但是刚开始学语言的时候建议写一下可以加深this指针的概念
    }
    int operator++(int)        //i++
    {
        int i=0;
        i=this->rx;
        this->rx++;
        return i;
    }
    friend istream &operator >> (istream &s,A &c)        //输入操作符重载
    {
        s>>c.rx;
        return s;
    }
    friend ostream &operator << (ostream &s,const A &c)  //输出操作符重载
    {
        s<<c.rx;
        return s;
    }
private:
    int rx;
};

void main()
{
    A a(0),b(0);
    cin>>a;
    cin>>b;
    cout<<a++<<endl;
    cout<<++b<<endl;
    system("pause");
}

 

posted on 2015-03-30 19:41  小咒  阅读(303)  评论(0编辑  收藏  举报

导航