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

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

Program 1:

程序1:

#include <iostream>
using namespace std;

void fun()
{
    cout << "1. fun() called" << endl;
}

void fun(int A, float B)
{
    cout << "2. fun() called " << A << " " << B << endl;
}

void fun(int A, double B)
{
    cout << "3. fun() called " << A << " " << B << endl;
}

int main()
{
    fun();
    fun(10, 3.14);

    return 0;
}

Output:

输出:

1. fun() called
3. fun() called 10 3.14

Explanation:

说明:

In the main() function, we called fun() without any argument then it will print "1. fun() called" on the console screen. 

main()函数中,我们不带任何参数的情况下调用fun() ,然后它将在控制台屏幕上打印“ 1. fun()被调用”。

And then we call fun(10,3.14), then it will call function void fun(int A,double B). Because "3.14" is a double value. That's why it will call the function with the argument double. If we want to call the function with argument float then we need to pass "3.14F" instead of "3.14".

然后我们调用fun(10,3.14) ,然后它将调用函数void fun(int A,double B) 。 因为“ 3.14”是双精度值。 这就是为什么它将使用参数double调用函数的原因。 如果要使用float参数调用该函数,则需要传递“ 3.14F”而不是“ 3.14”。

Program 2:

程式2:

#include <iostream>
using namespace std;

void fun()
{
    cout << "1. fun() called" << endl;
}

void fun(int A, float B)
{
    cout << "2. fun() called " << A << " " << B << endl;
}

int fun(int A, float B)
{
    cout << "3. fun() called " << A << " " << B << endl;
}

int main()
{
    fun();
    fun(10, 3.14);

    return 0;
}

Output:

输出:

main.cpp: In function ‘int fun(int, float)’:
main.cpp:14:5: error: ambiguating new declaration of ‘int fun(int, float)’
 int fun(int A, float B)
     ^~~
main.cpp:9:6: note: old declaration ‘void fun(int, float)’
 void fun(int A, float B)
      ^~~

Explanation:

说明:

This program will generate an error because a function cannot be overloaded on the basis of the return type.

该程序将产生错误,因为不能根据返回类型重载函数。

void fun(int A,float B);
int fun(int A,float B);

In the above declarations, the only return type is different. That's why it will generate an error.

在上面的声明中,唯一的返回类型是不同的。 这就是为什么它将生成错误的原因。

Program 3:

程式3:

#include <iostream>
using namespace std;

void fun()
{
    cout << "1. fun() called" << endl;
}

void fun(int A, float B)
{
    cout << "2. fun() called " << A << " " << B << endl;
}

int fun(int A, double B)
{
    cout << "3. fun() called " << A << " " << B << endl;
}

int main()
{
    fun();
    fun(10, 3.14);

    return 0;
}

Output:

输出:

1. fun() called
3. fun() called 10 3.14

Explanation:

说明:

Here, we overloaded function fun(),

在这里,我们重载了函数fun()

void fun()
void fun(int A,float B)
int fun(int A,double B)

Note: If arguments are different in the functions, then the return type of the function may be different for function overloading.

注意:如果函数中的参数不同,则对于函数重载,函数的返回类型可能不同。

Now come to the main() function, we are calling fun() without any argument then it will print "1. fun() called" on the console screen.

现在进入main()函数,我们在不带任何参数的情况下调用fun() ,它将在控制台屏幕上显示“ 1. fun(Called)”。

And then, we are calling fun(10,3.14), then it will call function "void fun(int A,double B)". Because "3.14" is a double value. That's why it will call the function with the argument double. If we want to call the function with argument float then we need to pass "3.14F" instead of "3.14".

然后,我们调用fun(10,3.14) ,然后它将调用函数“ void fun(int A,double B)”。 因为“ 3.14”是双精度值。 这就是为什么它将使用参数double调用函数的原因。 如果要使用float参数调用该函数,则需要传递“ 3.14F”而不是“ 3.14”。

Program 4:

计划4:

#include <iostream>
using namespace std;

void print()
{
    cout << "****" << endl;
}

void print(int NUM, char ch)
{
    for (int i = 0; i < NUM; i++)
        cout << ch;
    cout << endl;
}

void print(char ch, int NUM)
{
    for (int i = 0; i < NUM; i++)
        cout << ch;
    cout << endl;
}

int main()
{
    print();
    print(4, '#');
    print('@', 4);

    return 0;
}

Output:

输出:

****
####
@@@@

Explanation:

说明:

Here, we overloaded print() function, declarations for overload print() functions are given below,

在这里,我们重载print()函数,用于过载印刷声明()函数在下面给出,

void print()
void print(int NUM,char ch)
void print(char ch,int NUM)

Here, each overloaded function called one by one in the main() function and print below texts,

在这里,每个重载函数在main()函数中一个接一个地调用,并在文本下方显示,

****
####
@@@@


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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值