C++(6)单目运算符和特殊运算符的重载

332 阅读9分钟

运算符的重载

原理和机制

C++中运算符只支持基本数据类型运算,如果需要运算符支持类类型的运算,需要使用C++提供的新语法 ------- 运算符的重载

运算符的重载本质上是通过函数来实现的,将类类型数据的运算过程写成一个特殊的函数,当该类型的对象遇到这种运算时自动调用该函数来完成运算过程。

实现运算符重载的函数既可以是成员函数,也可以是全局函数。

双目运算符的重载

通过成员函数重载(以加法为例)

成员函数加法的重载: 本类类型 operator+(第二操作数){...} //xxx + yyy xxx是第一操作数 ,yyy 是第二操作数 //第一操作数就是调用该函数的对象


/*08-双目运算符的重载*/
#include <iostream>

using namespace std;

//分数
class Fraction {
public:
    Fraction(int x = 0, int y = 1) :x(x), y(y) {}
    //打印
    void print()
    {
        cout << this->x << "/" << this->y << endl;
    }
    Fraction operator+(const Fraction  &fb) {   //不传引用,会拷贝,效率问题 
        cout << "  NO-1 " << endl;
        // 1/2+ 1/3 = 5/6
      return  Fraction(this->x * fb.y + fb.x * this->y/*分子*/, this->y * fb.y/*分母*/);     
    }

private:
    int x;
    int y;
};
    int main()
    {
        Fraction fa(1, 2);
        Fraction fb(1, 3);

        Fraction fc = fa + fb;//fa.operator+(fb);   operator+(fa,fb);
        fc.print();


        return 0;
    }

通过全局函数重载(以加法为例)

全局函数加法的重载: 操作数的类型 operator+(第一操作数,第二操作数){...}

/*08-双目运算符的重载*/
#include <iostream>

using namespace std;

//分数
class Fraction {
public:
    Fraction(int x = 0, int y = 1) :x(x), y(y) {}
    //打印
    void print()
    {
        cout << this->x << "/" << this->y << endl;
    }
    Fraction operator+(const Fraction  &fb) {   //不传引用,会拷贝,效率问题 
        cout << "  NO-1 " << endl;
        // 1/2+ 1/3 = 5/6
      return  Fraction(this->x * fb.y + fb.x * this->y/*分子*/, this->y * fb.y/*分母*/);     
    }
    friend Fraction operator+(const Fraction& fa, const Fraction& fb);   //设置友元可以
private:
    int x;
    int y;
};
Fraction operator+(const Fraction& fa,const Fraction& fb) {   //不传引用,会拷贝,效率问题 
    cout << "  NO-2 " << endl;
    // 1/2+ 1/3 = 5/6
    return  Fraction(fa.x * fb.y + fb.x * fa.y/*分子*/, fa.y * fb.y/*分母*/);
}
    int main()
    {
        Fraction fa(1, 2);
        Fraction fb(1, 3);

        Fraction fc = fa + fb;//fa.operator+(fb);   operator+(fa,fb);
        fc.print();


        return 0;
    }

//如果类外重载无法访问类中的成员,可以使用以下方法 //1.直接将成员设置为公有(不推荐) //2.使用友元 //3.为私有成员提供访问接口

练习:

使用成员函数实现Fraction的减法,使用全局函数实现Fraction的乘除

//使用成员函数实现Fraction的减法,使用全局函数实现Fraction的乘除

/*08-双目运算符的重载*/
#include <iostream>

using namespace std;

//分数
class Fraction {
public:
    Fraction(int x = 0, int y = 1) :x(x), y(y) {}
    //打印
    void print()
    {
        cout << this->x << "/" << this->y << endl;
    }
    Fraction operator-(const Fraction  &fb) {   //不传引用,会拷贝,效率问题 
        cout << " " << endl;
        // 1/2+ 1/3 = 5/6
      return  Fraction(this->x * fb.y - fb.x * this->y/*分子*/, this->y * fb.y/*分母*/);     
    }
    friend Fraction operator*(const Fraction& fa, const Fraction& fb);   //设置友元可以
    friend Fraction operator/(const Fraction& fa, const Fraction& fb);
private:
    int x;
    int y;
};
Fraction operator*(const Fraction &fa,const Fraction &fb) {   //不传引用,会拷贝,效率问题 

    // 1/2 * 1/3 = 5/6
    return  Fraction((fa.x * fb.x)/*分子*/, (fa.y * fb.y)/*分母*/);
}
Fraction operator/(const Fraction& fa, const Fraction& fb) {   //不传引用,会拷贝,效率问题 
    //除以一个数等于乘以一个数的倒数
    // 1/2 * 1/3 = 5/6
    return  Fraction((fa.x * fb.y)/*分子*/, (fa.x * fb.y)/*分母*/);
}
 int main()
    {
        Fraction fa(1, 2);
        Fraction fb(1, 3);

        Fraction fc = fa - fb;//fa.operator-(fb);   operator-(fa,fb);
        Fraction fd = fa * fb;
        Fraction ff = fa / fb;
        fc.print();
        fd.print();
        ff.print();

        return 0;
    }

作业:

完成以下笔试题

(1)没有虚函数,不支持多态,不能用父类类型指向子类对象

(2)分配new [] ,释放应该用delete []

(3)没有虚函数,不支持多态,不能用父类类型指向子类对象

修改实参不影响形参,给NULL指针拷贝数据,段错误

(4)不能返回局部数组的地址,得到是野指针,使用结果不确定

构造一个类表示复数(Complex) -------- a+bi

重载+ - * 写出加减乘除

double real;
double imag;
C1 = 2+3i
C2 = 5-4i
C1+C2 = 7-i
C1-C2 = -3+7i
C1*C2 = 10 - 8i + 15i + 12 = 22+7i
 /*08-双目运算符的重载*/
#include <iostream>
using namespace std;

class Complex {
public:
    Complex(int real = 0, int imag = 0) :real(real), imag(imag) {}
    //打印
    void print()
    {
        cout << this->real << (this->imag>=0 ? " +":"") << this->imag << "i" << endl;
    }
    friend  Complex operator+(const Complex& n1, const Complex& n2);
    friend  Complex operator-(const Complex& n1, const Complex& n2);
    friend  Complex operator*(const Complex& n1, const Complex& n2);
    friend  Complex operator/(const Complex& n1, const Complex& n2);
    //-----------------------------------------
    // 如果必须要在类内定义的话,只能定义为单参数的运算符函数: friend  Num operator+(const Num& n1);
    // 如果定义为双参数的运算符函数,只能在类外定义
    //-------------------------------------

private:
    int real;
    int imag;
};
Complex operator+(const Complex& n1, const Complex& n2) {

    return  Complex(n1.real + n2.real, n1.imag + n2.imag);
}
Complex operator-(const Complex& n1, const Complex& n2) {

     return  Complex(n1.real - n2.real, n1.imag - n2.imag);
 }
Complex operator*(const Complex& n1, const Complex& n2) {

     return  Complex(n1.real * n2.real - n1.imag * n2.imag, n1.real * n2.imag+ n1.imag * n2.real);
 }

    int main()
    {
        Complex  c1(2, 3);
        Complex  c2(5, -4);
        Complex c3 = c1 + c2;
       /*等价于Complex*/
        //Complex c3 = c1.operator+(c2);//由于是类外定义使用,类内就可以是使用
        c3.print();
        Complex c4 = c1 - c2;
        c4.print();
        Complex c5 = c1 * c2;
        c5.print();
        return 0;
    }

编译器对类类型数据的双目运算符的处理(加法为例)

1.当编译器遇到fa+fb的代码,就会先去fa对应的类类型Fraction中寻找一个成员函数:
operator+(const Fraction &),如果有这个成员函数,就调用该函数计算  fa+fb ===> fa.operator+(fb); 
2.如果Fraction类中没有对应的成员函数,就去寻找一个全局函数:
operator+(const Fraction &,const Fraction &),如果有这个全局函数,就调用该函数计算  fa+fb ===> operator+(fa,fb);
3.全局函数也没有,编译器报错           

其他的双目运算符

== != > < >= <= (返回bool类型) = += -= (返回第一操作数的引用) //当类中有拷贝构造函数,=运算符才需要重载

类类型和基本类型之间的运算

Complex+浮点数 ----- 可以重载成成员函数/全局函数 浮点数+Complex ----- 只能重载成全局函数

重载也是可以加上参数的(写法有改变的)

Complex operator+(const double &num) {

        return  Complex(this->real + num, this->imag);
    }
/*08-双目运算符的重载*/
#include <iostream>
using namespace std;

class Complex {
public:
     Complex(double real = 0, double imag = 0) :real(real), imag(imag) {}
    //打印
    void print()
    {
        cout << this->real << (this->imag>=0 ? " +":"") << this->imag << "i" << endl;
    }
   
    Complex operator+(const double &num) {

        return  Complex(this->real + num, this->imag);
    }

    //-----------------------------------------
    // 如果必须要在类内定义的话,只能定义为单参数的运算符函数: friend  Num operator+(const Num& n1);
    // 如果定义为双参数的运算符函数,只能在类外定义
    //-------------------------------------

private:
    double real;
    double imag;
};

    int main()
    {
        Complex cp(4, 2);

        Complex cp1 = cp + 2;
        cp1.print();
        //如果要常数在前面,改重载方法里的参数就可以了
       
        return 0;
    }

注意:

当使用非本类类型去初始化一个新对象时,先尝试使用初始化数据作为参数去调用构造函数构造成本类型对象,要支持该语法类中必须具有以初始化数据类型作为参数的单参构造函数。

如果不希望C++编译器使用该隐式转换,可以在类的构造函数声明前加 explicit 关键字

#include <iostream>
using namespace std;

class Complex {
public:
     explicit Complex(double real = 0, double imag = 0) :real(real), imag(imag) {}
    //打印
    void print()
    {
        cout << this->real << (this->imag>=0 ? " +":"") << this->imag << "i" << endl;
    }
   
    Complex operator+(const double &num) {

        return  Complex(this->real + num, this->imag);
    }

    //-----------------------------------------
    // 如果必须要在类内定义的话,只能定义为单参数的运算符函数: friend  Num operator+(const Num& n1);
    // 如果定义为双参数的运算符函数,只能在类外定义
    //-------------------------------------

private:
    double real;
    double imag;
};

    int main()
    {
        Complex cp(4, 2);

        Complex cp1 = cp + 2.1;
        cp1.print();
        //如果要常数在前面,改重载方法里的参数就可以了
       
        return 0;
    }

输入输出运算符的重载(cout<< cin>>)

cin>>a

1.先去cin对应的类istream中寻找一个成员函数:istream& operator>>(a的引用类型) 
2.如果没有,就去寻找一个全局函数:istream& operator<<(istream &is,a的引用类型); 
3.如果再没有,报错 //istream是C++预定义的类,不能修改,因此开发者只能通过全局函数重载 //并且将重载函数设置为a类型的友元          
/*08-双目运算符的重载*/
#include <iostream>
using namespace std;

class Complex {
public:
     Complex(double real = 0, double imag = 0) :real(real), imag(imag) {}
    //打印
    void print()
    {
        cout << this->real <<(this->imag>=0 ? "+":"")<< this->imag << "i" << endl;
    }
   
    Complex operator+(const double &num) {

        return  Complex(this->real + num, this->imag);
    }

    friend istream& operator>>(istream& is, Complex& c);

      
private:
    double real;
    double imag;
};
istream& operator>>(istream &is,Complex &c ) {

    return  is >> c.real >> c.imag;
}
    int main()
    {
        Complex  c1;

        cin >> c1;
        c1.print();

       
        return 0;
    }

cout<<

1.先去cout对应的类ostream中寻找一个成员函数:ostream& operator<<(a的常引用类型) 2.如果没有,就去寻找一个全局函数:ostream& operator<<(ostream &os,a的常引用类型); 3.如果再没有,报错 //ostream是C++预定义的类,不能修改,因此开发者只能通过全局函数重载 //并且将重载函数设置为a类型的友元

/*08-双目运算符的重载*/
#include <iostream>
using namespace std;

class Complex {
public:
     Complex(double real = 0, double imag = 0) :real(real), imag(imag) {}
    //打印
    //void print()
    //{
    //    cout << this->real <<(this->imag>=0 ? "+":"")<< this->imag << "i" << endl;
    //}

    friend istream& operator>>(istream& is, Complex& c);
    friend ostream& operator<<(ostream& is, const Complex& c);
      
private:
    double real;
    double imag;
};
istream& operator>>(istream &is,Complex &c ) {

    return  is >> c.real >> c.imag;
}

ostream& operator<<(ostream & os,const Complex &c ) {

    return  os << c.real << (c.imag >= 0 ? "+" : "") << c.imag << "i" << endl;
}
    int main()
    {

        Complex  c1;

        cin >> c1;
        //c1.print();


        cout << c1;
        //c1.print();

        return 0;
    }

练习:

为Fraction类实现 ==,>,+=,>>,<<的重载

3.单目运算符的重载

/*08-单目运算符的重载*/
#include <iostream>
using namespace std;

class Interger {
public:
    Interger(int d = 0) :data(d) {}
    //重载-号(负号)
    Interger operator-()  //!!!!
    {
        return Interger(-this->data);
    }
    int get_data() {
        return this->data;
    }

    friend ostream& operator<<(ostream& os, const Interger& i);
private:
    int data;
};

ostream& operator<<(ostream& os, const Interger& i)
{
    return os << i.data;
}
    int main()
    {

        Interger  i1(10);

        Interger  i2;

        i2 = -i1;
        cout << i2 << endl;

        return 0;
    }

++,--的重载

/*03-单目运算符的重载*/
#include <iostream>

using namespace std;

class Interger{
public:
    Interger(int d=0):data(d){}

    //重载-号(负号)
    Interger operator-()
    {
        return Interger(-this->data);
    }

    //重载前++
    Interger& operator++()
    {
        ++this->data;
        return *this;
    }

    //重载后--
    Interger operator--(int)
    {
        return Interger(this->data--);
    }

    friend ostream& operator<<(ostream &os,const Interger &i);
    friend Interger operator++(Interger &i,int);
    friend Interger& operator--(Interger &i);

private:
    int data;
};

ostream& operator<<(ostream &os,const Interger &i)
{
    return os<<i.data;
}
//--------------
//重载后++  
//后++要有哑元
//--------------
Interger operator++(Interger &i,int)
{
    return Interger(i.data++);
}

//重载前--
Interger& operator--(Interger &i)
{
    --i.data;
    return i;
}

int main()
{
    Interger i1(10);
    Interger i2(20);

    //i2 = -i1;
    //cout<<i2<<endl;

    //i2 = ++i1;
    //i2 = i1++;
    //i2 = --i1;
    i2 = i1--;
    cout<<-i1<<endl;
    cout<<i2<<endl;

    return 0;
}

编译器对单目运算符(#)的处理

对象 ----- 去成员函数中找一个operator#()的一个函数 如果没有,就去全局函数中找一个operator#(对象) 再没有就报错 对象# ----- 去成员函数中找一个operator#(int)的一个函数 如果没有,就去全局函数中找一个operator#(对象,int) 再没有就报错

练习:

实现Float类(浮点类),重载 前后++(成员函数) 前后--(全局函数),<<,-(负号),=,+=

class Float{ //... private: double data; }

运算符重载的限制

以下运算符不能重载

?: . :: sizeof && || & |

不能发明运算符

@ $

不能改变运算符的特性,符合对象的运算属性

不能重载基本类型的运算,重载时至少要有一个是类类型

只能重载成全局函数的运算符

第一个操作数是C++预定义类型 >> << 第一操作数是基本类型 整数+类类型

只能重载成成员函数的运算符

=(+= -= *=) ------ 赋值运算符 [] -------- 数组对象(把对象当数组使用) () -------- 强转,函数对象(把对象当函数使用) -> * ------ 指针对象(把对象当指针使用)


特殊运算符的重载

数组对象

当对象遇到[]运算符时,在成员函数中找一个 operator,如果没有就报错

/*01-重载拷贝构造*/
#include <iostream>
#include <cstring>

using namespace std;

class MyArray {
public:
    //构造函数
    MyArray(int* arr = NULL, size_t len = 10) :len(len)
    {
        //分配空间 ----- 默认抛异常
        this->pdata = new int[this->len];
        //初始化数组元素
        if (arr) {
            //传入了数组就使用数组的数据进行初始化
            memcpy(this->pdata, arr, this->len * sizeof(int));
        }
        else {
            //没有传数组就初始化为0
            memset(this->pdata, 0, this->len * sizeof(int));
        }
    }

    //析构函数
    ~MyArray()
    {
        delete[] this->pdata;
    }

    //拷贝构造函数
    MyArray(const MyArray& arr) :len(arr.len)/*初始化参数列表*/
    {
        //this->len = arr.len;
        this->pdata = new int[this->len];
        memcpy(this->pdata, arr.pdata, this->len * sizeof(int));
    }
   //----------------------------------------
    //重载=运算符
    MyArray& operator=(const MyArray& arr)
    {
        //防止自赋值  防止出现arr1=arr1的情况
        if (this == &arr)
            return *this;

        //释放旧空间
        delete[] this->pdata;
        //申请空间
        this->pdata = new int[arr.len];
        this->len = arr.len;
        //拷贝数据
        for (size_t i = 0; i < arr.len; i++) {
            this->pdata[i] = arr.pdata[i];
        }

        return *this;
    }
 //数组对象,重载[]
    int& operator[](int n)
    {
        return this->pdata[n];
    }

    //重载<<
    friend ostream& operator<<(ostream& os, const MyArray& arr);

private:
    int* pdata;
    /*const */size_t len;
};

ostream& operator<<(ostream& os, const MyArray& arr)
{
    for (size_t i = 0; i < arr.len - 1; i++)
        os << arr.pdata[i] << ",";

    return os << arr.pdata[arr.len - 1];
}

int main()
{
    int arr[] = { 1,2,3,4,5,6,7,8,9 };
    MyArray arr2(arr, 9);
    cout << arr2 << endl;
    MyArray arr1 = arr2;
    cout << arr1 << endl;
    return 0;
}

强转和函数对象

强转

当编译器遇到 (类型)对象 语法时,就回去对象对应的类中寻找一个 operator 类型() 的成员函数,找不到就按默认强转/报错来处理。

函数对象

当编译器遇到 对象(实参) 语法时,就回去对象对应的类中寻找一个 operator()(参数) 的成员函数,找不到就报错。

/*01-重载拷贝构造*/
#include <iostream>
#include <cstring>

using namespace std;

class MyArray {
public:
    //构造函数
    MyArray(int* arr = NULL, size_t len = 10) :len(len)
    {
        //分配空间 ----- 默认抛异常
        this->pdata = new int[this->len];
        //初始化数组元素
        if (arr) {
            //传入了数组就使用数组的数据进行初始化
            memcpy(this->pdata, arr, this->len * sizeof(int));
        }
        else {
            //没有传数组就初始化为0
            memset(this->pdata, 0, this->len * sizeof(int));
        }
    }

    //析构函数
    ~MyArray()
    {
        delete[] this->pdata;
    }

    //拷贝构造函数
    MyArray(const MyArray& arr) :len(arr.len)/*初始化参数列表*/
    {
        //this->len = arr.len;
        this->pdata = new int[this->len];
        memcpy(this->pdata, arr.pdata, this->len * sizeof(int));
    }
    //----------------------------------------
     //重载=运算符
    MyArray& operator=(const MyArray& arr)
    {
        //防止自赋值  防止出现arr1=arr1的情况
        if (this == &arr)
            return *this;

        //释放旧空间
        delete[] this->pdata;
        //申请空间
        this->pdata = new int[arr.len];
        this->len = arr.len;
        //拷贝数据
        for (size_t i = 0; i < arr.len; i++) {
            this->pdata[i] = arr.pdata[i];
        }

        return *this;
    }
    //数组对象,重载[]
    int& operator[](int n)
    {
        return this->pdata[n];
    }

    //重载(强转) ------ (int)得到数组第一个元素
    operator int()
    {
        return this->pdata[0];
    }


    //强转(size_t),得到数组长度
    operator size_t()
    {
        return this->len;
    }


    //重载() ----- 函数对象 ---- 数组求和
    int operator()(const MyArray& arr)
    {
        int sum = 0;
        for (size_t i = 0; i < arr.len; i++) {
            sum += arr.pdata[i];
        }

        return sum;
    }
    //重载<<
    friend ostream& operator<<(ostream& os, const MyArray& arr);

private:
    int* pdata;
    /*const */size_t len;
};

ostream& operator<<(ostream& os, const MyArray& arr)
{
    for (size_t i = 0; i < arr.len - 1; i++)
        os << arr.pdata[i] << ",";

    return os << arr.pdata[arr.len - 1];
}

int main()
{
    int arr[] = { 1,2,3,4,5,6,7,8,9 };
    MyArray arr2(arr, 9);
    cout << arr2 << endl;
    MyArray arr1 = arr2;
    cout << arr1[2] << endl;
    MyArray arr1 = arr2;

    //强转(size_t),得到数组长度
    cout << (size_t)arr1 << endl;

    //重载(强转) ------ (int)得到数组第一个元素
    cout << (int)arr1 << endl;

    //重载() ----- 函数对象 ---- 数组求和
    cout << arr1(arr2) << endl;
    return 0;
}

指针对象 ( 把对象当指针用)

当编译器遇到 对象-> 语法时,就会去对象对应的类中寻找一个 operator->() 的成员函数,找不到就报错。

当编译器遇到 对象 语法时,就会去对象对应的类中寻找一个 operator() 的成员函数,找不到就报错。

使用指针对象把对象当指针用,目的是管理其他对象的指针。

如果指针对象再结合模板的语法,就是可以实现管理各种类型指针的智能指针。

/*02-指针对象*/
#include <iostream>
#include <cstring>

using namespace std;

class A{
public:
    A(){cout<<"A()"<<endl;}
    ~A(){cout<<"~A()"<<endl;}

    void show()
    {
        cout<<"show A"<<endl;
    }
};

//实现一个指针对象,管理A类型的指针
class myauto_ptr{
public:
    myauto_ptr(A *p=NULL):pdata(p)
    {
        cout<<"myauto_ptr()"<<endl;
    }

    ~myauto_ptr()
    {
        cout<<"~myauto_ptr()"<<endl;
        if(this->pdata)
            delete this->pdata;
    }

    //重载->运算符
    A *operator->()
    {
        return this->pdata;
    }

    //重载*运算符
    A& operator*()
    {
        return *this->pdata;
    }

private:
    A *pdata;//要管理的指针
};

int main()
{
    A *pa = new A;

    //使用指针对象管理pa ----- 无需再释放pa
    myauto_ptr ap(pa);

    //通过指针对象访问管理的指针指向的成员
    ap->show();
    //ap.operator->()->show();

    //解引用指针对象得到管理对象
    (*ap).show();
    //ap.operator*().show();

    return 0;
}

练习:

设计一个产品类(Product),记录包括单价(double price)和数量(int count)的成员

1.实现强转语法 (double) ----- 得到单价 (int) ----- 得到数量 2.实现函数对象,计算自己的总价 Product pd(19.8,12); pd() ===> 19.8 X 12 = 237.6

作业:

笔试题

32位系统

class A{

//虚函数表地址 8

bool a;//0~1

int b;//4~7

char c;//8~9

long d;//12~16

};

image-20220721225038181

为mystring类实现 +(拼接) =(赋值) <<(打印) (获取指定位置字符)

(char)===>强转char,得到首字符

mystring str("hello"); (char)str ===> 'h'

函数对象===>比较自己和参数mystring是否相同

mystring str("hello"); mystring str1("hello"); str(str1) ===> 1 mystring str("hello"); mystring str1("welcome"); str(str1) ===> 0