(C++)运算符重载

目录

一、运算符重载的概念

1、实现“+”号运算符的重载

2、运算符重载的规则

(1)不是所有的运算符都能重载

不能重载的运算符: ::  ?: sizeof  .  .*

(2)重载不能改变运算符的优先级

(3)重载不能改变运算符的操作数

(4)运算符重载不允许有默认值

(5)运算符重载函数既可以是全局函数,也可以是类成员函数

3、运算符重载的步骤

(1)写出函数名 operator...

(2)根据操作数写出函数的形参

(3)根据使用场景确定函数的返回值

4、重载输出运算符(<<)

5、重载单目运算符

6、逻辑运算符重载

7、数组重载、==号重载、=号重载

8、智能指针

作业:自己实现字符串类


一、运算符重载的概念

所谓运算符重载,就是给运算符赋予新的功能,同一个运算符具有不同的功能

运算符重载的本质:函数的重载

1、实现“+”号运算符的重载

重载功能:使实列对象可以直接相互加减,从而使类中的成员变量相互加减

#include <iostream>
using namespace std;

class Complex
{
    //friend Complex operator+(const Complex &c1,const Complex &c2);  //友元函数
    private:
        int m_a;
        int m_b;
    
    public:
        void print()
        {
            cout<<a<<" + "<<b<<"i"<<endl;
        }
        Complex(int a, int b)
        {
            this->m_a = a;
            this->m_b = b;
        }
        Complex operator+(const Complex &c)
        {
            Complex t(0,0);
            t.m_a = this->m_a + c.m_a;
            t.m_b = this->m_b + c.m_b;
            
            return t;
        }
};

/*
    Complex operator+(const Complex &c1, const Complex &c2)  //函数重载
    {
        Complex t(0,0);
        t.a = c1.a + c2.a;
        t.b = c1.b + c2.b;

        return t;
    }
*/

int main(int argc, char *argv[])
{
    Complex c1(1,2);
    Complex c2(2,3);
    Complex t(0,0);

    t = c1 + c2;  // t = operator+(c1,c2);
    t.print();

    return 0;
}

2、运算符重载的规则

(1)不是所有的运算符都能重载

不能重载的运算符: ::  ?: sizeof  .  .*

(2)重载不能改变运算符的优先级

(3)重载不能改变运算符的操作数

(4)运算符重载不允许有默认值

(5)运算符重载函数既可以是全局函数,也可以是类成员函数

3、运算符重载的步骤

(1)写出函数名 operator...

(2)根据操作数写出函数的形参

(3)根据使用场景确定函数的返回值

4、重载输出运算符(<<)

功能:可以直接通过输出类名,来访问类的成员变量(输出成员变量)

#include <iostream>
using namespace std;

class Complex
{
    friend ostream& operator<<(ostream &out, const Complex &c);  //友元函数
    private:
        int m_a;
        int m_b;

    public:
        Complex(int a, int b)
        {
            this->m_a = a;
            this->m_b = b;
        }
        
};

ostream& operator<<(ostream &out, const Complex &c)   //外部重载
{
    out<<c.m_a<<" + "<<c.m_b<<"i";
    return out;
}

int main(int argc, char *argv[])
{
    Complex c1(1,2);
    
    cout<<c1<<endl;   //通过输出c1,可以直接输出 1 + 2i ;

    return 0;
}

5、重载单目运算符

功能:将实例的对象经过单目运算符的计算,从而完成对象内部的成员变量的计算

#include <iostream>
using namespace std;

class Complex
{

    friend ostream& operator<<(ostream &out, const Complex &c);
    private:
        int m_a;
        int m_b;
    public:
        Complex(int a, int b)
        {
            this->m_a = a;
            this->m_b = b;
        }

        Complex operator++()  //前置++
        {
            this->m_a++;
            this->m_b++;
            
            return *this;
        }
        Complex operator++(int)  //后置++
        {
            Complex t = *this;
            this->m_a++;
            this->m_b++;
            
            return t;
        }

        Complex operator--()  //前置--
        {
            this->m_a--;
            this->m_b--;
            
            return *this;
        }
        Complex operator--(int)  //后置--
        {
            Complex t = *this;
            this->m_a--;
            this->m_b--;
            
            return t;
        }
        
};

ostream& operator<<(ostream &out, const Complex &c)
{
    out<<c.m_a<<" + "<<c.m_b<<"i";
    return out;
}

int main(int argc, char *argv[])
{
    Complex c1(1,2);
    cout<<c1<<endl;
    
    cout<<++c1<<endl;
    cout<<c1++<<endl;

    cout<<--c1<<endl;
    cout<<c1--<<endl;
    
    return 0;
}

6、逻辑运算符重载

功能:可以直接将两个对象进行逻辑&&运算比较,从而判断真或假

#include <iostream>
using namespace std;

class Complex
{
    private:
        int a; //实部
        int b; //虚部
    public:
        Complex(int a,int b)
        {
            this->a = a;
            this->b = b;
        }

        bool operator&&(const Complex &c)
        {
            return (this->a && c.a)&&(this->b && c.b);
        }
};

int main(int argc, char const *argv[])
{

    Complex c1(1,1);
    Complex c3(0,0);
    if(c1 && c3)      //c1.operator&&(c3);   //一般不重载逻辑&&运算符,不遵循短路原则
    {
        cout<<"成立"<<endl;
    }

    return 0;
}

7、数组重载、==号重载、=号重载

功能:对象通过[ ] 访问数组元素;可以让对象直接比较(==);对象间赋值(=)

#include <iostream>
using namespace std;

class Array
{
    private:
        int *data;
        int size;

    public:
        Array(int a)
        {
            size = a;
            data = new int[size];
        }      
        
        int& operator[](int i)
        {
            return data[i];
        }  
        
        bool operator==(const Array &a)
        {
            return (data == a.data)&&(size == a.size);
        }

        Array operator=(const Array &a)
        {
            if(*this == a)
            {
                return *this;
            }
            size = a.size;
            delete []data;
            
            data = new int[size];
            
            for(int i = 0; i < size; i++)
            {
                data[i] = a.data[i];
            } 
            return *this;     
        }

};

int main(int argc, char const *argv[])
{
    Array a1(10);                     //构造函数

    for (int i = 0; i < 10; i++)
    {
        a1[i] = i + 1;                //[]重载
    }

    for (int  i = 0; i < 10; i++)
    { 
        cout<<a1[i]<<endl;          
    }

    a1 = a1;                           //=重载
    for (int  i = 0; i < 10; i++)
    {
        cout<<a1[i]<<endl;
    }
    
    Array a2(5);
    for (int  i = 0; i < 5; i++)
    {
        a2[i] = i;
    }
    a1 = a2;

    for (int i = 0; i < 5; i++)
    {
        cout<<a1[i]<<endl;
    }

    return 0;
}

8、智能指针

为了解决跨函数释放空间的问题。

#include <iostream>
#include <memory>
#include <auto_ptr.h>
using namespace std;

class Test
{
    public:
        Test()
        {
            cout<<"Test的构造函数"<<endl;
        }
        void print()
        {
            cout<<"xxxx"<<endl;
        }
        ~Test()
        {
            cout<<"Test的析构函数"<<endl;
        }
};

void f1()
{
    Test *t1 = new Test;
}

void f2()
{
    auto_ptr<Test> my_memory(new Test);
    my_memory->print();                 //my_memory.operator->(print())
}

int main(int argc, char const *argv[])
{
    //f1();
    f2();
    return 0;
}

作业:自己实现字符串类

补全下面的代码(多重载)

#include <iostream>
using namespace std;
class MyString
{
private:
    char *pstring;
    int size;
};
int main(int argc, char const *argv[])
{
    MyString s1("helloworld");    //构造函数
    MyString s2(s1);              //构造函数

    cout<<s2[0]<<endl;             //[]重载
    cout<<s2[3]<<endl;

    s2 = "nihao beijing";           // = 重载

    MyString s3;                    //构造函数
    s3 = s2 = s1;                   // = 重载

    MyString s4;
    s4 = s4 + "hello world";       //+重载
 
    MyString s5(haaa);

    s5 = s5 + s4;                //+重载

    if(s4 == s5)                //==重载
    {
        cout<<"s4 == s5"<<endl;
    }
    else
    {
        cout<<"s4 != s5"<<endl;
    }
    return 0;
}

补全后的代码如下:

#include <iostream>
#include <cstring>
using namespace std;
class MyString
{
    private:
        char *pstring;
        int size;

    public:
        void print();

        MyString();

        MyString(const char * str);

        MyString(const MyString &s);

        char& operator[](int i);             //重载[]

        void operator=(const char *str);       //重载=
 
        MyString& operator=(const MyString &obj);   //重载=

        MyString& operator+(const char * s);         //重载+

        MyString& operator+(const MyString &obj);     //重载+

        bool operator==(const MyString &obj);        //重载==

        ~MyString();
};

void MyString::print()
{
    cout<<this->pstring<<endl;
}


MyString::MyString()
{
    cout<<"无参构造"<<endl;
    this->pstring = NULL;
    this->size = 0;
}

MyString::MyString(const char *str)
{
    this->size = strlen(str) + 1;
    this->pstring = new char[size];

    strcpy(pstring, str);
}

MyString::MyString(const MyString &s)
{
    this->size = s.size;
    this->pstring = new char[this->size];

    strcpy(this->pstring, s.pstring);
}

char& MyString::operator[](int i)
{
    return pstring[i];
}

void MyString::operator=(const char *str)
{
    this->size = strlen(str) + 1;
    
    if (this->pstring == NULL)
    {
        this->pstring = new char[this->size];
    }
    else
    {
        delete []this->pstring;
        this->pstring = new char[this->size];
    }

    strcpy(this->pstring, str);
}

MyString& MyString::operator=(const MyString &obj)
{
    if (*this == obj)
    {
        return *this;
    }
    
    this->size = strlen(obj.pstring) + 1;
    if (this->pstring == NULL)
    {
        this->pstring = new char[this->size];
    }
    else
    {
        delete []this->pstring;
        this->pstring = new char[this->size];
    }

    strcpy(this->pstring, obj.pstring);

    return *this;
}

MyString& MyString::operator+(const char * s)
{
    if (this->pstring == NULL)
    {
        this->size = strlen(s) + 1;
        this->pstring = new char[strlen(s) + 1];
    }
    else
    {
        this->size = this->size + strlen(s);

        char *p = new char[this->size];
        strcpy(p, this->pstring);

        delete []this->pstring;
        this->pstring = new char[(this->size + strlen(s))];
        strcpy(this->pstring, p);

        delete []p;
        p = NULL;
    }
    strcat(this->pstring, s);
    //cout<<this->pstring<<endl;

    return *this;
}

MyString& MyString::operator+(const MyString &obj)
{

    if (this->pstring == NULL)
    {
        this->size = obj.size;
        this->pstring = new char[obj.size];
    }
    else
    {
        this->size = this->size + obj.size - 1;

        char *p = new char[this->size];
        strcpy(p, this->pstring);

        delete []this->pstring;
        this->pstring = new char[(this->size + obj.size - 1)];
        strcpy(this->pstring, p);

        delete []p;
        p = NULL;
    }
    strcat(this->pstring, obj.pstring);

    return *this;
}

bool MyString::operator==(const MyString &obj)
{
    return (this->size == obj.size)&&(this->pstring == obj.pstring);
}

MyString::~MyString()
{
    cout<<"系够函数"<<endl;
    if (this->pstring != NULL)
    {
        delete []this->pstring;
        this->pstring = NULL;
    }
    
}

int main(int argc, char const *argv[])
{
    MyString s1("helloworld");    //构造函数
    MyString s2(s1);              //构造函数

    cout<<s2[0]<<endl;             //[]重载
    cout<<s2[3]<<endl;

    s2 = "nihao beijing";           // = 重载

    MyString s3;                    //构造函数
    s3 = s2 = s1;                   // = 重载
    s3.print();

    MyString s4;
    s4 = s4 + "hello world";       //+重载
    s4.print();

    MyString s5("haaa");

    s5 = s5 + s4;                //+重载
    s5.print();

    if(s4 == s5)                //==重载
    {
        cout<<"s4 == s5"<<endl;
    }
    else
    {
        cout<<"s4 != s5"<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值