运算符重载

/*  1.只能重载c++提供的标准运算符

   2.参数个数固定

   3.针对类的对象操作

运算符的重载形势只有两种: 一种是重载为类的友元行数

特殊运算符的重载 “--” “++”   等等

 http://c.chinaitlab.com/special/czhenyan/Index.html

*/

#include<iostream>
using namespace std;
class  base
{
public:
    int a1;
    float b1;
    base()
    {
        a1=0;
        b1=0.0;
        cout<<"constructing base "<<endl;
    }
    base(int a,float b)
    {
        a1=a;
        b1=b;
        cout<<"constructing base "<<endl;
       
    }
    ~base()
    {
        cout<<"desconstructing base"<<endl;
    }
    base operator +(int c);
};
base base::operator +(int c)
{
    a1+=c;
    b1+=(float)c;
    cout<<"operator + success"<<endl;
    return *this;
}
int main(int argc,char **argv)
{
    base o1(1,1.0),oo;
    oo=o1+5;// 隐式调用
    oo=o1.operator+(5);//显式调用
    cout<<"   "<<oo.a1<<"     "<<oo.b1<<endl;
   
}image

posted @ 2013-12-18 12:31  Android开发8585  阅读(307)  评论(0编辑  收藏  举报