Advanced use of functions in C++ core programming

Table of contents

1. The default parameters of the function

2. Function placeholder parameters

3. Function overloading

4. Function overloading - matters needing attention


1. The default parameters of the function

In C++, the formal parameters in the formal parameter list of the function can have default values

Syntax: return value type function name (parameter = default value) {}

Example 1:

#include<iostream>

using namespace std;

// 函数的默认参数

int func(int a,int b,int c)

{

    return a+b+c;

}

int main()

{

    func(10,20);

    return 0;

}

The error shows:

Fewer parameters are passed in.

Example 2:

#include<iostream>

using namespace std;

// 函数的默认参数



int func(int a,int b=20,int c=30)

{

    return a+b+c;

}

int main()

{

    cout<<func(10)<<endl;;

    return 0;

}

operation result:

Example 3:

#include<iostream>

using namespace std;

// 函数的默认参数



// 如果我们自己传入数据,就用自己的数据,如果没有,那么就用默认值

int func(int a,int b=20,int c=30)

{

    return a+b+c;

}

int main()

{

    cout<<func(10,40)<<endl;;

    return 0;

}

operation result:

Precautions:

// 1. If a position parameter has a default value, then this position must have a default value from left to right

// 2. If the function declaration has a default value, the function cannot have a default parameter when the function is implemented (only one of the declaration and the implementation has a default parameter)

Example:

#include<iostream>

using namespace std;

// 函数的默认参数



// 如果我们自己传入数据,就用自己的数据,如果没有,那么就用默认值

int func(int a,int b=20,int c=30)

{

    return a+b+c;

}

int func2(int a=10,int b=10);

int func2(int a,int b)

{

    return a+b;

}

int main()

{

    cout<<func(10,40)<<endl;

    cout<<"func2="<<func2()<<endl;

    return 0;

}

operation result:

2. Function placeholder parameters

The formal parameter list of a function in C++ can have a placeholder parameter, which is used as a placeholder, and the place must be filled when the function is called

Syntax: return value type function name (data type) {}

void func (int a, int) // The second parameter is for placeholder

func(10,10);// Must pass parameters to call this function

Example:

#include<iostream>

using namespace std;

// 占位参数

void func(int a,int)

{

    cout<<"this is a func"<<endl;

}

// 占位参数也可以有默认值

void func2(int a,int =20)

{

    cout<<"this is a func2"<<endl;

}

int main()

{

    func(10,40);// 占位参数必须填补

    func2(10);

    return 0;

}

operation result:

3. Function overloading

Function: the function name can be the same, improving reusability

Conditions to be met for function overloading:

  • under the same scope
  • same function name
  • Function parameter types are different, or the number is different, or the order is different

Note: The return value of a function cannot be used as a condition for function overloading, nor can it have default parameters

Example:

#include<iostream>

using namespace std;

// 函数重载

void func()

{

    cout<<"func函数的调用!"<<endl;

}

void func(int a,int b=10)

{

    cout<<"有参数的func函数调用!"<<endl;

}

void func(double a)

{

    cout<<"有参数且参数类型与上一个不同的func函数调用"<<endl;

}

void func(int a,double b)

{

    cout<<"func(int a,double b)的调用"<<endl;

}

void func(double a,int b)

{

    cout<<"func(double a,int b)的调用"<<endl;

}

// 注意事项

// 函数的返回值不可以作为函数重载的条件

/*int func(double a,int b)

{

    cout<<"func(double a,int b)的调用"<<endl;

}*/

int main ()

{

    func();

    func(10);

    func(3.14);

    func(10,3.14);

    func(3.14,10);

    return 0;

}

operation result:

 

4. Function overloading - matters needing attention

  • references as overload conditions
  • Function overloading encounters function default parameters
#include<iostream>

using namespace std;

// 函数重载的注意事项

//1、引用作为重载的条件

void func(int &a)

{

    cout<<"func(int &a)函数的调用!"<<endl;

}

// 两个函数的区别是参数类型不同

void func(const int &a)

{

    cout<<"func(const int &a)函数调用!"<<endl;

}



// 函数重载碰到默认参数的情况

void func2(int a)

{

    cout<<"func2(int a)的调用"<<endl;

}

void func2(int a,int b=10)

{

    cout<<"func2(int a,int b=10)的调用"<<endl;

}

int main ()

{

    int a=10;

    const int b=10;

    func(a); // 传入一个变量的时候默认是没有const执行

    func(b);

    func(10);// 传入一个常量或者const修饰的变量执行的是有const修饰的函数



    cout<<endl;

    //func2(10);// 有默认值的时候两个函数都可以使用,有歧义,避免这样设置

    func2(10);// b没有默认值的时候可以使用

    func2(10,20);// 这样就很明确,即使有默认参数也不影响函数的调用

    return 0;

}

operation result:

Guess you like

Origin blog.csdn.net/hjl011006/article/details/131771412