扬州市网站建设_网站建设公司_网站制作_seo优化
2025/12/18 0:59:23 网站建设 项目流程

C++ 允许在同一作用域中的某个函数运算符指定多个定义,分别称为函数重载运算符重载

重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是它们的参数列表和定义(实现)不相同。

当您调用一个重载函数重载运算符时,编译器通过把您所使用的参数类型与定义中的参数类型进行比较,决定选用最合适的定义。选择最合适的重载函数或重载运算符的过程,称为重载决策

C++ 中的函数重载

在同一个作用域内,可以声明几个功能类似的同名函数,但是这些同名函数的形式参数(指参数的个数、类型或者顺序)必须不同。您不能仅通过返回类型的不同来重载函数。

下面的实例中,同名函数print()被用于输出不同的数据类型:

实例

#include <iostream> using namespace std; class printData { public: void print(int i) { cout << "整数为: " << i << endl; } void print(double f) { cout << "浮点数为: " << f << endl; } void print(char c[]) { cout << "字符串为: " << c << endl; } }; int main(void) { printData pd; // 输出整数 pd.print(5); // 输出浮点数 pd.print(500.263); // 输出字符串 char c[] = "Hello C++"; pd.print(c); return 0; }

当上面的代码被编译和执行时,它会产生下列结果:

整数为: 5 浮点数为: 500.263 字符串为: Hello C++

C++ 中的运算符重载

您可以重定义或重载大部分 C++ 内置的运算符。这样,您就能使用自定义类型的运算符。

重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。

Box operator+(const Box&);

声明加法运算符用于把两个 Box 对象相加,返回最终的 Box 对象。大多数的重载运算符可被定义为普通的非成员函数或者被定义为类成员函数。如果我们定义上面的函数为类的非成员函数,那么我们需要为每次操作传递两个参数,如下所示:

Box operator+(const Box&, const Box&);

下面的实例使用成员函数演示了运算符重载的概念。在这里,对象作为参数进行传递,对象的属性使用this运算符进行访问,如下所示:

实例

#include <iostream> using namespace std; class Box { public: double getVolume(void) { return length * breadth * height; } void setLength( double len ) { length = len; } void setBreadth( double bre ) { breadth = bre; } void setHeight( double hei ) { height = hei; } //

https://avg.163.com/topic/detail/8054846
https://avg.163.com/topic/detail/8054871
https://avg.163.com/topic/detail/8055619
https://avg.163.com/topic/detail/8054933
https://avg.163.com/topic/detail/8055654
https://avg.163.com/topic/detail/8055089
https://avg.163.com/topic/detail/8055678
https://avg.163.com/topic/detail/8055253
https://avg.163.com/topic/detail/8054844
https://avg.163.com/topic/detail/8055706
https://avg.163.com/topic/detail/8054922
https://avg.163.com/topic/detail/8055731
https://avg.163.com/topic/detail/8055065
https://avg.163.com/topic/detail/8055260
https://avg.163.com/topic/detail/8054843
https://avg.163.com/topic/detail/8054870
https://avg.163.com/topic/detail/8054913
https://avg.163.com/topic/detail/8055094
https://avg.163.com/topic/detail/8055241
https://avg.163.com/topic/detail/8055653
https://avg.163.com/topic/detail/8055674
https://avg.163.com/topic/detail/8055703
https://avg.163.com/topic/detail/8055727
https://avg.163.com/topic/detail/8055622
https://avg.163.com/topic/detail/8055651
https://avg.163.com/topic/detail/8055676
https://avg.163.com/topic/detail/8055705
https://avg.163.com/topic/detail/8055729
https://avg.163.com/topic/detail/8055621
https://avg.163.com/topic/detail/8055650
https://avg.163.com/topic/detail/8055673
https://avg.163.com/topic/detail/8055700
https://avg.163.com/topic/detail/8055725
https://avg.163.com/topic/detail/8055646
https://avg.163.com/topic/detail/8055672
https://avg.163.com/topic/detail/8055699
https://avg.163.com/topic/detail/8055722
https://avg.163.com/topic/detail/8055618
https://avg.163.com/topic/detail/8055648
https://avg.163.com/topic/detail/8055677
https://avg.163.com/topic/detail/8055701
https://avg.163.com/topic/detail/8055723
https://avg.163.com/topic/detail/8056185
https://avg.163.com/topic/detail/8056199
https://avg.163.com/topic/detail/8056197
https://avg.163.com/topic/detail/8056796
https://avg.163.com/topic/detail/8056179
https://avg.163.com/topic/detail/8056195
https://avg.163.com/topic/detail/8056308
https://avg.163.com/topic/detail/8056848
https://avg.163.com/topic/detail/8056309
https://avg.163.com/topic/detail/8056183
https://avg.163.com/topic/detail/8056182
https://avg.163.com/topic/detail/8056200
https://avg.163.com/topic/detail/8056762
https://avg.163.com/topic/detail/8056181
https://avg.163.com/topic/detail/8056198
https://avg.163.com/topic/detail/8056177
https://avg.163.com/topic/detail/8056196
https://avg.163.com/topic/detail/8056288
https://avg.163.com/topic/detail/8056178
https://avg.163.com/topic/detail/8056194
https://avg.163.com/topic/detail/8056175
https://avg.163.com/topic/detail/8056193
https://avg.163.com/topic/detail/8053775
https://avg.163.com/topic/detail/8053815
https://avg.163.com/topic/detail/8053842
https://avg.163.com/topic/detail/8053858
https://avg.163.com/topic/detail/8053894
https://avg.163.com/topic/detail/8053773
https://avg.163.com/topic/detail/8053809
https://avg.163.com/topic/detail/8053845
https://avg.163.com/topic/detail/8053867
https://avg.163.com/topic/detail/8053892
https://avg.163.com/topic/detail/8053770
https://avg.163.com/topic/detail/8053806
https://avg.163.com/topic/detail/8053839
https://avg.163.com/topic/detail/8053863
https://avg.163.com/topic/detail/8053889
https://avg.163.com/topic/detail/8053767
https://avg.163.com/topic/detail/8053805
https://avg.163.com/topic/detail/8053843
https://avg.163.com/topic/detail/8053868
https://avg.163.com/topic/detail/8053891
https://avg.163.com/topic/detail/8053774
https://avg.163.com/topic/detail/8053810
https://avg.163.com/topic/detail/8053840
https://avg.163.com/topic/detail/8053869
https://avg.163.com/topic/detail/8053893
https://avg.163.com/topic/detail/8053771
https://avg.163.com/topic/detail/8053804
https://avg.163.com/topic/detail/8053837
https://avg.163.com/topic/detail/8053864
https://avg.163.com/topic/detail/8053887
https://avg.163.com/topic/detail/8053766
https://avg.163.com/topic/detail/8053801
https://avg.163.com/topic/detail/8053841
https://avg.163.com/topic/detail/8053862
https://avg.163.com/topic/detail/8053890
https://avg.163.com/topic/detail/8053769
https://avg.163.com/topic/detail/8053799
https://avg.163.com/topic/detail/8053844
https://avg.163.com/topic/detail/8053860
https://avg.163.com/topic/detail/8053888
https://avg.163.com/topic/detail/8053763
https://avg.163.com/topic/detail/8053803
https://avg.163.com/topic/detail/8053838
https://avg.163.com/topic/detail/8053861
https://avg.163.com/topic/detail/8053772
https://avg.163.com/topic/detail/8053885
https://avg.163.com/topic/detail/8053802
https://avg.163.com/topic/detail/8053768
https://avg.163.com/topic/detail/8053836
https://avg.163.com/topic/detail/8053800
https://avg.163.com/topic/detail/8053857
https://avg.163.com/topic/detail/8053835
https://avg.163.com/topic/detail/8053886
https://avg.163.com/topic/detail/8053856
https://avg.163.com/topic/detail/8053882
https://avg.163.com/topic/detail/8053765
https://avg.163.com/topic/detail/8053798
https://avg.163.com/topic/detail/8053834
https://avg.163.com/topic/detail/8053855
https://avg.163.com/topic/detail/8053884
https://avg.163.com/topic/detail/8053762
https://avg.163.com/topic/detail/8053761
https://avg.163.com/topic/detail/8053797
https://avg.163.com/topic/detail/8053795
https://avg.163.com/topic/detail/8053833
https://avg.163.com/topic/detail/8053832
https://avg.163.com/topic/detail/8053853
https://avg.163.com/topic/detail/8053854
https://avg.163.com/topic/detail/8053881
https://avg.163.com/topic/detail/8053883
https://avg.163.com/topic/detail/8053613
https://avg.163.com/topic/detail/8053651
https://avg.163.com/topic/detail/8053673
https://avg.163.com/topic/detail/8053696
https://avg.163.com/topic/detail/8053718
https://avg.163.com/topic/detail/8053606
https://avg.163.com/topic/detail/8053610
https://avg.163.com/topic/detail/8053647
https://avg.163.com/topic/detail/8053650
https://avg.163.com/topic/detail/8053671
https://avg.163.com/topic/detail/8053670
https://avg.163.com/topic/detail/8053690
https://avg.163.com/topic/detail/8053695
https://avg.163.com/topic/detail/8053713
https://avg.163.com/topic/detail/8053716
https://avg.163.com/topic/detail/8053615
https://avg.163.com/topic/detail/8053641
https://avg.163.com/topic/detail/8053666
https://avg.163.com/topic/detail/8053686
https://avg.163.com/topic/detail/8053607
https://avg.163.com/topic/detail/8053611
https://avg.163.com/topic/detail/8053645
https://avg.163.com/topic/detail/8053646
https://avg.163.com/topic/detail/8053667
https://avg.163.com/topic/detail/8053669
https://avg.163.com/topic/detail/8053712
https://avg.163.com/topic/detail/8053692
https://avg.163.com/topic/detail/8053734
https://avg.163.com/topic/detail/8053717
https://avg.163.com/topic/detail/8053603
https://avg.163.com/topic/detail/8053707
https://avg.163.com/topic/detail/8053644
https://avg.163.com/topic/detail/8053617
https://avg.163.com/topic/detail/8053665
https://avg.163.com/topic/detail/8053668
https://avg.163.com/topic/detail/8053694
https://avg.163.com/topic/detail/8053689
https://avg.163.com/topic/detail/8053711
https://avg.163.com/topic/detail/8053708
https://avg.163.com/topic/detail/8053612
https://avg.163.com/topic/detail/8053639
https://avg.163.com/topic/detail/8053663
https://avg.163.com/topic/detail/8053688
https://avg.163.com/topic/detail/8053709
https://avg.163.com/topic/detail/8053609
https://avg.163.com/topic/detail/8053640
https://avg.163.com/topic/detail/8053664
https://avg.163.com/topic/detail/8053691
https://avg.163.com/topic/detail/8053710
https://avg.163.com/topic/detail/8053638
https://avg.163.com/topic/detail/8053660
https://avg.163.com/topic/detail/8053684
https://avg.163.com/topic/detail/8053705
https://avg.163.com/topic/detail/8053725
https://avg.163.com/topic/detail/8053608
https://avg.163.com/topic/detail/8053637
https://avg.163.com/topic/detail/8053662
https://avg.163.com/topic/detail/8053687
https://avg.163.com/topic/detail/8053706
https://avg.163.com/topic/detail/8053601
https://avg.163.com/topic/detail/8053635
https://avg.163.com/topic/detail/8053659
https://avg.163.com/topic/detail/8053598
https://avg.163.com/topic/detail/8053685
https://avg.163.com/topic/detail/8053634
https://avg.163.com/topic/detail/8053703
https://avg.163.com/topic/detail/8053658
https://avg.163.com/topic/detail/8053414
https://avg.163.com/topic/detail/8053683
https://avg.163.com/topic/detail/8053449
https://avg.163.com/topic/detail/8053704
https://avg.163.com/topic/detail/8053482
https://avg.163.com/topic/detail/8053506
https://avg.163.com/topic/detail/8053526
https://avg.163.com/topic/detail/8053403
https://avg.163.com/topic/detail/8053452
https://avg.163.com/topic/detail/8053480
https://avg.163.com/topic/detail/8053502
https://avg.163.com/topic/detail/8053529
https://avg.163.com/topic/detail/8053399
https://avg.163.com/topic/detail/8053455
https://avg.163.com/topic/detail/8053481
https://avg.163.com/topic/detail/8053504
https://avg.163.com/topic/detail/8053523
https://avg.163.com/topic/detail/8053400
https://avg.163.com/topic/detail/8053445
https://avg.163.com/topic/detail/8053478
https://avg.163.com/topic/detail/8053499
https://avg.163.com/topic/detail/8053527
https://avg.163.com/topic/detail/8053396
https://avg.163.com/topic/detail/8053447
https://avg.163.com/topic/detail/8053479
https://avg.163.com/topic/detail/8053497
https://avg.163.com/topic/detail/8053522
https://avg.163.com/topic/detail/8053388
https://avg.163.com/topic/detail/8053450
https://avg.163.com/topic/detail/8053475
https://avg.163.com/topic/detail/8053498
https://avg.163.com/topic/detail/8053533
https://avg.163.com/topic/detail/8053392
https://avg.163.com/topic/detail/8053453
https://avg.163.com/topic/detail/8053476
https://avg.163.com/topic/detail/8053500
https://avg.163.com/topic/detail/8053531
https://avg.163.com/topic/detail/8053386
https://avg.163.com/topic/detail/8053454
https://avg.163.com/topic/detail/8053474
https://avg.163.com/topic/detail/8053494
https://avg.163.com/topic/detail/8053520
https://avg.163.com/topic/detail/8053404
https://avg.163.com/topic/detail/8053446
https://avg.163.com/topic/detail/8053469
https://avg.163.com/topic/detail/8053492
https://avg.163.com/topic/detail/8053521
https://avg.163.com/topic/detail/8053409
https://avg.163.com/topic/detail/8053451
https://avg.163.com/topic/detail/8053470
https://avg.163.com/topic/detail/8053496
https://avg.163.com/topic/detail/8053528
https://avg.163.com/topic/detail/8053398
https://avg.163.com/topic/detail/8053448
https://avg.163.com/topic/detail/8053471
https://avg.163.com/topic/detail/8053495
https://avg.163.com/topic/detail/8053519
https://avg.163.com/topic/detail/8053391
https://avg.163.com/topic/detail/8053443
https://avg.163.com/topic/detail/8053468
https://avg.163.com/topic/detail/8053491
https://avg.163.com/topic/detail/8053518
https://avg.163.com/topic/detail/8053387
https://avg.163.com/topic/detail/8053444
https://avg.163.com/topic/detail/8053472
https://avg.163.com/topic/detail/8053493
https://avg.163.com/topic/detail/8053524
https://avg.163.com/topic/detail/8053385
https://avg.163.com/topic/detail/8053442
https://avg.163.com/topic/detail/8053467
https://avg.163.com/topic/detail/8053490
https://avg.163.com/topic/detail/8053517
https://avg.163.com/topic/detail/8053212
https://avg.163.com/topic/detail/8053236
https://avg.163.com/topic/detail/8053253
https://avg.163.com/topic/detail/8053261
https://avg.163.com/topic/detail/8053214
https://avg.163.com/topic/detail/8053288
https://avg.163.com/topic/detail/8053235
https://avg.163.com/topic/detail/8053254
https://avg.163.com/topic/detail/8053263
https://avg.163.com/topic/detail/8053289
https://avg.163.com/topic/detail/8053217
https://avg.163.com/topic/detail/8053234
https://avg.163.com/topic/detail/8053251
https://avg.163.com/topic/detail/8053265
https://avg.163.com/topic/detail/8053285
https://avg.163.com/topic/detail/8052452
https://avg.163.com/topic/detail/8052460
https://avg.163.com/topic/detail/8053215
https://avg.163.com/topic/detail/8053233
https://avg.163.com/topic/detail/8053250
https://avg.163.com/topic/detail/8052453
https://avg.163.com/topic/detail/8052459
https://avg.163.com/topic/detail/8053211
https://avg.163.com/topic/detail/8053231
https://avg.163.com/topic/detail/8053245
https://avg.163.com/topic/detail/8053207

重载 + 运算符,用于把两个 Box 对象相加 Box operator+(const Box& b) { Box box; box.length = this->length + b.length; box.breadth = this->breadth + b.breadth; box.height = this->height + b.height; return box; } private: double length; // 长度 double breadth; // 宽度 double height; // 高度 }; // 程序的主函数 int main( ) { Box Box1; // 声明 Box1,类型为 Box Box Box2; // 声明 Box2,类型为 Box Box Box3; // 声明 Box3,类型为 Box double volume = 0.0; // 把体积存储在该变量中 // Box1 详述 Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0); // Box2 详述 Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0); // Box1 的体积 volume = Box1.getVolume(); cout << "Volume of Box1 : " << volume <<endl; // Box2 的体积 volume = Box2.getVolume(); cout << "Volume of Box2 : " << volume <<endl; // 把两个对象相加,得到 Box3 Box3 = Box1 + Box2; // Box3 的体积 volume = Box3.getVolume(); cout << "Volume of Box3 : " << volume <<endl; return 0; }

当上面的代码被编译和执行时,它会产生下列结果:

Volume of Box1 : 210 Volume of Box2 : 1560 Volume of Box3 : 5400

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

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

双目算术运算符+ (加),-(减),*(乘),/(除),% (取模)
关系运算符==(等于),!= (不等于),< (小于),> (大于),<=(小于等于),>=(大于等于)
逻辑运算符||(逻辑或),&&(逻辑与),!(逻辑非)
单目运算符+ (正),-(负),*(指针),&(取地址)
自增自减运算符++(自增),--(自减)
位运算符| (按位或),& (按位与),~(按位取反),^(按位异或),,<< (左移),>>(右移)
赋值运算符=, +=, -=, *=, /= , % = , &=, |=, ^=, <<=, >>=
空间申请与释放new, delete, new[ ] , delete[]
其他运算符()(函数调用),->(成员访问),,(逗号),[](下标)

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

  • .:成员访问运算符
  • .*, ->*:成员指针访问运算符
  • :::域运算符
  • sizeof:长度运算符
  • ?::条件运算符
  • #: 预处理符号

运算符重载实例

下面提供了各种运算符重载的实例,帮助您更好地理解重载的概念。

序号运算符和实例
1一元运算符重载
2二元运算符重载
3关系运算符重载
4输入/输出运算符重载
5++ 和 -- 运算符重载
6赋值运算符重载
7函数调用运算符 () 重载
8下标运算符 [] 重载
9类成员访问运算符 -> 重载

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询