catalogue
Function overloading (important syntax knowledge in C + +)
Namespace
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; } }
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 + +)
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; }