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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

输入输出数组元素的函数重载_C ++函数重载| 查找输出程序| 套装3

發布時間:2025/3/11 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 输入输出数组元素的函数重载_C ++函数重载| 查找输出程序| 套装3 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

輸入輸出數組元素的函數重載

Program 1:

程序1:

#include <iostream> using namespace std;class Test { public:void fun(){cout << "Fun() called" << endl;}void fun(int num){cout << num << endl;} };int main() {Test T;T.fun('A');return 0; }

Output:

輸出:

65

Explanation:

說明:

Here, we created a class Test that contains two member functions fun() that are overloaded.

在這里,我們創建了一個Test類,其中包含兩個已重載的成員函數fun() 。

Now, look to the main() function. Here we created an object T. And, called function fun(), but there is no exact match of function fun() is available in the class. But integer and character have the same internal structure that's why it called the function fun() with character argument and print ASCII value of 'A' that is 65.

現在,查看main()函數。 在這里,我們創建了一個對象T。 并且,稱為函數fun() ,但是在類中沒有函數fun()的完全匹配。 但是,整數和字符具有相同的內部結構,這就是為什么它使用字符參數調用函數fun()并打印ASCII值 “ A”為65的原因。

Program 2:

程式2:

#include <iostream> using namespace std;struct Test {void fun(){cout << "Fun() called" << endl;}void fun(int num){cout << num << endl;} };int main() {Test T;T.fun(100);return 0; }

Output:

輸出:

100

Explanation:

說明:

Here, we created a structure Test that contains two member functions fun() that are overloaded. By default the members of a structure are public.

在這里,我們創建了一個結構Test ,其中包含兩個重載的成員函數fun() 。 默認情況下,結構的成員是公共的 。

Now, look to the main() function. Here we created a structure variable of T.? Then we called function fun() with an integer argument, then "100" will be printed on the console screen.

現在,查看main()函數。 在這里,我們創建了一個結構變量T。 然后我們使用整數參數調用fun()函數,然后控制臺屏幕上將顯示“ 100”。

Program 3:

程式3:

#include <iostream> using namespace std;struct Test {void fun(){cout << "Fun() called" << endl;}void fun(int num){cout << num << endl;} } * T;int main() {T.fun(100);return 0; }

Output:

輸出:

main.cpp: In function ‘int main()’: main.cpp:17:7: error: request for member ‘fun’ in ‘T’, which is of pointer type ‘Test*’ (maybe you meant to use ‘->’ ?)T.fun(100);^~~

Explanation:

說明:

This code will generate an error because, here, we created the pointer to a structure, but we called a member of a structure using "." Operator, as we know that, first we need to assign the address of structure variable and then we need to use referential operator "->" to access the members of a structure using the pointer.

該代碼將產生錯誤,因為在這里,我們創建了指向結構的指針,但是我們使用“”來調用結構的成員。 眾所周知,運算符首先需要分配結構變量的地址,然后需要使用引用運算符“->”來使用指針訪問結構的成員。

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

輸入輸出數組元素的函數重載

總結

以上是生活随笔為你收集整理的输入输出数组元素的函数重载_C ++函数重载| 查找输出程序| 套装3的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。