C + + Learning note 2

  • 2, C + + Basics

6. Inline inline function

If the keyword inline is prefixed to the function definition, the function is declared as an inline function

Whenever a call to the function occurs in a program, the C + + compiler uses the code in the function body instead of the function call expression. Recompile process replacement

Using inline function instead of macro definition can eliminate the insecurity of macro definition. Inline functions have all the advantages and disadvantages of macro definition

Waste space and time

Simple functions can be added with inline

7. Functions with default parameters

When declaring function prototypes, C++ can specify default parameter values for one or more parameters, and call this function later. If omitting one of the parameters, C++
Automatically take the default value as the value of the corresponding parameter

int special(int x=5,float y=5.3);

When making function calls, there are several forms

1)special(100,79.8)//x=100, y=79.8

2)special(25);//x=25, y=5.3

3)special();//x=5,y=5.3

Can't special(, 25); this is wrong

When transferring parameters, you must transfer them from left to right. When setting default values, you must set them from right to left. Parameters with default values are placed on the right. Parameters without default values must be transferred

#include<iostream>
using namespace std;
//Function declaration, the default value is set at the declaration
int my_add(int x=10,int y=20);
int main()
{
    cout<<"my_add()= "<<my_add()<<endl;
    cout<<"my_add(100)= "<<my_add(100)<<endl;
    cout<<"my_add(100,200)= "<<my_add(100,200)<<endl;
    return 0;
} 
//Define function: generally no default value is set
int my_add(int x,int y)
{
    return x+y;
}

8. Function overload

In traditional C language, function names must be unique, and functions with the same name are not allowed in programs; functions with the same name are allowed in C + + within the same scope, which is called function overloading, which is one of the polymorphic features of C + +

Two or more functions can use the same function name as long as the function parameter types are different, the number of parameters is different, or both

The overload of a function has nothing to do with the return value

You can decide which function to use by calling different parameter types

You can also decide which function to use according to the number of parameters passed

When overload meets default parameter: it needs to be avoided manually

If there is a function overload, try to minimize the default parameters. If there are default parameters, write function overload as little as possible

#include<iostream>

using namespace std;

int myAdd(int x, int y = 10);
int myAdd(int x, int y, int z = 100);

int main()
{
	int sum1, sum2;
	//sum1=myAdd(10,20); / / error so that the computer does not know whether to call myAdd of two parameters or myAdd of three parameters
	//Ambiguity of function overload
	// cout<<"sum1= "<<sum1<<endl;
	sum2 = myAdd(100);//Correct, only myAdd of two parameters can be called
	cout << "sum2= " << sum2 << endl;
	return 0;
}

int myAdd(int x, int y)
{
	cout << "in int myAdd(int x,int y) fun" << endl;
	return x + y;
}

int myAdd(int x, int y, int z)
{
	cout << "in int myAdd(int x,int y,int z) fun" << endl;
	return x + y + z;
}

Overloaded function determines which overloaded version to call according to the number of parameters or parameter type class

int mul(int x,int y);

double mul(int x,int y);

In this way, overload is not allowed. It depends on the number and type of parameters

In general, overloaded functions should perform the same function

9. Cast

The form of (int) x is used in. c

Another form is allowed in C + +: int(x)

Both of the above methods are acceptable to C + +, and the latter method is recommended

Complement:

Variable initialization style of int num=10;//c

Int num (100); / / variable initialization style of C + +

#Include < cstdlib > / / starts with c and represents the library file of c language

10. new and delete operators

Equivalent to malloc and free in C language

 

The initial value of the memory space can be assigned at the same time of memory application

#include<iostream>
int main()
{
    int *p1=new int(10);//Initialize the contents of the new requested space to 10
    *p1=100;
    cout<<"*p1= "<< *p1 <<endl;
    delete p1;
    return 0;
}

The array is not allowed to be initialized on new; when releasing the array, add []

int main()
{
    int i;
    int *p=new int[5];
    for(i=0;i<5;i++)
        p[i]=i;
    for(i=0;i<5;i++)
        cout<<p[i]<<", ";
    cout<<endl;
    delete []p;//Note that when releasing an array, add []
    return 0;
}

When using new to dynamically allocate memory, null pointer will be returned if there is not enough memory

11, citation

Reference is to give an alias to a variable, making pointer operation more convenient

Definition: type name & alias = variable name or alias

For example: int a = 5; int & B = a, int & C = B;

Reference must be assigned at the same time of definition. It can be defined first and then assigned

#include<iostream>
using namespace std;
int main()
{
    int num=10;
    int &data = num;//data is a reference and must be assigned when it is defined. data is the alias of num
    cout<<"&num = "<<&num <<endl;
    cout<<"&data= "<<&data<<endl;
    data=100;
    cout<<"num = "<<num <<endl;
    cout<<"data= "<<data<<endl;
    return 0;
}

As the parameter of the function, the reference can directly change the value of the variable in the main function

The effect of reference parameter is the same as that of passing by address. C + + advocates using reference instead of passing address

#include<iostream>
using namespace std;
void setNum(int &x);
int main()
{
    int num=0;
    cout<<"start:num= "<<num<<endl;
    setNum(num);
    cout<<"end:num= "<<num<<endl;
    return 0;
} 
void setNum(int &x)//x is the reference, which is the alias of the variable passed in the calling function and the alias of num in main
{
    x=100;//To change the value of a variable in a calling function by reference
}

Reference as the return value of the function, return the alias of the variable you return

#include<iostream>
using namespace std;
int a[]={1,3,5,7,9};
int &index(int);
int main()
{
    index(2)=25;
    cout<<index(2)<<endl;//Output 25
    cout<<a[2]<<endl;
    return 0;
} 
int &index(int i)
{
    return a[i];
}
#include<iostream>
using namespace std;
int num = 100;
int &index(void);
int main()
{
    cout<<num<<endl;
    index() = 200;
    cout<<num<<endl;//Output 200
    cout<<index()<<endl;
    return 0;
} 
int &index(void)
{
    return num;
}

 

Published 8 original articles, won praise 2, visited 202
Private letter follow

Tags: C

Posted on Mon, 03 Feb 2020 08:39:32 -0500 by Rodney H.