zoukankan      html  css  js  c++  java
  • 运算符重载

    /*  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

  • 相关阅读:
    个人总结21
    个人总结08
    个人总结07
    构建之法读后感01
    学习进度表 03
    四则运算3
    求最大值
    学习进度表02
    四则运算 2
    学习进度表01
  • 原文地址:https://www.cnblogs.com/lzh-Linux/p/3480086.html
Copyright © 2011-2022 走看看