[C + +] Introduction to C + + Part 1

introduction

C + + is the inheritance of C language. It can not only carry out procedural programming of C language, object-based programming characterized by abstract data types, but also object-oriented programming characterized by inheritance and polymorphism. While C + + is good at object-oriented programming, it can also carry out process based programming. Therefore, C + + can adapt to the problem scale.

C + + keywords

C + + has 63 keywords in total, while C language has 32 keywords.
The following are the keywords of C + +. Subsequent blogs will introduce the commonly used ones. Now you only need to know what keywords C + + has.

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.

Definition of namespace

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.

//1. Common namespace
namespace N1 // N1 is the name of the namespace
{
	// Namespace, you can define either variables or functions
	int a;
	int Add(int left, int right)
	{
	return left + right;
	}
}
//2. Namespaces can be nested
namespace N2
{
	int a;
	int b;
	int Add(int left, int right)
	{
		return left + right;
	}
	namespace N3
	{
		int c;
		int d;
		int Sub(int left, int right)
		{
			return left - right;
		}
	}
}
//3. Multiple namespaces with the same name are allowed in the same project, and the compiler will finally synthesize them into the same namespace.
namespace N1
{
	int Mul(int left, int right)
	{
		return left * right;
	}
}

Note: a namespace defines a new scope, and all contents in the namespace are limited to the namespace.

Use of namespaces

How should members in a namespace be used? For example:

namespace N
{
	int a = 10;
	int b = 20;
	int Add(int left, int right)
	{
		return left + right;
	}
	int Sub(int left, int right)
	{
		return left - right;
	}
}
int main()
{
	printf("%d\n", a); // The statement compilation error, unrecognized a
	return 0;
}

There are three ways to use namespaces:

1. Add namespace name and scope qualifier:

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

2. Use using to bring in members from a namespace:

using N::b;
int main()
{
	printf("%d\n", N::a);
	printf("%d\n", b);
	return 0;
}

3. Use the using namespace namespace name to import:

using namespce N;
int main()
{
	printf("%d\n", N::a);
	printf("%d\n", b);
	Add(10, 20);
	return 0;
}

C + + input & output

#include<iostream>
using namespace std;

int main()
{
	cout<<"Hello world!!!"<<endl;
	return 0;
}

explain:

  1. When using cout standard output (console) and cin standard input (keyboard), the < iostream > header file and std standard namespace must be included.
  1. It is more convenient to use c + + input and output without adding data format control, such as shaping –% d and character –% c.

Note: in the early standard library, all functions were implemented in the global domain and declared in the header file with. h suffix. When used, it only needs to include the corresponding header file. Later, it is now in the std namespace. In order to distinguish it from the C header file and correctly use the namespace, it is stipulated that the C + + header file does not contain. h; The old compiler (vc 6.0) also supports < iostream. h > format, which is no longer supported by subsequent compilers. Therefore, the method of < iostream. h > + std is recommended.

//Example:
#include <iostream>
using namespace std;

int main()
{
	int a;
	double b;
	char c;
	cin>>a;
	cin>>b>>c;
	cout<<a<<endl;
	cout<<b<<" "<<c<<endl;
	return 0;
}

Default parameters

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

void TestFunc(int a = 0)
{
	cout<<a<<endl;
}
int main()
{
	TestFunc(); // When no parameter is passed, the default value of the parameter is used
	TestFunc(10); // When passing parameters, use the specified arguments
}

2. Default parameter classification:

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

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, and cannot be given at intervals
  2. Default parameters cannot appear in both function declarations and definitions
//Example:
//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 default value to use.
  1. The default value must be a constant or a global variable
  2. C language not supported (compiler not supported)

function overloading

Concept:
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.
Example:

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;
}

Thinking questions

Why does C + + support function overloading and C language does not support function overloading?
First of all, we know that in C/C + +, a program needs to go through the following stages: preprocessing, compilation, assembly and linking.

1, in fact, our project is usually made up of multiple header files and multiple source files, and through the compilation link of our C language stage, we can know that when the Add function defined in b.cpp is called in the current a.cpp, there is no Add function address in the a.o's target file before the compiled link, because Add is defined in b.cpp, so Add's address is in b.o.

2. So the link stage is dedicated to this problem. When the linker sees that a.o calls Add, but there is no Add address, it will find the Add address in the symbol table of b.o and link it together.
3. When linking, which name will the connector use to find the Add function? Here, each compiler has its own function name modification rules.
4. Because the modification rules of vs under Windows are too complex, and the modification rules of gcc under Linux are simple and easy to understand, gcc is used to demonstrate the modified name below.

We can see from the following that the name of gcc function remains unchanged after modification. After modification, the function of g + + becomes [_Z + function length + function name + type initial]

1. Results compiled with C language compiler:

Conclusion: under linux, the modification of function name has not changed after gcc compilation.

2. Results compiled with C + + compiler:

Conclusion: under linux, after g + + compilation, the modification of function name changes, and the compiler adds the function parameter type information to the modified name.

Conclusion: C language can't support overloading because functions with the same name can't be distinguished. C + + is distinguished by function modification rules. As long as the parameters are different, the modified names are different, and overloading is supported.

Tags: C C++

Posted on Sat, 25 Sep 2021 05:24:23 -0400 by batfastad