运算符重载——C++(26、27)

运算符重载

定义

  • 所谓重载,就是重新赋予新的含义,函数重载是对一个已有的函数赋予新的含义。

方法

  • 运算符重载的方法是定义一个重载运算符的函数,在需要执行被重载的运算符时,系统就自动调用该运算符。也就是说,运算符重载是通过定义函数实现的,运算符重载实质是是函数的重载。

格式

函数类型 operator 运算符名称 (形参列表)
{
	  对运算的重载处理
}

例如: 把加号运算符重载成乘号运算符

int operator + (int a,int b)
{
	return(a*b);
}

TIPS

  • 运算符重载函数除了可以作为类的成员函数外,还可以是非成员函数: 放在类外,作为友元函数存在。(不推荐)(由于友元的使用会破坏类的封装,因此从原则上说,要尽量讲运算符函数作为成员函数。)
  • 去重载一个操作符,应该只有在必要的时候,比如实现一种新的数据类型时,才重载操作符。
  • 重载操作符的目的是为了让代码更容易阅读和理解。

代码实现

1/2 复数的加减乘除

#include<iostream>

using namespace std;

class Complex
{
public:
    Complex();
    Complex(double r,double i);
    void printf();
    Complex operator +(Complex &in)
    {
        Complex temp;
        temp.imag=imag+in.imag;
        temp.real=real+in.real;
        return temp;
    }
    Complex operator -(Complex &in)
    {
        Complex temp;
        temp.imag=imag-in.imag;
        temp.real=real-in.real;
        return temp;
    }
    Complex operator *(Complex &in)
    {
        Complex temp;
        temp.imag=imag*in.real+in.imag*real;
        temp.real=real*in.real-imag*in.imag;
        return temp;
    }
    Complex operator /(Complex &in)
    {
        Complex temp;
        temp.imag=(imag*in.real-real*in.imag)/((in.real*in.real)+(in.imag*in.imag));
        temp.real=(real*in.real+imag*in.imag)/((in.real*in.real)+(in.imag*in.imag));
        return temp;
    }

private:
    double real;
    double imag;

};
void Complex::printf()
{
    if(real==0)
    {
        cout << "   "<<imag <<  "i" << endl;
    }
    else if(imag==0)
    {
        cout << "   "<< real << endl;
    }
    else if(imag>0)
    cout << "( " << real << "+" << imag << "i ) " << endl;
    else
    cout << "( " << real << imag << "i ) " << endl;

}
Complex::Complex()
{
    real=0;
    imag=0;
}
Complex::Complex(double r,double i)
{
    real=r;
    imag=i;
}

int main()
{
    Complex a(3,1);
    Complex b(1,-1);
    Complex c,d,e,f;
    c=a+b;
    d=a-b;
    e=a*b;
    f=a/b;
    a.printf();   cout << "加上" << endl;   b.printf();  cout << "的结果为" <<endl;  c.printf();cout << "\n" << endl;
    a.printf();   cout << "减去" << endl;   b.printf();  cout << "的结果为" <<endl;  d.printf();cout << "\n" << endl;
    a.printf();   cout << "乘上" << endl;   b.printf();  cout << "的结果为" <<endl;  e.printf();cout << "\n" << endl;
    a.printf();   cout << "除去" << endl;   b.printf();  cout << "的结果为" <<endl;  f.printf();


    return 0;
}

2/2 分数的加减乘除

#include <iostream>
#include <cmath>
using namespace std;

class Fenshu
{
public:
    Fenshu();
    Fenshu(int num_in,int denum_in);
    void print();
    Fenshu operator +(Fenshu &in)
    {
        Fenshu temp;
        temp.num=num*in.denum+denum*in.num;
        temp.denum=in.denum*denum;
        return temp;
    }
    Fenshu operator -(Fenshu &in)
    {
  /*      num=-num;                 //法1:不好用:会改变num 的值,后面不好输出
        return operator +(in);*/
        Fenshu temp;
        temp.num=(-num)*in.denum+denum*in.num;
        temp.denum=in.denum*denum;
        return temp;    }
    Fenshu operator *(Fenshu &in)
    {
        Fenshu temp;
        temp.num=num*in.num;
        temp.denum=in.denum*denum;
        return temp;
    }
    Fenshu operator /(Fenshu &in)
    {
        Fenshu temp;
        temp.num=num*in.denum;
        temp.denum=in.denum*num;
        return temp;
    }
private:
    int num;
    int denum;
    void normalize();
};

Fenshu::Fenshu()
{
    num=0;
    denum=0;
}
Fenshu::Fenshu(int num_in,int denum_in)
{
    num=num_in;
    denum=denum_in;
    normalize();
}
//normalize()对分数进行简化操作包括:
//1.只允许分子为负数,如果分母为负数则把负数挪到分子部分,如1/-2==-1/2
//2.利用欧几里德算法(辗转求余原理)将分数进行简化:2/10 => 1/5
void Fenshu::normalize()
{
    if(denum<0)
    {
        num=-num;
        denum=-denum;
    }
                    //用欧几里得算法求最大公约数:a
    int a=abs(num);
    int b=abs(denum);
    while(b>0)
    {
        int t=a%b;
        a=b;
        b=t;
    }
    num/=a;
    denum/=a;
}
void Fenshu::print()
{
    normalize();

    if(num==0)                       //分子是0,结果就是0
    {
        cout << "0" << endl;
    }
    else if(denum==1)
    {
        cout << num << endl;
    }
    else

    cout << num << "/" << denum << endl;
}

int main()
{
    Fenshu a(2,4),b(1,2),c;
    c=a+b;
    a.print();
    cout << "加上" << endl;
    b.print();
    cout << "的结果为:" << endl;
    c.print();

    a.print();
    cout << "减去" << endl;
    b.print();
    cout << "的结果为:" << endl;
    c=a-b;
    c.print();

    c=a*b;
    a.print();
    cout << "乘上" << endl;
    b.print();
    cout << "的结果为:" << endl;
    c.print();

    c=a/b;
    a.print();
    cout << "除去" << endl;
    b.print();
    cout << "的结果为:" << endl;
    c.print();
    
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

la via

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值