日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

c++重载++运算符_C ++运算符重载| 查找输出程序| 套装3

發(fā)布時(shí)間:2025/3/11 c/c++ 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++重载++运算符_C ++运算符重载| 查找输出程序| 套装3 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

c++重載++運(yùn)算符

Program 1:

程序1:

#include <iostream> using namespace std;class Test { public:int A;Test(){A = 0;}Test(int a){A = a;}void print(){cout << A << " ";} };Test operator*(Test T1, Test T2) {Test temp;temp.A = T1.A * T2.A;return temp; }int main() {Test T1(10);Test T2(5);Test T3;T3 = operator*(T1, T2);T3.print();return 0; }

Output:

輸出:

50

Explanation:

說(shuō)明:

Here, we created a class Test with public data member A, and we defined constructors and print() member function, overloaded ‘*’ operator using non-member function. So, we passed two objects argument and returned a temporary object that contains the result of the multiplication.

在這里,我們使用公共數(shù)據(jù)成員 A創(chuàng)建了一個(gè)Test類,并定義了構(gòu)造函數(shù)和print()成員函數(shù),并使用非成員函數(shù)重載了'*'運(yùn)算符。 因此,我們傳遞了兩個(gè)對(duì)象參數(shù),并返回了一個(gè)包含乘法結(jié)果的臨時(shí)對(duì)象。

In the main() function, we created three objects T1, T2, and T3. And then multiplication of T1 and T2 assigned to the object T3 using the overloaded function.

在main()函數(shù)中,我們創(chuàng)建了三個(gè)對(duì)象T1 , T2和T3 。 然后使用重載函數(shù)將分配給對(duì)象T3的T1和T2相乘。

Program 2:

程式2:

#include <iostream> using namespace std;class Test {int A;public:Test(){A = 0;}Test(int a){A = a;}int operator<(Test T){int ret = 0;if (A < T.A)return 1;elsereturn 0;} };int main() {Test T1(10);Test T2(5);if (T1 < T2)cout << "T1 less than T2";elsecout << "T2 less than T1";return 0; }

Output:

輸出:

T2 less than T1

Explanation:

說(shuō)明:

Here, we created the class Test with data member A and defined two constructors. We also defined function to overload less than '<' operator to compare two objects. The overloaded function returned an integer value for comparison.

在這里,我們使用數(shù)據(jù)成員A創(chuàng)建了Test類,并定義了兩個(gè)構(gòu)造函數(shù)。 我們還將函數(shù)定義為重載小于'<'運(yùn)算符以比較兩個(gè)對(duì)象。 重載的函數(shù)返回一個(gè)整數(shù)值以進(jìn)行比較。

In the main() function. Here we created T1 and T2 objects that initialized with 10 and 5 respectively. And we compared both objects then "T2 less than T1" message will be printed on the console screen.

在main()函數(shù)中。 在這里,我們創(chuàng)建了分別用10和5初始化的T1和T2對(duì)象。 然后我們比較了兩個(gè)對(duì)象,然后控制臺(tái)屏幕上將顯示“ T2小于T1”消息。

Program 3:

程式3:

#include <iostream> using namespace std;class Test {int A;public:Test(){A = 0;}Test(int a){A = a;}int operator ::(){return A;} };int main() {Test T1(10);cout << ::T1;return 0; }

Output:

輸出:

main.cpp:17:18: error: expected type-specifier before ‘::’ tokenint operator ::()^~ main.cpp: In function ‘int main()’: main.cpp:27:13: error: ‘::T1’ has not been declaredcout << ::T1;^~

Explanation:

說(shuō)明:

It will generate syntax error because we cannot overload "::" operator in C++.

因?yàn)槲覀儾荒茉贑 ++中重載“ ::”運(yùn)算符,所以它將產(chǎn)生語(yǔ)法錯(cuò)誤。

翻譯自: https://www.includehelp.com/cpp-tutorial/operator-overloading-find-output-programs-set-3.aspx

c++重載++運(yùn)算符

總結(jié)

以上是生活随笔為你收集整理的c++重载++运算符_C ++运算符重载| 查找输出程序| 套装3的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。