运营网站团队建设,个人养老金制度将推,扬中网站推广导流,用html制作简易网页的教程C 允许在同一作用域中的某个函数和运算符指定多个定义#xff0c;分别称为函数重载和运算符重载。
重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明#xff0c;但是它们的参数列表和定义#xff08;实现#xff09;不相同。
当您调用一个重…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 CC 中的运算符重载您可以重定义或重载大部分 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/8054846https://avg.163.com/topic/detail/8054871https://avg.163.com/topic/detail/8055619https://avg.163.com/topic/detail/8054933https://avg.163.com/topic/detail/8055654https://avg.163.com/topic/detail/8055089https://avg.163.com/topic/detail/8055678https://avg.163.com/topic/detail/8055253https://avg.163.com/topic/detail/8054844https://avg.163.com/topic/detail/8055706https://avg.163.com/topic/detail/8054922https://avg.163.com/topic/detail/8055731https://avg.163.com/topic/detail/8055065https://avg.163.com/topic/detail/8055260https://avg.163.com/topic/detail/8054843https://avg.163.com/topic/detail/8054870https://avg.163.com/topic/detail/8054913https://avg.163.com/topic/detail/8055094https://avg.163.com/topic/detail/8055241https://avg.163.com/topic/detail/8055653https://avg.163.com/topic/detail/8055674https://avg.163.com/topic/detail/8055703https://avg.163.com/topic/detail/8055727https://avg.163.com/topic/detail/8055622https://avg.163.com/topic/detail/8055651https://avg.163.com/topic/detail/8055676https://avg.163.com/topic/detail/8055705https://avg.163.com/topic/detail/8055729https://avg.163.com/topic/detail/8055621https://avg.163.com/topic/detail/8055650https://avg.163.com/topic/detail/8055673https://avg.163.com/topic/detail/8055700https://avg.163.com/topic/detail/8055725https://avg.163.com/topic/detail/8055646https://avg.163.com/topic/detail/8055672https://avg.163.com/topic/detail/8055699https://avg.163.com/topic/detail/8055722https://avg.163.com/topic/detail/8055618https://avg.163.com/topic/detail/8055648https://avg.163.com/topic/detail/8055677https://avg.163.com/topic/detail/8055701https://avg.163.com/topic/detail/8055723https://avg.163.com/topic/detail/8056185https://avg.163.com/topic/detail/8056199https://avg.163.com/topic/detail/8056197https://avg.163.com/topic/detail/8056796https://avg.163.com/topic/detail/8056179https://avg.163.com/topic/detail/8056195https://avg.163.com/topic/detail/8056308https://avg.163.com/topic/detail/8056848https://avg.163.com/topic/detail/8056309https://avg.163.com/topic/detail/8056183https://avg.163.com/topic/detail/8056182https://avg.163.com/topic/detail/8056200https://avg.163.com/topic/detail/8056762https://avg.163.com/topic/detail/8056181https://avg.163.com/topic/detail/8056198https://avg.163.com/topic/detail/8056177https://avg.163.com/topic/detail/8056196https://avg.163.com/topic/detail/8056288https://avg.163.com/topic/detail/8056178https://avg.163.com/topic/detail/8056194https://avg.163.com/topic/detail/8056175https://avg.163.com/topic/detail/8056193https://avg.163.com/topic/detail/8053775https://avg.163.com/topic/detail/8053815https://avg.163.com/topic/detail/8053842https://avg.163.com/topic/detail/8053858https://avg.163.com/topic/detail/8053894https://avg.163.com/topic/detail/8053773https://avg.163.com/topic/detail/8053809https://avg.163.com/topic/detail/8053845https://avg.163.com/topic/detail/8053867https://avg.163.com/topic/detail/8053892https://avg.163.com/topic/detail/8053770https://avg.163.com/topic/detail/8053806https://avg.163.com/topic/detail/8053839https://avg.163.com/topic/detail/8053863https://avg.163.com/topic/detail/8053889https://avg.163.com/topic/detail/8053767https://avg.163.com/topic/detail/8053805https://avg.163.com/topic/detail/8053843https://avg.163.com/topic/detail/8053868https://avg.163.com/topic/detail/8053891https://avg.163.com/topic/detail/8053774https://avg.163.com/topic/detail/8053810https://avg.163.com/topic/detail/8053840https://avg.163.com/topic/detail/8053869https://avg.163.com/topic/detail/8053893https://avg.163.com/topic/detail/8053771https://avg.163.com/topic/detail/8053804https://avg.163.com/topic/detail/8053837https://avg.163.com/topic/detail/8053864https://avg.163.com/topic/detail/8053887https://avg.163.com/topic/detail/8053766https://avg.163.com/topic/detail/8053801https://avg.163.com/topic/detail/8053841https://avg.163.com/topic/detail/8053862https://avg.163.com/topic/detail/8053890https://avg.163.com/topic/detail/8053769https://avg.163.com/topic/detail/8053799https://avg.163.com/topic/detail/8053844https://avg.163.com/topic/detail/8053860https://avg.163.com/topic/detail/8053888https://avg.163.com/topic/detail/8053763https://avg.163.com/topic/detail/8053803https://avg.163.com/topic/detail/8053838https://avg.163.com/topic/detail/8053861https://avg.163.com/topic/detail/8053772https://avg.163.com/topic/detail/8053885https://avg.163.com/topic/detail/8053802https://avg.163.com/topic/detail/8053768https://avg.163.com/topic/detail/8053836https://avg.163.com/topic/detail/8053800https://avg.163.com/topic/detail/8053857https://avg.163.com/topic/detail/8053835https://avg.163.com/topic/detail/8053886https://avg.163.com/topic/detail/8053856https://avg.163.com/topic/detail/8053882https://avg.163.com/topic/detail/8053765https://avg.163.com/topic/detail/8053798https://avg.163.com/topic/detail/8053834https://avg.163.com/topic/detail/8053855https://avg.163.com/topic/detail/8053884https://avg.163.com/topic/detail/8053762https://avg.163.com/topic/detail/8053761https://avg.163.com/topic/detail/8053797https://avg.163.com/topic/detail/8053795https://avg.163.com/topic/detail/8053833https://avg.163.com/topic/detail/8053832https://avg.163.com/topic/detail/8053853https://avg.163.com/topic/detail/8053854https://avg.163.com/topic/detail/8053881https://avg.163.com/topic/detail/8053883https://avg.163.com/topic/detail/8053613https://avg.163.com/topic/detail/8053651https://avg.163.com/topic/detail/8053673https://avg.163.com/topic/detail/8053696https://avg.163.com/topic/detail/8053718https://avg.163.com/topic/detail/8053606https://avg.163.com/topic/detail/8053610https://avg.163.com/topic/detail/8053647https://avg.163.com/topic/detail/8053650https://avg.163.com/topic/detail/8053671https://avg.163.com/topic/detail/8053670https://avg.163.com/topic/detail/8053690https://avg.163.com/topic/detail/8053695https://avg.163.com/topic/detail/8053713https://avg.163.com/topic/detail/8053716https://avg.163.com/topic/detail/8053615https://avg.163.com/topic/detail/8053641https://avg.163.com/topic/detail/8053666https://avg.163.com/topic/detail/8053686https://avg.163.com/topic/detail/8053607https://avg.163.com/topic/detail/8053611https://avg.163.com/topic/detail/8053645https://avg.163.com/topic/detail/8053646https://avg.163.com/topic/detail/8053667https://avg.163.com/topic/detail/8053669https://avg.163.com/topic/detail/8053712https://avg.163.com/topic/detail/8053692https://avg.163.com/topic/detail/8053734https://avg.163.com/topic/detail/8053717https://avg.163.com/topic/detail/8053603https://avg.163.com/topic/detail/8053707https://avg.163.com/topic/detail/8053644https://avg.163.com/topic/detail/8053617https://avg.163.com/topic/detail/8053665https://avg.163.com/topic/detail/8053668https://avg.163.com/topic/detail/8053694https://avg.163.com/topic/detail/8053689https://avg.163.com/topic/detail/8053711https://avg.163.com/topic/detail/8053708https://avg.163.com/topic/detail/8053612https://avg.163.com/topic/detail/8053639https://avg.163.com/topic/detail/8053663https://avg.163.com/topic/detail/8053688https://avg.163.com/topic/detail/8053709https://avg.163.com/topic/detail/8053609https://avg.163.com/topic/detail/8053640https://avg.163.com/topic/detail/8053664https://avg.163.com/topic/detail/8053691https://avg.163.com/topic/detail/8053710https://avg.163.com/topic/detail/8053638https://avg.163.com/topic/detail/8053660https://avg.163.com/topic/detail/8053684https://avg.163.com/topic/detail/8053705https://avg.163.com/topic/detail/8053725https://avg.163.com/topic/detail/8053608https://avg.163.com/topic/detail/8053637https://avg.163.com/topic/detail/8053662https://avg.163.com/topic/detail/8053687https://avg.163.com/topic/detail/8053706https://avg.163.com/topic/detail/8053601https://avg.163.com/topic/detail/8053635https://avg.163.com/topic/detail/8053659https://avg.163.com/topic/detail/8053598https://avg.163.com/topic/detail/8053685https://avg.163.com/topic/detail/8053634https://avg.163.com/topic/detail/8053703https://avg.163.com/topic/detail/8053658https://avg.163.com/topic/detail/8053414https://avg.163.com/topic/detail/8053683https://avg.163.com/topic/detail/8053449https://avg.163.com/topic/detail/8053704https://avg.163.com/topic/detail/8053482https://avg.163.com/topic/detail/8053506https://avg.163.com/topic/detail/8053526https://avg.163.com/topic/detail/8053403https://avg.163.com/topic/detail/8053452https://avg.163.com/topic/detail/8053480https://avg.163.com/topic/detail/8053502https://avg.163.com/topic/detail/8053529https://avg.163.com/topic/detail/8053399https://avg.163.com/topic/detail/8053455https://avg.163.com/topic/detail/8053481https://avg.163.com/topic/detail/8053504https://avg.163.com/topic/detail/8053523https://avg.163.com/topic/detail/8053400https://avg.163.com/topic/detail/8053445https://avg.163.com/topic/detail/8053478https://avg.163.com/topic/detail/8053499https://avg.163.com/topic/detail/8053527https://avg.163.com/topic/detail/8053396https://avg.163.com/topic/detail/8053447https://avg.163.com/topic/detail/8053479https://avg.163.com/topic/detail/8053497https://avg.163.com/topic/detail/8053522https://avg.163.com/topic/detail/8053388https://avg.163.com/topic/detail/8053450https://avg.163.com/topic/detail/8053475https://avg.163.com/topic/detail/8053498https://avg.163.com/topic/detail/8053533https://avg.163.com/topic/detail/8053392https://avg.163.com/topic/detail/8053453https://avg.163.com/topic/detail/8053476https://avg.163.com/topic/detail/8053500https://avg.163.com/topic/detail/8053531https://avg.163.com/topic/detail/8053386https://avg.163.com/topic/detail/8053454https://avg.163.com/topic/detail/8053474https://avg.163.com/topic/detail/8053494https://avg.163.com/topic/detail/8053520https://avg.163.com/topic/detail/8053404https://avg.163.com/topic/detail/8053446https://avg.163.com/topic/detail/8053469https://avg.163.com/topic/detail/8053492https://avg.163.com/topic/detail/8053521https://avg.163.com/topic/detail/8053409https://avg.163.com/topic/detail/8053451https://avg.163.com/topic/detail/8053470https://avg.163.com/topic/detail/8053496https://avg.163.com/topic/detail/8053528https://avg.163.com/topic/detail/8053398https://avg.163.com/topic/detail/8053448https://avg.163.com/topic/detail/8053471https://avg.163.com/topic/detail/8053495https://avg.163.com/topic/detail/8053519https://avg.163.com/topic/detail/8053391https://avg.163.com/topic/detail/8053443https://avg.163.com/topic/detail/8053468https://avg.163.com/topic/detail/8053491https://avg.163.com/topic/detail/8053518https://avg.163.com/topic/detail/8053387https://avg.163.com/topic/detail/8053444https://avg.163.com/topic/detail/8053472https://avg.163.com/topic/detail/8053493https://avg.163.com/topic/detail/8053524https://avg.163.com/topic/detail/8053385https://avg.163.com/topic/detail/8053442https://avg.163.com/topic/detail/8053467https://avg.163.com/topic/detail/8053490https://avg.163.com/topic/detail/8053517https://avg.163.com/topic/detail/8053212https://avg.163.com/topic/detail/8053236https://avg.163.com/topic/detail/8053253https://avg.163.com/topic/detail/8053261https://avg.163.com/topic/detail/8053214https://avg.163.com/topic/detail/8053288https://avg.163.com/topic/detail/8053235https://avg.163.com/topic/detail/8053254https://avg.163.com/topic/detail/8053263https://avg.163.com/topic/detail/8053289https://avg.163.com/topic/detail/8053217https://avg.163.com/topic/detail/8053234https://avg.163.com/topic/detail/8053251https://avg.163.com/topic/detail/8053265https://avg.163.com/topic/detail/8053285https://avg.163.com/topic/detail/8052452https://avg.163.com/topic/detail/8052460https://avg.163.com/topic/detail/8053215https://avg.163.com/topic/detail/8053233https://avg.163.com/topic/detail/8053250https://avg.163.com/topic/detail/8052453https://avg.163.com/topic/detail/8052459https://avg.163.com/topic/detail/8053211https://avg.163.com/topic/detail/8053231https://avg.163.com/topic/detail/8053245https://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类成员访问运算符 - 重载