Necessary basic knowledge from c to c + +

Posted by biodrux on Tue, 08 Feb 2022 20:38:26 +0100

First of all, we must know that c + + is compatible with c syntax, which syntax is suitable for use, and which one
Before we start learning a language, type a line of hello world in c + + syntax

#include<ostream>
using namespace std;
int main()
{
	cout << "hello word" << endl;
	return 0;
}

Here we can see that c + + needs a line of using namespace std. what does this mean? Next, let's introduce the namespace.

Namespace

In 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 exist in the global scope, which may lead to many conflicts. The purpose of using namespace is to localize the name of identifier to avoid naming conflict or name pollution. The emergence of namespace keyword is aimed at this problem.
To define a namespace, you need to use the namespace keyword, followed by the name of the namespace, followed by a pair of {}, which are the members of the namespace.

int main()
{
	int scanf = 10;
	int strlen = 20;
	printf("%d\n", scanf);
	printf("%d\n", strlen);
	return 0;
}

This code cannot be passed in c language. In order to solve the problem of naming conflict, c + + has a namespace to isolate names.

namespace bit
{
    int scanf=10;
    int strlen=20;
}

In order to prevent naming conflicts, the c + + library defines the contents of its own library in an std namespace. There are three ways to use the contents of the standard library:
1. Specify namespace

int main()
{
	std::cout << "hello world" << std::endl;
	
}

This method is very troublesome. It should be specified everywhere, but it is the most standardized way.
2. Expand the whole std
It's the line above, using namespace std;
It seems convenient. If our own definition conflicts with the library, we can't solve it.
3. Expand the contents of some common libraries
using std::ount;
using std::endl;
The compromise scheme of 1.2 is also commonly used in the project

c + + input and output

Global object cout of type ostream
Global object cin of type istream

The difference between cout and cin compared with printf and scanf in c language is that they can automatically identify types

Default parameters and function overloading

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

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

Semi default parameter
The semi default is from right to left

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

Function of default parameters: it is more convenient to change the value of parameters.

void StackInit(struct Stack* ps,int InitCapacity=4)
{
	ps->a = (int *)malloc(sizeof(int)* 4);
	ps->size = 0;
	ps->capacity = 4;
}
int main()
{ 
	struct Stack st1;
	//Suppose I know that at least 100 data must be stored in the stack
	StackInit(&st1,100);
	struct Stack st2;
	//Suppose I know that there are at most 10 data stored in the stack
	StackInit(&st2,10);
	return 0;
}

function overloading

Function overloading: it is a special case of functions. C + + allows to declare several functions with the same name with similar functions in the same scope. The formal parameter list (number or type or order of parameters) of these functions with the same name must be different. It is commonly used to deal with the problems of similar functions and different data types.
c language does not allow the definition of functions with the same name.
c + + can define parameters with different types or numbers.

int Add(int left, int right)
{
 return left+right;
}
double Add(double left, double right)
{
 return left+right;
}
long Add(long left, long right)
{
 return left+right;
}
int main()
{
 Add(10, 20);
 Add(10.0, 20.0);
 Add(10L, 20L);
 
 return 0;

quote

Instead of defining a new variable, a reference gives an alias to an existing variable. The compiler will not open up memory space for the referenced variable. It shares the same memory space with the variable it references.

void TestRef()
{
 int a = 10;
 int& ra = a;//< = = = = define reference type
 
 printf("%p\n", &a);
 printf("%p\n", &ra);
}

Referenced properties:

  1. References must be initialized when defined
  2. A variable can have multiple references
  3. Reference once an entity is referenced, other entities cannot be referenced
int main()
{
	int a = 0;
	int &b = a;
	int &c = a;
	int &d = b;
	int &r = a;
	int x = 10;
	//References must be initialized
	r = x;
	//Instead of making r an alias for X, assign x to R
	return 0;
}
int main()
{
	const int a = 10;
	//int &ra=a; RA's reference to a belongs to permission amplification, so it can't
	const int &ra = a;
	int b = 10;
	int &rb = b;
	const int& crb = b;//crb reference b belongs to the reduction of permission, so it can
	return 0;
}

If the reference is used as the return value (under special scenarios)
Value transfer return

int Add(int a, int b)
{
	int c = a + b;
	return c;
}
int main()
{
	const int& ret = Add(1, 2);
	return 0;
}

c first passes to a temporary variable, receives the temporary variable with Const reference, and the return object is a copy of c.
Pass reference and return. The returned reference is the reference of c.

Topics: C C++