C++(16)--运算符重载(自定义Integer类)

《老九学堂C++课程》《C++ primer》学习笔记。《老九学堂C++课程》详情请到B站搜索《老九零基础学编程C++入门》
-------------简单的事情重复做,重复的事情用心做,用心的事情坚持做(老九君)---------------

C++创始人:要轻松的使用这种语言,不要觉得必须使用所有的特性,不要在第一次学习的时候就试图使用所有的特性。

1.运算符重载–重点

1.函数的重载和参数有关,和函数名无关。
2.运算符重载就是“想法的转换”,它的目标是简化函数调用的方式。
3.把标准的函数使用方式,重新定义成自己认为的方式。给符号赋予新的含义,使得一个运算符可以拥有不同的功能。

实际上:我们已经用过了很多运算符的重载,C++本身已经对这些运算符进行了重载

  1. +号可以实现不同类型数据的加法操作。
  2. << 既是位移运算符,又可以配cout 实现控制台数据的输出

两个对象想要进行相加,必须进行运算符重载。重载的一个效果是为了是为了美观

小试牛刀:定义一个运算符重载,就像是定义了一个函数,只不过这个函数名称以关键字operator开头.

运算符重载的语法格式为:

返回类型 operater被重载的运算符(参数列表)

编译器实际调用:

num3 = num1.operator+(num2);

demo1:重载加法运算符1

//mian.cpp
#include <iostream>
#include "Integer.h"
using namespace std;
void TestInteger();
void TestIntefer()
{
    Integer int1(1024), int2(2048),int3;
    int3 = int1 + int2;
    cout << "int3 = int1 + int2的结果为" << int3.Intvalue()<< endl;
}
int main() {
    TestIntefer();
    return 0;
}
//Integer.h
//
// Created by 陈莹莹 on 2021/2/14.
// 我们自己定义的整型类,将整型封装成类,以便面向对象的封装

#ifndef CHAPTER12_INTEGER_H
#define CHAPTER12_INTEGER_H


class Integer {
public:
    Integer();
    Integer(int value):m_value(value){}
    // 重载加号运算符
    Integer operator+(Integer other);
    int Intvalue(){return m_value;}
    ~Integer();
private:
    int m_value; // 实际的整型数字,提供一些十分方便操作的函数

};

//Integer.cpp
//
// Created by 陈莹莹 on 2021/2/14.
//

#include "Integer.h"
//调用默认构造时会为m_value赋一个默认值为0
Integer::Integer():m_value(0)
{
    //ctor
}
Integer Integer::operator+(Integer other)
{
    Integer result(this->m_value + other.m_value);  // 加法的结果,返回
    return result;
}
Integer::~Integer()
{
    //dtor
}

demo2:重载加法运算符2–( )

    // 重载加号运算符2--对应的.cpp 需要改变
    // const Integer &operator+(const Integer & other) const;
    const Integer operator+(const Integer & other) const;
    // 第一个const 修饰返回值,返回值用引用更好(报错,不返回引用)
    // 第二个const 修饰参数,传递的是参数的引用,并且不希望改变参数
    // 第三个const 不修改当前的成员变量
//Integer.cpp
const Integer Integer::operator+(const Integer & other) const
{
    Integer result(this->m_value + other.m_value);  // 加法的结果,返回
    return result;
}

可以被重载的运算符包括所有的C的运算符,可以重载的运算符列表如下

二远运算符+,-,*,/,%
关系运算符==, !=, <, >, <=, >=
逻辑运算符
一元运算符+正, -负, *(指针), &(取地址), ++(自增), --(自减)
位运算符&,
赋值运算符=, +=, -=, *=, /=, %=, &=,
内存声明与释放new, delete, new[], delete[]
其他运算符()(函数), ->(成员访问), ->*(成员指针访问), ,(逗号), [](下标)

不能被重载的运算符

.点运算符
*成员指针访问运算符
::域运算符
sizeof长度运算符
?:三元运算符

预算符重载的注意点:
1.重载不能修改运算变量的个数
2.重载不能修改运算符的优先级别
3.重载不能修改运算顺序

(运算符重载先听一遍,之后需要的化买《Effective C++》(更高效的C++)再看)
在条款10:重载赋值运算符(复合赋值符号)的建议用法:返回*this。返回一个新建对象也可以,但是不够高效。
在条款11: 如果是自我赋值,就不要做任何事情。

2.友元函数–难点(流运算符重载)

Integer num1(1024), Integer num2(24);
Integer num3 = num1 + num2; // 正常执行
Integer num4 = 1024 + num1; // 编译错误。 因为int 类型中没有重载+ 号

使用友元函数,重载运算符时加一个friend关键字。 可以直接访问类的私有成员。
(仅摘录相关代码)
demo: 友元函数重载,+,流运运算符

//main.cpp
    Integer int5;
    // int5 = int4 + 1024;    // 没有实现友元函数也能够运行的写法
    int5 = 1024 + int4;       // 加了友元函数才能过
    cout << "int5 = " << int5.Intvalue() << endl;
    // 重载流运算符--可以实现封装某个对象的打印格式
    cout << "重载了流运算符,可以直接打印对象" << int5 << endl;
    Integer int6;
    cin >> int6;
    cout << "输入的int6为:" << int6;
//Integer.h
    // 友元函数--friend
    friend const Integer operator+(int intValue, const Integer & other);  // 最后不能加const会报错

    // 重载流运算符--一般只能使用友元的方式,返回值插入流,会变所以不用const 修饰返回值
    friend ostream & operator<<(ostream & out, const Integer & num);
    friend istream & operator>>(istream & in, Integer & num);
//Integer.cpp
// 友元函数不需要使用域运算符号
const Integer operator+(int intValue, const Integer & other){
    cout << "使用友元方式重载了+运算符,实现int类型和Integer类型的相加" << endl;
    return Integer(intValue + other.m_value);  // 返回了一个新的Integer对象
}
ostream & operator<<(ostream & out, const Integer & num){
    out << num.m_value;
    return out;
}
istream & operator>>(istream & in, Integer & num){
    cout << "请输入一个整数:" ;
    in >> num.m_value;
    return in;
}
运算符声明成类成员还是声明成独立友元函数的建议准则
必须声明为成员函数赋值预算符=、数组下标预算符[]、函数调用运算符()、成员访问运算符->
必须声明为友元函数流预算符>>、<<、类型转换运算符
一般声明为友元函数一元运算符、复合赋值运算符
一般声明为友元函数二元运算符

注意:

  1. 剩下的预算符可以友元也可以成员函数重载
  2. 必须选择一种方式,避免二义性,参数列表不同就是不同的两个重载
  3. 没有绝对的好坏,按上表建立来就可以。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值