运算符重载

运算符重载为成员函数

重载为类成员的运算符函数定义形式

    函数类型  operator 运算符(形参)
    {
           ......
    }
    参数个数=原操作数个数-1   (后置++、--除外)

双目运算符重载规则

  • 如果要重载 B 为类成员函数,使之能够实现表达式 oprd1 B oprd2,其中 oprd1 为A 类对象,则 B 应被重载为 A 类的成员函数,形参类型应该是 oprd2 所属的类型。
  • 经重载后,表达式 oprd1 B oprd2 相当于 oprd1.operator B(oprd2)

复数类加减法运算重载为成员函数

  • 要求:
    • 将+、-运算重载为复数类的成员函数。
  • 规则:
    • 实部和虚部分别相加减。
  • 操作数:
    • 两个操作数都是复数类的对象。
  • 源代码:

    // 运算符重载.cpp: 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    
    //定义一个复数类
    class Complxe {
    public:
    	Complxe(double r = 0, double i = 0) :real(r), image(i) {};
    	//重载+运算
    	Complxe operator +(const Complxe &c2)const;
    	//重载-运算符
    	Complxe operator -(const Complxe &c2)const;
    	//显示函数
    	void display();
    private:
    	double real;
    	double image;
    	
    };
    
    //重载函数的实现
    Complxe Complxe::operator+(const Complxe &c2)const {
    //创造一个临时无名对象作为返回值
    	return Complxe(real + c2.real, image + c2.image);
    }
    Complxe Complxe::operator-(const Complxe &c2)const {
    	return Complxe(real - c2.real, image - c2.image);
    }
    void Complxe::display() {
    	cout << "("<< real << "," << image <<")"<< endl;
    }
    int main()
    {
    	Complxe c1(2, 5), c2(3, 6), c3;
    	cout << "c1="; c1.display();
    	cout << "c2="; c2.display();
    	c3 = c1 + c2;
    	cout << "c1+c2="; c3.display();
    	c3 = c1 - c2;
    	cout << "c1-c2="; c3.display();
    
        return 0;}

    前置单目运算符重载规则

    • 如果要重载 U 为类成员函数,使之能够实现表达式 U oprd,其中 oprd 为A类对象,则 U 应被重载为 A 类的成员函数,无形参。
    • 经重载后,表达式 U oprd 相当于 oprd.operator U()

    后置单目运算符 ++和--重载规则

    • 如果要重载 ++或--为类成员函数,使之能够实现表达式 oprd++ 或 oprd-- ,其中 oprd 为A类对象,则 ++或-- 应被重载为 A 类的成员函数,且具有一个 int 类型形参。
    • 经重载后,表达式 oprd++ 相当于 oprd.operator ++(0)

    重载前置++和后置++为时钟类成员函数

    • 前置单目运算符,重载函数没有形参
    • 后置++运算符,重载函数需要有一个int形参
    • 操作数是时钟类的对象。
    • 实现时间增加1秒钟



    #include "stdafx.h"
    #include<iostream>
    using namespace std;
    class Clock {
    private:
        int hour, minute, second;
    public:
        Clock(int h, int m, int s);
        //前置单目运算符
        Clock& operator ++();
        //后置单目运算符
        Clock operator ++(int);
    //显示时间的函数
        void showTime();
    };
    
    Clock::Clock(int h, int m, int s) {
        if (h >= 0 && h < 24 && m >= 0 && m < 60 && s >= 0 && s < 60)
        {
            this->hour = h;
            this->minute = m;
            this->second = s;
        }
        else {
            cout << "Time error!" << endl;
        }
        
    }
    Clock &Clock::operator++() {//operator穿引用,实现双向传递,类中的成员的值会发生变化
        second++ ;
        if (second >= 60)
        {
            minute++;
            second -=60 ;
            if (minute >= 60)
            {
                hour++;
                minute -= 60;
                    hour =(hour+1)%24 ;
            }
    
        }
        return *this;
    }
    Clock Clock::operator++(int) {
        Clock old = *this;//将当前构造的值传给对象old,在类中写一个默认构造函数?
        ++(*this);//调用前置运算符
        return old;
    }
    void Clock::showTime() {
        cout << hour << ":" << minute << ":" << second << endl;
    }
    int main()
    {
        //构造一个对象
        Clock c1(23, 59, 59);//23点59分59秒
        c1.showTime();
        (c1++).showTime();
        (++c1).showTime();
        return 0;
    }
    

    运算符重载为非成员函数

    有些运算符不能重载为成员函数,例如二元运算符的左操作数不是对象,或者是不能由我们重载运算符的对象

    运算符重载为非成员函数的规则

    • 函数的形参代表依自左至右次序排列的各操作数。
    • 重载为非成员函数时
    • 参数个数=原操作数个数(后置++、--除外)
    • 至少应该有一个自定义类型的参数。
    • 后置单目运算符 ++和--的重载函数,形参列表中要增加一个int,但不必写形参名。
    • 如果在运算符的重载函数中需要操作某类对象的私有成员,可以将此函数声明为该类的友元

    运算符重载为非成员函数的规则

    • 双目运算符 B重载后,

    表达式oprd1 B oprd2

    等同于operator B(oprd1,oprd2 )

    • 前置单目运算符 B重载后,

    表达式 B oprd

    等同于operator B(oprd )

    • 后置单目运算符 ++和--重载后,

    表达式 oprd B

    等同于operator B(oprd,0 )

     重载Complex的加减法和“<<”运算符为非成员函数

    • 将+、-(双目)重载为非成员函数,并将其声明为复数类的友元,两个操作数都是复数类的常引用。 • 将<<(双目)重载为非成员函数,并将其声明为复数类的友元,它的左操作数是std::ostream引用,右操作数为复数类的常引用,返回std::ostream引用,用以支持下面形式的输出:

    cout << a << b;
    

    该输出调用的是:

    operator << (operator << (cout, a), b);
    
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    class Complex {
    public:
    	Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) { }
    	friend Complex operator+(const Complex &c1, const Complex &c2);
    	friend Complex operator-(const Complex &c1, const Complex &c2);
    	friend ostream &operator <<(ostream &out,const Complex &c2);
    	
    private:
    	double real;  //复数实部
    	double imag;  //复数虚部
    };
    //++是类外成员,申明为类友元
    Complex operator+(const Complex &c1, const Complex &c2) {
    	return Complex(c1.real + c2.real, c1.imag + c2.imag);
    }
    Complex operator-(const Complex &c1, const Complex &c2) {
    	return Complex(c1.real - c2.real, c1.imag - c2.imag);
    }
    
    ostream & operator <<(ostream &out, const Complex &c2)//传引用,实现双向传递,这里ostream是一个类 
    {
    	out << "(" << c2.real << c2.imag << ")";
    	return out;
    
    }
    
    int main() {
    	Complex c1(5, 4), c2(2, 10), c3;
    	cout << "c1 = " << c1 << endl;
    	cout << "c2 = " << c2 << endl;
    	c3 = c1 - c2;   //使用重载运算符完成复数减法
    	cout << "c3 = c1 - c2 = " << c3 << endl;
    	c3 = c1 + c2;   //使用重载运算符完成复数加法
    	cout << "c3 = c1 + c2 = " << c3 << endl;
    	return 0;
    }
    
    
    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值