输入输出数组元素的函数重载_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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值