IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++运算符重载 -> 正文阅读

[C++知识库]C++运算符重载


前言

本篇文章主要介绍了C++运算符重载


1.运算符重载的概念

基本概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。

运算符重载的基本格式:
返回值类型 operator运算符 (参数列表)

大多数运算符可以被重载 不能重载的运算符只有5个:
运算符名称运算符
成员访问运算符.
成员指针访问运算符.*
域运算符::
长度运算符sizeof
条件运算符?:

2.运算符重载的两种方式

2.1 使用成员函数重载运算符(类内重载)

#include <iostream>
#include <string>

using namespace std;

/*
    实现A类,包含int类型变量的money
*/
class A{
public:
    A(int money = 0){
        this->money = money;
    }
    int getMoney(){
        return this->money;
    }
    // 重载A类的+号运算符
    A operator+(A &other){
        return A(this->money + other.money);
    }
private:
    int money;
};
int main() {
    A a1(100);
    A a2(200);
    A a3;    
    cout << "begin:" << a3.getMoney() << endl;
    a3 = a1 + a2;
    cout << "end:" << a3.getMoney() << endl;
    return  0;
}

2.2 使用友元函数重载运算符(类外重载)

#include <iostream>
#include <string>

using namespace std;

/*
    实现A类,包含int类型变量的money
*/
class A{
public:
    A(int money = 0){
        this->money = money;
    }
    int getMoney(){
        return this->money;
    }
    // 重载A类的+号运算符
    friend A operator+(const A a1,const A a2){
        return A(a1.money+a2.money);
    }
private:
    int money;
};
int main() {
    A a1(100);
    A a2(200);
    A a3;    
    cout << "begin:" << a3.getMoney() << endl; // output:0
    a3 = a1 + a2;
    cout << "end:" << a3.getMoney() << endl; //output:300
    return  0;
}

3.几种运算符重载

3.1 赋值运算符重载

#include <iostream>
#include <string.h>
#include <string>
#include <sstream>
using namespace std;

/*
    实现A类,包含int类型变量的money
*/
class A{
public:
    A(int money = 0,const char *name = ""){
        this->money = money;
        this->name = new char[strlen(name)+1];
        strcpy(this->name, name);
    }
    ~A(){
        if (this->name) {
            delete name;
        }
    }
    int getMoney(){
        return this->money;
    }
    string getInfo(){
        stringstream ret;
        ret << "name:" << name << "\tmoney:" << money;
        return ret.str();
    }
    //赋值运算符的重载:将money赋值给其他对象,注意浅拷贝还是深拷贝的问题
    A &operator=(const A &other){
        // if(name){
        //     delete name;
        // }
        // name = new char[strlen(other.name)+1];
        // strcpy(name, other.name);
        this->money = other.money; 
        return *this; //返回值,便于连续赋值 a1 = a2 = a3
    }  
    // 重载A类的+号运算符
    friend A operator+(const A &a1,const A &a2){
        return A(a1.money+a2.money);
    }

private:
    int money;
    char *name;
};
int main() {
    A a1(100,"a1");
    A a2(200,"a2");
    A a3(0,"a3");    
    cout << "begin:" << a3.getInfo() << endl; //output:a3 0
    a3 = a1 + a2;
    cout << "end:" << a3.getInfo() << endl;//output:a3 300
    return  0;
}

3.2 关系运算符的重载

	//关系运算符重载,返回值一般为bool类型
    bool operator>(const A &other){
        bool ret = false;
        if (this->money > other.money) {
            ret = true;
        }
        return ret;
    }

3.3 插入运算符(>>)和 提取运算符(<<)的重载

这里一般采用友元函数的形式重载(成员函数重载后写法是反的:a << cout)


    /*
	插入运算符和提取运算符的重载:
		cout 是 ostream类的对象。ostream 类和 cout 都是在头文件 <iostream>中声明的。ostream 类将<<重载为成员函数。
	*/
	// >> 重载,(如果要修改对象的内容不加const)
    friend istream &operator>>(istream &is, A &a1){
        is >> a1.money;
        return is;
    }
    // << 重载
    friend ostream &operator<<(ostream &os, const A &a1){
        os << a1.name << "\t" << a1.money;
        return os;
    }

//测试:
int main() {
    A a1(100,"a1");
    A a2(200,"a2");
    A a3(0,"a3");    
    a3 = a1 + a2;

    cout << a1 << endl << a2 << endl << a3 << endl;
    cin >> a1; //input:5000
    cout << a1; //output:a1 5000
    return  0;
}

3.4 前置运算符重载++ 和 后置运算符重载++

前置运算符,需要引用返回,不需要参数。返回自增后的值,且返回的是一个左值 ;
后置++,不需要引用返回,需要参数区分。返回自增前的值,且返回的是一个右值。

//前置++,++money
    A &operator++(){
        this->money++;
        return *this;
    }
    //后置++,money++
    const A operator++(int tmp){
        A a(this->money,this->name);
        this->money++;
        return a;
    }

//测试
int main() {
    A a1(100,"a1");
    A a2(200,"a2");
    A a3(0,"a3");    
    a3 = a1 + a2;

    cout << a3 << endl;
    cout << ++a3 << endl; //output:a3 301
    cout << a3++ << endl; //output:a3 301
    return  0;
}


  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-07-05 23:22:06  更:2022-07-05 23:24:04 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/12 12:45:38-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码