输入输出数组元素的函数重载_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:
輸出:
65Explanation:
說明:
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:
輸出:
100Explanation:
說明:
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机网络怎么寻址_计算机网络中的无类寻
- 下一篇: 面试官:AtomicInteger是如何