栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++杂讲 运算符重载

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++杂讲 运算符重载

目录

知识点1【运算符重载概述】

知识点2【可重载的运算符】

知识点3【运算符重载的步骤】

知识点4【重载运算符<<】

知识点5【重载>>运算符】

知识点6【重载==运算符】

知识点7【自增自减(++/--)运算符重载】

 知识点8【重载=赋值运算符】

 知识点9【重载 函数调用运算符 () 】

知识点10【不要重载&& || 用户没办法实现短路特性 】(重要)

知识点11【不能重载的运算符】

知识点12【运算符重载案例】


知识点1【运算符重载概述】

运算符重载,就是对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

比如:重载+运算符完成"lucy"+“bob”拼接

void operator+(参数1,参数2)
{
    
}

知识点2【可重载的运算符】

知识点3【运算符重载的步骤】

运算符重载分析步骤:

1、分析运算符左边的运算对象。如果不是自定义类的对象,只能用全局函数 实现运算符重载。

        全局函数的第一参数:左边的运算对象,

        全局函数的第二个参数:右边的运算对象

2、分析运算符左边的运算对象.如果是自定义类的对象,可以用全局函数、成员函数 实现运算符重载。

        全局函数的第一参数:左边的运算对象,

        全局函数的第二个参数:右边对象

成员函数:可以省略一个参数,只需要将运算符右边的对象 传递到成员函数中。(推荐)

知识点4【重载运算符<<】
#include 
#include 
using namespace std;
class Person
{
    friend ostream& operator<<(ostream &out, Person &ob);
private:
    int num;
    string name;
    int age;
public:
    Person(){}
    Person(int num, string name, int age)
    {
        this->num = num;
        this->name = name;
        this->age = age;
    }
    void showPerson(void)
    {
        cout<<"学号:"< 

知识点5【重载>>运算符】
#include 
#include 
using namespace std;
class Person
{
    friend ostream& operator<<(ostream &out, Person &ob);
    friend istream& operator>>(istream &in, Person &ob);
private:
    int num;
    string name;
    int age;
public:
    Person(){}
    Person(int num, string name, int age)
    {
        this->num = num;
        this->name = name;
        this->age = age;
    }
    void showPerson(void)
    {
        cout<<"学号:"<(istream &in, Person &ob)
{
    in>>ob.num>>ob.name>>ob.age;
    return in;
}

int main(int argc, char *argv[])
{
    Person ob1(100,"lucy", 18);
    Person ob2(101,"bob", 19);

    cout<>ob3;
    cout< 

知识点6【重载==运算符】
#include 
#include 
using namespace std;
class Person
{
    friend ostream& operator<<(ostream &out, Person &ob);
    friend istream& operator>>(istream &in, Person &ob);
    //friend bool operator==(Person &ob1, Person &ob2);
private:
    int num;
    string name;
    int age;
public:
    Person(){}
    Person(int num, string name, int age)
    {
        this->num = num;
        this->name = name;
        this->age = age;
    }
    void showPerson(void)
    {
        cout<<"学号:"<(istream &in, Person &ob)
{
    in>>ob.num>>ob.name>>ob.age;
    return in;
}

#if 0
//全局友元函数 实现==重载
bool operator==(Person &ob1, Person &ob2)
{
    if(ob1.num==ob2.num && ob1.name==ob2.name && ob1.age==ob2.age)
        return true;
    else
        return false;
}
#endif

int main(int argc, char *argv[])
{
    Person ob1(100,"lucy", 18);
    Person ob2(101,"bob", 19);

    cout<>ob3;
    cout< 

知识点7【自增自减(++/--)运算符重载】

当编译器看到 ++a   (前置++),它就调用operator++(a),

当编译器看到 a++   (后置++),它就会去调用operator++(a,int)

全局函数实现:

        ++ob ------------->operator++(ob)

        ob++ ------------->operator++(ob, int)

成员函数实现:

        ++ob ------------->ob.operator++()

        ob++ ------------->ob.operator++(int)

#include 

using namespace std;
class Data
{
    friend ostream& operator<<(ostream &out, Data ob);
private:
    int a;
    int b;
public:
    Data(){}
    Data(int a, int b):a(a),b(b){}

    //重载ob++  先使用  后加减
    Data operator++(int)
    {
        //保存++之前的值
        Data tmp = *this;

        //进行自增
        a = a+1;
        b = b+1;

        return tmp;//返回++之前的值
    }

    //重载++ob  先加减  后使用
    Data operator++()
    {
        //先加减
        a=a+1;
        b=b+1;

        //再使用
        return *this;
    }

};
ostream& operator<<(ostream &out, Data ob)
{
    out<<"a="< 知识点8【重载=赋值运算符】 

(类中有指针成员 才需要重载=)

如果类中没有指针成员 相同类型的对象 可以直接=赋值(浅拷贝)

如果类中有指针成员 相同类型的对象 必须重载=(深拷贝)

#include
class Data2
{
public:
    char *str;//指针成员
public:
    Data2(){}
    Data2(char *str)
    {
        this->str = new char[strlen(str)+1];
        strcpy(this->str, str);
    }
    ~Data2()
    {
        if(str != NULL)
        {
            delete [] str;
            str=NULL;
        }
    }
    //重载=
    Data2& operator=(Data2 &ob)
    {
        cout<<"重载="<str = new char[strlen(ob.str)+1];
        strcpy(this->str, ob.str);
        return *this;
    }
};
void test02()
{
    Data2 ob1("hello world");
    Data2 ob2;
    Data2 ob3;
    ob3 = ob2 = ob1;
    cout< 

总结:如果类中有指针成员

必须实现拷贝构造(深拷贝)、必须实现析够函数(释放指针成员所指向的空间)、必须重载=运算符(深拷贝)

 知识点9【重载 函数调用运算符 () 】
class Print
{
public:
    void operator()(char *str)
    {
        cout< 

8、不要重载&& || 用户没办法实现短路特性 (重要)

知识点10【不要重载&& || 用户没办法实现短路特性 】(重要)

知识点11【不能重载的运算符】

 

知识点12【运算符重载案例】

mystring.h

#ifndef MYSTRING_H
#define MYSTRING_H
#include
using namespace std;

class MyString
{
    friend ostream& operator<<(ostream &out, MyString ob);
private:
    char *str;
    int size;
public:
    MyString();
    MyString(char *str);
    MyString(const MyString &ob);
    ~MyString();

    //获取字符串的长度
    int getSize(void);
    //获取字符
    char& operator[](int pos);
    char& at(int pos);

    //重载赋值运算符=
    MyString& operator=(MyString &ob);
    MyString& operator=(char *str);

    //重载赋值运算符+=
    MyString& operator+=(MyString &ob);
    MyString& operator+=(char *str);

    //重载+运算符
    MyString operator+(MyString &ob);
   // MyString operator+(char *str);

    bool operator>(MyString &ob);

};


#endif // MYSTRING_H

myString.cpp

#include "mystring.h"
#include
#include
using namespace std;
MyString::MyString()
{
    str=NULL;
    size=0;
    cout<<"无参构造"<str = new char[size+1];
    strcpy(this->str, str);
    cout<<"有参构造"<=0 && pos=0 && posstr != NULL)
    {
        delete [] this->str;
        this->str=NULL;
    }

    size = strlen(str);
    this->str = new char[size+1];
    strcpy(this->str, str);

    return *this;
}

MyString& MyString::operator+=(MyString &ob)
{
    if((str==NULL) && (ob.str != NULL))
    {
        size = ob.size;
        str = new char[size+1];
        memset(str, 0,size+1);
        strcpy(str, ob.str);

        //记得返回
    }
    else if((str != NULL) && (ob.str == NULL))
    {
        //记得返回
    }
    else if((str == NULL) && (ob.str == NULL))
    {
        cout<<"全为NULL无法拼接"<str == NULL)
    {
        size = strlen(str);
        this->str = new char[size+1];
        memset(this->str, 0, size+1);
        strcpy(this->str, str);
    }
    else if(this->str != NULL)
    {
        size = size+strlen(str);
        char *tmp_str = new char[size+1];
        memset(tmp_str, 0, size+1);
        strcpy(tmp_str, this->str);
        strcat(tmp_str, str);

        //释放this->str指向的旧空间
        delete [] this->str;
        this->str = tmp_str;
    }
    return *this;
}

MyString MyString::operator+(MyString &ob)
{
    if((this->str==NULL) && (ob.str == NULL))
    {
        cout<<"全空无法进行+运算"<str!=NULL) && (ob.str == NULL))
    {
        return *this;
    }
    else if((this->str==NULL) && (ob.str != NULL))
    {
        return ob;
    }
    else if((this->str!=NULL) && (ob.str != NULL))
    {
        char *buf=new char[size+ob.size+1];
        memset(buf, 0, size+ob.size+1);
        strcpy(buf,this->str);
        strcat(buf, ob.str);

        MyString tmp(buf);

        delete [] buf;

        return tmp;
    }
}

bool MyString::operator>(MyString &ob)
{
    if((str != NULL) && (ob.str != NULL) )
    {
        if(strcmp(str, ob.str)>0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        cout<<"存在NULL字符串无法比较大小"< 

main.cpp

#include 
#include "mystring.h"
using namespace std;

int main(int argc, char *argv[])
{
#if 0
    MyString str1("hello");
    cout<<"str1的长度="< str12)
    {
        cout<<"大于"< 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/942472.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号