Basic syntax you must know when you first know C + +

Posted by raghavan20 on Wed, 19 Jan 2022 09:44:23 +0100

catalogue

Namespace

Definition of namespace

C + + input & output

Default parameters

Function overloading (important syntax knowledge in C + +)

Namespace

stay C/C++ There are a large number of variables, functions and classes to be learned later. The names of these variables, functions and classes will all exist in the global scope, which may lead to many conflicts. The purpose of using namespaces is to localize the names of identifiers , to Avoid naming conflicts or names contaminated , namespace The emergence of keywords is aimed at this problem   

Definition of namespace

//Normal namespace
namespace A  //A is the name of the namespace
{
	//The contents of the namespace can define both variables and functions
	int a = 10;
    int Add(int left, int right) {
	return left + right;
	}

}

//Namespaces can be nested definitions
namespace B
{
	int a=10;
	int b=20;
	namespace C
	{
		int c=30;
	}
}

//Multiple identical namespaces are allowed in the same project, but the compiler will eventually merge them into one
namespace A 
{
	void swap(int left, right) {
		int tem = left;
		left = right;
		right = tmp;
	}
}
be careful: A namespace defines a new scope , everything in a namespace is limited to that namespace

So the question is, how do we use namespaces

If we want to access namespaces, there are generally three ways

1. Add namespace name and scope qualifier

int main()
{
	printf("%d ", A::a);
	return 0;
}

2. Use using to introduce members in the namespace

using A::a;
int main()
{
	printf("%d ", a);
	return 0;
}

3. Use the using namespace namespace name to import

using namespace A;
int main()
{
	printf("%d ", a);
	return 0;
}

C + + input & output

#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n; //cin is called the stream extraction operator, which is similar to scanf in C
	cout << "Hello C++" << endl; //cout is called the stream insertion operator, which is similar to printf in C
    //Cout < < endl means line feed (\ n)
	return 0;
}

Note: when using cout standard output (console) and cin standard input (keyboard), you must include < iostream > header file to std standard namespace.

cout can automatically identify built-in types without adding data format control

#include<iostream>
using namespace std;
int main()
{
	int a = 10;
	double b = 10.23;
	char c = 'a';
	cout << a << endl;
	cout << b << endl;
	cout << c << endl;
	return 0;
}

 

Default parameters

The default parameter is to specify a default value for the parameter of the function when the function is declared or defined. When calling this function, if no argument is specified, the default value is adopted; otherwise, the specified argument is used

void TestFunc(int a = 0)
{
	cout << a << endl;
}

int main()
{
	TestFunc(); //a=0;
	TestFunc(10);//a=10;
	return 0;

}

All default parameters

void TestFunc(int a = 10, int b = 20, int c = 30) {
 cout<<"a = "<<a<<endl;
 cout<<"b = "<<b<<endl;
 cout<<"c = "<<c<<endl; 
}

Semi default parameter (semi default is not the default half)

void TestFunc(int a, int b = 10, int c = 20) {
 cout<<"a = "<<a<<endl;
 cout<<"b = "<<b<<endl;
 cout<<"c = "<<c<<endl; }

be careful:

1. Semi default parameters must be given from right to left in turn, and cannot be given at intervals

void TestFunc(int a=30, int b , int c = 20) //That won't work
{
 cout<<"a = "<<a<<endl;
 cout<<"b = "<<b<<endl;
 cout<<"c = "<<c<<endl; 
}

2. Default parameters cannot appear in function declaration and definition at the same time

//a.h
void TestFunc(int a = 10);
// a.c
void TestFunc(int a = 20)
{}
// Note: if the life and the defined location appear at the same time, and the values provided by the two locations happen to be different, the compiler cannot determine which to use
 There are two default values.

3. The default value must be a constant or a global variable

4.C language is not supported

Function overloading (important syntax knowledge in C + +)

In natural language, a word can have multiple meanings. People can judge the true meaning of the word through the context, that is, the word is overloaded
For example, there used to be a joke that there were two state-owned sports that we didn't have to watch or worry about at all. One is table tennis and the other is men's football. The former is“ No one can win! " , the latter is " No one can win! "
Concept of function overloading
function overloading : Is a special case of a function, C++ Allow in In the same scope Declare several functions similar Function with the same name , of these functions with the same name
parameter list ( Number or type or order of parameters ) Must be different It is often used to deal with problems with similar functions and different data types
int Add(int left, int right)
{
	return left + right;
}

double Add(double left, double right)//Different parameter types
{
	return left + right;
}

int Add(int left)//Different number of parameters
{
	return left;
}

 

Topics: C C++ Back-end Visual Studio Code