In the past few years, the code of the project has been written in C language, and it can be said that the others are basically forgotten. In the future, some Linux projects may need to be written in C++, so I decided to review and summarize the syntax of C++ and the use of some libraries, mainly some summary content for future use. I just wrote it down. This series of articles is updated from time to time. For simple knowledge, multiple parts will be summarized in one article. This article will introduce the concepts of namespaces, references, function overloading, and cout formatted output.
Article directory
1 namespace
Namespaces refer to various blocks that can be created in a program to group all similar objects and can refer to all variables, functions or classes within the block. In this way, we can create separate scopes and reuse the same variable names that exist in different namespaces in the program. The use of namespaces allows us to work with files in multiple libraries. The characteristics of the namespace are as follows:
- Namespaces can only be declared globally
- Namespaces can nest other namespaces
1. Declaration of namespace
namespace namespacename
{
//code declarations where m and n are declared in namespace_name's scope
int m, n;
}
We can use operators ::
to access variables or functions in a namespace:
namespace_name :: variablename
namespace_name :: functionname
If a namespace is unnamed, variables and functions specified within it can be explicitly accessed in the same way as global variables.
2. Nesting of namespaces
Example:
namespace example1 {
namespace example2 {
namespace example3 {
int sample;
}
}
}
access sample
variable:
example1::example2::example3::sample.
3. using namespce
By using the using-directive and the syntax shown below, you can insert the entire namespace into a piece of code:
using namespace namespacename;
The most common example of this usage is: using namespace std
, which is used to access the namespace named standard, which includes the C++ I/O objects cout and cin.
4. Example
Here is an example:
#include <iostream>
using namespace std;
//Creating nested namespaces
namespace no1 {
// Declaring variable within the namespace no1
int sample = 10;
namespace no2 {
namespace no3 {
// Declaring variable within the namespace no3
// that initializes to the variable sample in namespace no1
int sample1 = sample;
}
}
}
//Creating namespace that allows us to use it explicitly to access a variable in the nested namespace
namespace myalias = no1::no2::no3;
//Creating namespace demo and declaring an initialized variable
namespace demo {
int gvar = 200;
}
//Inserting the entire namespace demo into our code
using namespace demo;
int main() {
//Accessing the variable without the scope resolution operator as it is a global namespace
cout << "Value of global variable in demo namespace = " << gvar << "\n";
//Accessing the value of sample1 using the namespace myalias
cout << "Value of sample1 in nested namespace third = " << myalias::sample1 << "\n";
return 0;
}
5. Summary
By using namespaces, we can prevent conflicts in files with the same variable names and function names in the same program. Using namespaces to define the same code in different files and libraries can improve code readability.
2 quotes
When a variable is declared as a reference, it can be used as an alias for an existing variable. This is very similar to pointers in C language, but for languages such as PHP and Java, there are no pointers, but the concept of references. This is because they do not use the registers of the operating system chip, and most of them are used in variable memory defined by themselves. But many careless programmers will access null pointers, and for references, it avoids this.
In C language, & can be used to obtain the address of a variable, and in C++, & can also be used to declare a reference. Let's see an example
int a = 100;
int &b = a;
cout << "a = " << a << ",b = " << b <<endl;
cout << "&a = " << &a << ",&b = " << &b <<endl;
--------------
output:
a = 100,b = 100
&a = 0x61fe14,&b = 0x61fe14
That is to say, the addresses of a
and b
are the same, and we can change the value in the same memory through a
or .b
- Unlike pointers, references must be assigned a value when initialized, and cannot be changed after initialization, so the above reference can be understood as a const declaration of a pointer:
int * const p = &a
. - When the reference type is declared
const
as , the compiler will generate a temporary variable when the type of the reference parameter declared by the actual participating function is inconsistent or the parameter is not an lvalue. For exampleconst double &a
, if the parameter is 1 or a floating-point number 1.0 is passed inint
, the compiler will create a temporary anonymous variable and let ita
point to it. This anonymous variable only exists during the function call. - Reference as function return value: If the return value of the function is not declared as a reference, the return value will be stored in a temporary location first, and then copied to the variable that holds the return value of the function. And if the function return value is a reference, it will be copied directly from the original variable.
3 function overloading
C++ has a new feature: polymorphism. It means that different forms appear under certain conditions. For functions, functions are allowed to have multiple forms. Similarly, function overloading in C++ means that you can have multiple functions with the same name but different parameters. The compiler resolves related functions by comparing parameters.
- Function overloading has nothing to do with the return type of the function, if and only when two functions have the same return type, different parameter types or number of parameters, they can be defined as overloading
example:
int demo(int a, int b);
int demo(int c, int d, int e);
int demo(float c, float d);
4 cout formatted output
When it comes to formatted output in C++ cout
, the header files provide some additional functions and manipulators for further control over the precision and format of the output. The following are some common functions and operators:
1. Set the precision of floating-point numbers to n digits
std::setprecision(n)
: std::cout << std::setprecision(4) << myFloat
output floating-point numbers as four-digit precision.
2. Set the output format of floating-point numbers to fixed-point notation (the number of decimal places is fixed)
std::fixed
: std::cout << std::fixed << myFloat
output floating-point numbers in fixed-point notation.
3. Set the output format of floating point numbers to scientific notation
std::scientific
: std::cout << std::scientific << myFloat
output floating point numbers in scientific notation.
4. Set the field width to n characters
std::setw(n)
: std::cout << std::setw(8) << myString
set the field width to 8 characters.
5. Set the padding character to c
std::setfill(c)
: std::cout << std::setfill('*') << std::setw(10) << myString
set the field width to 10 characters, and fill the blank part with asterisks.
6. Make the output left-aligned
std::left
: std::cout << std::left << std::setw(10) << myString
left-aligned fields are output.
std::right
Aligned to the right (default)