Complex复数类运算符重载(类的成员函数实现)

一、复数类运算符重载

《1》分类:
在c++中,有些运算符可以重载,有些不可以重载,详情见下图:
这里写图片描述
那么,一般用的比较多的有+、-、*、/、=、前置++,- -、后置++,- -、<<,故而,今天,我们将来实现这些操作,以复数类为例:
《2》方法
我们知道,在c++中,类的成员函数与全局函数可以互相转换,全局函数转为类的成员函数时,会将参数列表中的第一个参数隐藏起来,用一个隐式的this指针来代替,类的成员函数转全局函数时,过程相反,为了保证类的私有成员变量可以被该全局函数正常访问,需要在该类中声明该全局函数为其友元函数,本文着重谈类的成员函数来实现运算符的重载。
《3》特殊运算符
1、不可以使用全局函数以及其对应的友元函数来重载的操作符有 =、 ()、 [ ]、 ->、
2、必须使用全局函数以及其对应的友元函数来重载的操作符为 <<

《4》代码实现

//类实现+、-、*、/、=、前置++、--、后置++、--
#include<iostream>
using namespace std;
class Complex
{
private://私有权限
    int a;
    int b;
    //friend Complex operator+(Complex &c1,Complex&c2);//成员变量为私有,借助友元函数来操作
public:
    Complex(int a = 0,int b = 0)
    {
        this->a = a;
        this->b = b;
    }
    void printCom()
    {
        cout<<a<<"+"<<b<<"i"<<endl;
    }
//实现类的-操作

Complex operator-(Complex &c2)
{
        Complex tmp;

        tmp.a = this->a - c2.a ;
        tmp.b = this->b -c2.b ;

        return tmp;
}

Complex operator+(Complex&c2)
{
    Complex tmp;
    tmp.a = a +c2.a;
    tmp.b = b +c2.b;
    return tmp;
}
Complex operator*(Complex &c2)
{
    Complex tmp ;
    tmp.a = a*c2.a ;
    tmp.b = b*c2.b ;
    return tmp;
}

Complex operator/(Complex &c2)
{
    Complex tmp;
    tmp.a = a / c2.a ;
    tmp.b = b / c2.b ;
    return tmp;

}

Complex operator=(Complex &c2)
{
    Complex tmp;

    tmp.a = c2.a ;
    tmp.b = c2.b ;
    return tmp;
}
Complex& operator++()//前置++
{
    this->a++;
    this->b++;
    return *this;
}

Complex& operator--()//前置--
{
    this->a--;
    this->b--;
    return *this;
}
//后置:先使用属性,再操作自身
Complex operator++(int)//后置++
{
   Complex tmp = *this;
    tmp.a++;
    tmp. b++;
    return tmp ;
}

Complex operator--(int)//后置--
{
   Complex tmp = *this;
    tmp.a--;
    tmp. b--;
    return tmp ;
}
};

//运算符重载是函数,需要写函数原型
//写出函数调用语句
//完善函数原型
int main()
{
    Complex c1(1,2),c2(3,4);
    Complex c3 = c1+ c2;
    //Complex c4 = operator-(c2)
    Complex c4 =c1 - c2;
    Complex c5 = c1 * c2;
    Complex c6 = c1 / c2;
    Complex c7 = c1; 

    ++c1;
    c1.printCom();//2+3i
    --c1;
    c1.printCom();//1+2i
    cout<<"\n";

    c1++;
    c1.printCom();//2+3i
    c1--;
    c1.printCom();//1+2i
    cout<<"\n";

    c1.printCom();//1+2i
    c2.printCom();//3+4i
    c3.printCom();//4+6i
    c4.printCom();//-2+-2i
    c5.printCom();//3+8i
    c6.printCom();//0+0i
    c7.printCom();//1+2i

    return 0;
}

《5》实验结果
这里写图片描述

《6》运算符<<的重载实现链式编程

class Complex
{
private://私有权限
    int a;
    int b;
    //friend Complex operator+(Complex &c1,Complex&c2);//成员变量为私有,借助友元函数来操作
        friend ostream& operator<<(ostream &out ,Complex &c1);
public:
    Complex(int a = 0,int b = 0)
    {
        this->a = a;
        this->b = b;
    }
    void printCom()
    {
        cout<<a<<"+"<<b<<"i"<<endl;
    }
};


ostream& operator<<(ostream &out ,Complex &c1)
{
    cout<<"12345王代文"<<endl;
    cout<<c1.a <<"+"<<c1.b<<"i"<<endl;
    return out;
}

int main()
{
    Complex c1(1,2),c2(3,4);
    int a = 10;
    char *p = "abc";
    cout<<a<<endl;
    cout<<p<<endl;

    cout<<c1;
    //cout<<c1<<endl;//或者cout<<c1<<"aaa";这都是错误的,原因是cout<<c1;是一个返回值,当左值不可,除非重载
    cout<<c1<<"链式编程测试"<<endl;
    return 0;
}

这里写图片描述

《7》复数类运算符重载(+、-、*、/、=、前置++,–、后置++,–、<<)全局函数实现见下一篇博客;

猜你喜欢

转载自blog.csdn.net/dai_wen/article/details/80245987
今日推荐