C++重载

本文介绍了C++中的函数重载和运算符重载概念,提供了多个示例,包括加号、减号、比较、输入输出、自增和赋值运算符的重载,展示了如何在类中实现这些功能,以便在编程时提供更灵活的接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 重载简介

重载分为函数重载和操作符重载。

函数重载: 有多个函数名称相同但是参数的个数、类型、返回值不同,在实际调用时编译器会按照参数的类型、顺序进行自动匹配。

操作符重载:带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的,在使用时只需要直接使用运算符即可,看起来和算数表达式无异。

代码示范:

//
// Created by 11010 on 2023/4/8.
//

#ifndef CPP_11_HEAVYLOAD_TEST_H
#define CPP_11_HEAVYLOAD_TEST_H
#include <iostream>
#include <string>

using namespace std;

class test {
public:
    //定义多个名称相同但是参数个数、顺序、返回值不同的函数
    int max(int a,int b);
    double max(int a,double b);
    void max(string a,int b,int c);

    test();
};


#endif //CPP_11_HEAVYLOAD_TEST_H



//
// Created by 11010 on 2023/4/8.
//

#include "test.h"

//返回较大的数
int test::max(int a, int b) {
    static int Max= a>b? a:b;
    return Max;
}

//返回较大的数
double test::max(int a, double b) {
    static int Max2= a>b? a:b;
    return Max2;
}

//打印三个参数的拼接字符
void test::max(string a, int b, int c) {
  cout<<a<<","<<b<<","<<c<<endl;
}


test::test() {

}



#include <iostream>
#include "test.h"

int main() {


    test *test=new class test();  //申明类对象
    //使用重载函数
    int max1=test->max(12,23);
    int max2=test->max(22,34.567);
    test->max("字符串",12,12);
    cout<<max1<<","<<max2<<endl;

    std::cout << "Hello, World!" << std::endl;
    return 0;
}

二 可重载运算符/不可重载运算符

下面是可重载的运算符列表:

+

-

*

/

%

^

&

|

~

!

,

=

<

>

<=

>=

++

--

<<

>>

==

!=

&&

||

+=

-=

/=

%=

^=

&=

|=

*=

<<=

>>=

[]

()

->

->*

new

new []

delete

delete []

下面是不可重载的运算符列表:

::

.*

.

?:

三 符号重载示范

重载加号运算符代码示范:
//
// Created by 11010 on 2023/4/8.
//

#ifndef CPP_11_HEAVYLOAD_OPERATORDEMO_H
#define CPP_11_HEAVYLOAD_OPERATORDEMO_H
#include <iostream>
#include <string>

using namespace std;

//操作符重载类
class operatorDemo {
public:
    int width;
    int len;
    int height;

    operatorDemo(int w,int l,int h);
    operatorDemo();
    void print(); //打印信息

    //申明重载函数: 重载+号,用于把两个对象的属性相加操作
    operatorDemo operator+(const operatorDemo &op);
};


#endif //CPP_11_HEAVYLOAD_OPERATORDEMO_H





//
// Created by 11010 on 2023/4/8.
//

#include "operatorDemo.h"

operatorDemo::operatorDemo(int w, int l, int h) : width(w), len(l), height(h) {

}

//重载加号,用于使两个对象相加
operatorDemo operatorDemo::operator+(const operatorDemo &op) {
    operatorDemo OP; //临时类变量,用于返回一个新的类对象
    OP.width = width + op.width;
    OP.len = len + op.len;
    OP.height = height + op.height;

    return OP;
}


operatorDemo::operatorDemo() {

}

void operatorDemo::print() {
    cout<<"长:"<<len<<",宽:"<<width<<",高:"<<height<<endl;
}





//重载+号运算符
operatorDemo op1(12,12,12), op2(34,23,14); //申明两个类对象并给出初始值
op1.print();
op2.print();

//使用重载函数使两个对象相加
operatorDemo OP=op1+op2;  //实际上全写为: op1.operator+(op2); 只不过operator和()可以免写罢了
OP.print();







负号运算符重载

//
// Created by 11010 on 2023/4/8.
//

#ifndef CPP_11_HEAVYLOAD_MINUSOPERATOR_H
#define CPP_11_HEAVYLOAD_MINUSOPERATOR_H
#include <iostream>
#include <string>

using namespace std;

//重载减号运算符
class minusOperator {
public:
    int len;
    int width;
    void print();
    minusOperator();
    minusOperator(int l,int w);
    minusOperator operator- (); //重载运算符符号


};


#endif //CPP_11_HEAVYLOAD_MINUSOPERATOR_H



//
// Created by 11010 on 2023/4/8.
//

#include "minusOperator.h"

minusOperator::minusOperator() {
    //给成员赋初值
    len = 0;
    width = 0;
}

minusOperator::minusOperator(int l, int w) : len(l), width(w) {

}

//相当于给参数取相反数
minusOperator minusOperator::operator-() {
    len = -len;
    width = -width;
    return minusOperator(len,width);
}


void minusOperator::print() {
  cout<<"运行结果:"<<len<<","<<width<<endl;
}





//使用重载函数给参数取反
minusOperator mo(12, 34), mo2(33, 99);
minusOperator a = -mo;  //相当于 mo.operator-();
minusOperator b = -mo2; //相当于 mo2.operator-();
a.print();
b.print();







比较运算符的重载示例代码:

//
// Created by 11010 on 2023/4/8.
//

#ifndef CPP_11_HEAVYLOAD_RELATIONOPERATOR_H
#define CPP_11_HEAVYLOAD_RELATIONOPERATOR_H
#include <iostream>

using namespace std;

class relationOperator {
public:
    int len;
    int width;

    void print(); //打印信息
    relationOperator(int l,int w);
    bool operator> (const relationOperator &ro); //重载小于运算符
};


#endif //CPP_11_HEAVYLOAD_RELATIONOPERATOR_H



//
// Created by 11010 on 2023/4/8.
//

#include "relationOperator.h"

void relationOperator::print() {
cout<<"长:"<<len<<",宽:"<<width<<endl;
}


relationOperator::relationOperator(int l, int w):len(l),width(w) {

}


bool relationOperator::operator>(const relationOperator &ro) {
    //只要有一个大于就返回true
    if (len>ro.len)  return true;
    if (width>ro.width) return true;
    //均不大于返回false
    return false;
}





 //重载比较运算大于号,用于比较两个对象是否相等
 relationOperator relationOperator(12,34), relationOperator1(99,23);
string max= relationOperator>relationOperator1? "大于" : "不大于";
cout<<max<<endl;









输入输出运算符重载示例代码:



//
// Created by 11010 on 2023/4/8.
//

#ifndef CPP_11_HEAVYLOAD_OUTANDINOPERATOR_H
#define CPP_11_HEAVYLOAD_OUTANDINOPERATOR_H
#include <iostream>
#include <string>

using namespace std;


//重载输入、输出运算符
class outAndInOperator {
public:
    int len;
    int width;

    void print();
    outAndInOperator(int l,int w);
    //重载输入输出流符号需要使用friend申明为友元函数
   friend istream & operator>> (istream &in, outAndInOperator &io); //输入流重载
   friend ostream & operator<< (ostream &on,const outAndInOperator &oo); //输出流重载


};


#endif //CPP_11_HEAVYLOAD_OUTANDINOPERATOR_H





//
// Created by 11010 on 2023/4/8.
//


#include "outAndInOperator.h"

void outAndInOperator::print() {
 cout<<"长:"<<len<<",宽:"<<width<<endl;
}

outAndInOperator::outAndInOperator(int l, int w):len(l),width(w) {

}

//输入流重载
istream &operator>>(istream &in, outAndInOperator &io) {
    in>>io.len>>io.width;  //调用系统重载把当前参数加入输入流
    return in; //返回输入流对象
}

ostream &operator<<(ostream &on,const outAndInOperator &oo) {
    on<<"len: "<<oo.len<<", width:"<<oo.width<<endl;
    return on;
}






//建立三个对象
outAndInOperator ot(120,234), ot2(980,1234),ot3(0,0);
ot.print();
ot2.print();
ot3.print();

 //使用重载后的输入输出流符号
 cout<<"请输入长和宽"<<endl;
 cin>>ot3; //把参数保存到ot3里面
 cout<<"============================================"<<endl;
 cout<<ot<<","<<endl;
 cout<<ot2<<","<<endl;
 cout<<ot3<<","<<endl;









自增运算符重载示例代码:

//
// Created by 11010 on 2023/4/8.
//

#ifndef CPP_11_HEAVYLOAD_PLUSPLUSOPERATOR_H
#define CPP_11_HEAVYLOAD_PLUSPLUSOPERATOR_H

#include <iostream>

using namespace std;

//重载自增运算符:++
class PlusPlusOperator {

public:
    int len;
    int width;

    PlusPlusOperator(int l,int w);
    PlusPlusOperator operator++ (); //重载前置++运算符
    PlusPlusOperator operator++ (int); //重载后置++运算符
    void print(); //打印信息
};


#endif //CPP_11_HEAVYLOAD_PLUSPLUSOPERATOR_H





//
// Created by 11010 on 2023/4/8.
//

#include "PlusPlusOperator.h"

PlusPlusOperator::PlusPlusOperator(int l, int w):len(l),width(w) {

}

//前置++重载
PlusPlusOperator PlusPlusOperator::operator++() {
    ++len;
    ++width;
    //使用构造函数返回自增后的值
    return PlusPlusOperator(len, width);
}

//后置++重载
PlusPlusOperator PlusPlusOperator::operator++(int) {
    //先构造函数,返回为自增之前的值
    PlusPlusOperator p(len,width);
    //开始自增: 因为后置自增的逻辑是先使用变量在自增
    ++len;
    ++width;

    //返回原始对象,
    return p;
}

void PlusPlusOperator::print() {
 cout<<"len: "<<len<<", width: "<<width<<endl;
}






cout << "============================================" << endl;
//使用重载后的++运算符: 一种前置++,一种后置++
PlusPlusOperator p(11, 12), p2(10, 10);
p++;
p.print();
++p;
p.print();

p2++;
p2.print();
++p2;
p2.print();







等号运算符重载示例代码:

//
// Created by 11010 on 2023/4/8.
//

#ifndef CPP_11_HEAVYLOAD_EQUALOPERATOR_H
#define CPP_11_HEAVYLOAD_EQUALOPERATOR_H
#include <iostream>

using namespace std;

//重载赋值运算符: 其实与加号运算符原理一样
class EqualOperator {
public:
    int len;
    int width;

    EqualOperator(int l,int w);
    void operator= (const EqualOperator &equalOperator);
    void print();
};


#endif //CPP_11_HEAVYLOAD_EQUALOPERATOR_H





//
// Created by 11010 on 2023/4/8.
//

#include "EqualOperator.h"

//构造函数
EqualOperator::EqualOperator(int l, int w) :len(l),width(w){

}

//重载等号运算符
void EqualOperator::operator= (const EqualOperator &equalOperator) {
len=equalOperator.len;
width=equalOperator.width;
}

//打印消息
void EqualOperator::print() {
cout<<"len: "<<len<<", width: "<<width<<endl;
}






  cout << "============================================" << endl;
EqualOperator eq(10,10);
eq.print(); //打印原始值
EqualOperator eq2(30,30);
eq2.print(); //打印原始值
//使用重载等号函数,把eq2赋给eq
eq=eq2;
eq.print(); //打印改变后的值 ,结果为: len: 30, width: 30







重载()代码示例:

//
// Created by 11010 on 2023/4/8.
//

#ifndef CPP_11_HEAVYLOAD_BRACKETSOPERATOR_H
#define CPP_11_HEAVYLOAD_BRACKETSOPERATOR_H
#include <iostream>

using namespace std;


//括号运算符重载: 目的是在使用时可以重载任意多个参数
class bracketsOperator {
public:
    int max;
    int min;

    bracketsOperator(int max,int min);
    bracketsOperator operator() (int a,int b,int c); //括号运算符重载: 目的是在使用时可以重载任意多个参数
    void print();
};


#endif //CPP_11_HEAVYLOAD_BRACKETSOPERATOR_H





//
// Created by 11010 on 2023/4/8.
//

#include "bracketsOperator.h"

//构造函数
bracketsOperator::bracketsOperator(int max, int min):max(max),min(min) {

}

//重载()运算符
bracketsOperator bracketsOperator::operator()(int a, int b, int c) {
    max=a+b+c;
    min= (a>b? a:b)-c ;
    return bracketsOperator(max, min);
}


//打印消息
void bracketsOperator::print() {
cout<<"max: "<<max<<", min: "<<min<<endl;
}





cout << "============================================" << endl;
bracketsOperator bo(100,0);
bo.print();

bracketsOperator bo2(0,0);
//使用重载括号函数,里面可以填入三个参数
bo2=bo(11,11,22);
bo2.print();  //输出结果:  max: 44, min: -11





数组下标重载演示代码:

//
// Created by 11010 on 2023/4/8.
//

#ifndef CPP_11_HEAVYLOAD_INDEXOPERATOR_H
#define CPP_11_HEAVYLOAD_INDEXOPERATOR_H

#include <iostream>

using namespace std;

//给出数组长度
const int indexLength = 20;


//重载数组下标
class indexOperator {
public:
    indexOperator();
    int &operator[](int index); //重载数组下标
    void print();

private:
    int array[indexLength];
};


#endif //CPP_11_HEAVYLOAD_INDEXOPERATOR_H





//
// Created by 11010 on 2023/4/8.
//

#include "indexOperator.h"

//构造函数: 初始化数组
indexOperator::indexOperator() {
    for (int i = 0; i < indexLength; ++i) {
        array[i] = i * 2 + 1;
    }
}

//重载数组下标符 [] :当使用的数组超过最大下标时做出提示,而不会使程序崩溃
int &indexOperator::operator[](int index) {
    if (index > indexLength) {
        cout << "数组下标非法,超过数组的最大长度! 数组最大长度为:" << indexLength << endl;
        return array[0]; //默认返回第一个数组元素
    }
    return array[index];
}

void indexOperator::print() {
   cout<<"数组元素为:"<<endl;
    for (int i = 0; i < indexLength; ++i) {
        cout<<array[i]<<",";
    }
    cout<<"一共有"<<indexLength<<"个元素"<<endl;
}





cout << "============================================" << endl;
indexOperator io;
io.print(); //正常调用打印函数输出数组
cout<<io[1]<<endl; //直接使用类名加下标的方式访问数组
io[indexLength+10]; //越界访问,但是程序不会异常,会在控制台提示



完整代码clone地址: git@gitcode.net:XiaoWang_csdn/cpp_11_heavyload.git

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code_shenbing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值