输入输出数组元素的函数重载

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相关推荐

  1. g++默认参数_C ++默认参数| 查找输出程序| 套装2

    g++默认参数 Program 1: 程序1: #include <iostream> using namespace std; int K = 10; int fun() { retur ...

  2. g++默认参数_C ++默认参数| 查找输出程序| 套装1

    g++默认参数 Program 1: 程序1: #include <iostream> using namespace std; int sum(int X, int Y = 20, in ...

  3. 小程序 || 语句_C ++开关语句| 查找输出程序| 套装1

    小程序 || 语句 Program 1: 程序1: #include <iostream> using namespace std; int main() {switch (printf( ...

  4. 小程序 || 语句_C ++条件语句| 查找输出程序| 套装2

    小程序 || 语句 Program 1: 程序1: #include <iostream> #include <stdio.h> using namespace std; in ...

  5. c语言指针++_C ++此指针| 查找输出程序| 套装3

    c语言指针++ Program 1: 程序1: #include <iostream> using namespace std; class Test { int VAL; public: ...

  6. c ++查找字符串_C ++朋友功能| 查找输出程序| 套装1

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { int A, B; fr ...

  7. c ++查找字符串_C ++朋友功能| 查找输出程序| 套装2

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample1 { int A, B; f ...

  8. c语言指针++_C ++此指针| 查找输出程序| 套装1

    c语言指针++ Program 1: 程序1: #include <iostream> using namespace std; int main() { int A = 10; this ...

  9. 结构化程序goto语句_C ++ goto语句| 查找输出程序| 套装1

    结构化程序goto语句 Program 1: 程序1: #include <iostream> #include <math.h> using namespace std; i ...

  10. 小程序 || 语句_C ++条件语句| 查找输出程序| 套装1

    小程序 || 语句 Program 1: 程序1: #include <iostream> using namespace std; int main() {if (NULL) {cout ...

最新文章

  1. python神秘的魔法函数_Python魔法函数
  2. ParameterizedType应用,java反射,获取参数化类型的class实例
  3. VTK:可视化算法之HeadBone
  4. Rmarkdown与Rnotebook使用心得
  5. python美多商城项目百度网盘_美多商城项目(六)
  6. 1002. 三角形 (
  7. 如何在命令提示符窗口下运行Win32控制台应用程序
  8. 【量化课堂】彼得·林奇的成功投资
  9. labview教学视频:http://www.51zxw.net/study.asp?vip=19649795
  10. python输入esc退出循环_如何用Esc键停止GhPython或者RhinoPython脚本运行
  11. element-ui表格组件分页后完整导出到excel的方法
  12. 宴会泡泡机市场前景分析及研究报告
  13. 十二星座的心是什么做的
  14. 123456123456#5#----com.zzj.DinosourKnown235---前拼show后广--恐龙百科-333333
  15. android 图库管理,Android调用系统图库
  16. envoy网络安全opa等
  17. 华为NQA与默认路由
  18. Window Server 2008 Enterprise SP2激活方法
  19. ubuntu安装显卡驱动记录(未完待续)
  20. Parallel的使用 之Parallel.for

热门文章

  1. python 彻底解读多线程与多进程_python 多进程与多线程浅析
  2. 【SSM面向CRUD编程专栏 2】Spring相关API 数据源(连接池)的配置 注解开发 整合junit
  3. 安装python3.9
  4. 腾讯财报中“最大秘密”:2018云收入91亿元,交首份TO B答卷
  5. rsync服务扩展应用
  6. Gram matrix 格拉姆矩阵
  7. android学习之-Style样式的定义
  8. 数据中心部署气流遏制系统需要考虑的十大要素
  9. eclispe快捷键
  10. ActiveReports 9实战教程(1): 手把手搭建环境Visual Studio 2013 社区版