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

    /*运算符重载:
    * 1.
    * 全局函数 完成 +操作符 重载(又叫友元函数重载)
    * Complex operator+(Complex &c1, Complex &c2)
    * 类成员函数 完成 -操作符 重载
    * Complex operator-(Complex &c2)
    *
    * 区别:成员函数中有this指针。。友元函数中没有this指针
    *2.二元运算符重载
    * 成员函数重载:
    * ObjectA op ObjectB
    * 重载成员函数,解释为:
    * ObjectA.operator op(ObjectB)
    * 左操作数由ObjectA通过this指针传递,右操作数由参数ObjectB传递
    * 重载为友元函数,解释为:
    * operator op(ObjectA, ObjectB)
    * 左右操作数都由参数传值
    *3.一元运算符重载
    * 成员函数重载:
    * ObjectA op 或 op ObjectA
    * 重载成员函数,解释为:
    * ObjectA.operator op()
    * 操作数由对象通过this指针隐式传递
    * 重载为友元函数,解释为:
    * operator op(ObjectA)
    * 操作数由参数表的参数ObjectA提供
    *4.前置和后置运算符总结
    * c++通过一个占位符来区分前置运算和后置运算的区别
    * 前置
    * Cal & Cal::operator++() //重载成员函数 Object.operator ++()
    * Cal & Cal::operator--() //重载成员函数 Object.operator --()
    * friend Cal & operator++(Cal &cal) //重载友元函数 operator ++(Object)
    * friend Cal & operator--(Cal &cal) //重载友元函数 operator --(Object)
    * 后置
    * Cal Cal::operator++(int) //重载成员函数 Object.operator ++(0)
    * Cal Cal::operator--(int) //重载成员函数 Object.operator --(0)
    * friend Cal operator++(Cal &cal,int) //重载友元函数 operator ++(Object,0)
    * friend Cal operator--(Cal &cal,int) //重载友元函数 operator --(Object,0)
    *5.一般的运算符重载常用重载成员函数, 重载友元函数常用于运算符的左右操作数类型不同的情况。
    *6.operator= 必须重载为成员函数
    *  重载函数原型为:类型 & 类名 :: operator= ( const 类名 & ) ;
    *
    * */

    #include <iostream>
    using namespace std;
    class Cal {
    public:
    Cal(int a, int b) :
    a(a), b(b) {
    }
    //二元运算符重载
    Cal operator +(int x) const;
    Cal operator -(const Cal &b) const;
    friend Cal operator *(const Cal &a, const Cal &b);
    //一元运算符重载
    Cal& operator --(); //前置--
    friend Cal& operator ++(Cal &cal); //前置++
    //后置
    Cal operator --(int); //后置--
    friend Cal operator ++(Cal &cal, int);//后置++
    void print() {
    cout << "a=" << a << " b=" << b << endl;
    }
    private:
    int a;
    int b;
    };
    Cal Cal::operator +(int x) const {
    Cal res(0, 0);
    res.a = a + x;
    return res;
    }
    Cal Cal::operator -(const Cal &b) const {
    Cal res(0, 0);
    res.b = this->b - b.b;
    return res;
    }
    Cal operator *(const Cal &a, const Cal &b) {
    Cal res(0, 0);
    res.a = a.a * b.a;
    return res;
    }
    Cal& Cal::operator --() {
    a--;
    b--;
    return *this;
    }
    Cal& operator ++(Cal &cal) {
    cal.a++;
    cal.b++;
    return cal;
    }
    Cal Cal::operator --(int) {
    /*因为后置--,先返回值,再--
    * return *this
    * this.a--
    * this.b--
    * 所以按下面写
    */
    Cal tmp(0,0);
    tmp = *this;
    this->a--;
    this->b--;
    return tmp;
    }
    Cal operator ++(Cal &cal, int){
    //原理同后置--
    Cal tmp = cal;
    cal.a++;
    cal.b++;
    return tmp;
    }
    class friendClass{
    private :
    int x;
    public:
    friendClass(int x){
    this->x = x;
    }
    //friend void operator <<(ostream &os, friendClass &fc);
    friend ostream& operator <<(ostream &os,friendClass &fc);
    };
    //void operator <<(ostream &os, friendClass &fc){
    //    cout<<"hello world"<<endl;
    //    cout<<"x="<<fc.x<<endl;
    //}
    ostream& operator <<(ostream &os,friendClass &fc){
    //ostream out = os;
    cout<<"hello world1111111"<<endl;
    cout<<"x="<<fc.x<<endl;
    return os;
    }
    int main() {
    Cal a(1, 9);
    Cal b(3, 1);
    Cal c(0, 0);
    c = a + 8; //等价于 c = a.operator+(8)
    c.print();
    c = a - b; //等价于 c = a.operator-(b)//重载为成员函数
    c.print();
    c = a * b; //等价于 c = operator*(a,b) 重载为友元函数
    c.print();
    cout<<"+++++++++++++++++"<<endl;
    //前置运算符
    c = --a; //等价于 c = a.operator--()
    c.print();
    c = ++b; //等价于c = operator++(b)
    c.print();
    a--;
    a.print();
    cout<<"+++++++++++++++++"<<endl;
    //后置++和后置--测试
    Cal tmp(0,0);
    Cal d(1,1);
    d.print();
    tmp = d--;
    tmp.print();
    d.print();
    Cal f(2,2);
    f.print();
    tmp = f++;
    tmp.print();
    f.print();
    friendClass fc(5);
    //cout<<fc;
    cout<<fc<<"2222"<<endl;
    return 0;
    
    }
  • 相关阅读:
    Activex、OLE、COM、OCX、DLL之间有什么区别?
    如何通过VC的 CHttpFile 抓取网页内容
    RPC远程过程调用实例详解(转)
    C++中extern “C”含义深层探索
    Android NDK Development ---- Android 4.4
    Android NDK Overview ---- Android 4.4
    Android NDK How-To ---- Android 4.4
    使用Jquery+EasyUI框架开发项目+下载+帮助--EasyUI的简介
    zzzzw_在线考试系统③完结篇
    一个月的时间--java从一无所有到能用框架做点东西出来
  • 原文地址:https://www.cnblogs.com/w-which/p/7217328.html
Copyright © 2011-2022 走看看