C++ Getting Started, Beginning

Posted by Rommeo on Sun, 16 Jan 2022 04:23:28 +0100

Preface

Tip: Here you can add a general description of what this article will record:
For example: With the continuous development of artificial intelligence, machine learning is becoming more and more important. Many people have started learning machine learning. This paper introduces the basic content of machine learning.

Tip: The following is the main body of this article. The following cases can be used as reference.

1. C++ Keyword

Up to now, there are several major versions of C++98, C++11 and C++17. There are 63 keywords in C++98, of which 32 are inherited from C.

In the following C++11 and C++17, a few more keywords were added, which I learned to add.

2. Namespaces

1.std space

namespace std library standard namespace, including all representations in the C++ standard library

#include<iostream>
using namespace std;
int main()
{
	cout << "hello world" << endl;
}
//For example, the cout and endl used in this library belong to the std library. If you do not declare using the std library, you need to use the std Library
//Scope qualifier (:) declaration introduced.
#include<iostream>
int main()
{
	std::cout << "hello world" << std::endl;
}

2. Other self-defined spaces

You can also define your own namespace to accomplish the functions you need

#include<iostream>
namespace myspace
{
	int a = 100;
	int b = 300;
	int Add(int a, int b)
	{
		return a + b;
	}
	void print()
	{
		std::cout <<"Add(a, b) "<< Add(a, b) << std::endl;
	}
}
using namespace myspace;
int main()
{
	myspace::print(); //Use field qualifier to indicate that the print function in myspace space was called
	print(); //Introduced directly with the using namespace keyword
	return 0;
}

A new scope is equivalent to a new namespace in that it separates functions and variables you define from others.

3. C++ Input and Output

C has its own input and output, and C++ has its own input and output.

#include<stdio.h>
int main()
{
	printf("hello world\n");
	return 0;
}
#include<stdio.h>
int main()
{
	char s[13];
	scanf("%s", s);
	printf("%s\n",s);
	return 0;
}

C has a high format output requirement for output input, that is, you must declare the output format when you output, otherwise he will make an error or think you need to input a string on your screen.
C++ overrides input and output in the case of inheriting input and output from C. There is no requirement for output input format. When entering custom types, you need to overload input and output operators.

#include<iostream>
using namespace std;
int main()
{
	int a;
	float b;
	cin >> a >> b;
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
}

4. Default parameters in C++.

Significance of 1.C++ default parameters

The default parameter is also known as the spare parameter, which means that when you don't give him parameters, you set him an arbitrary parameter so that he doesn't have to output out-of-order or out-of-line values.

#include<iostream>
using namespace std;
void test(int a = 0)
{
	cout << "a = " << a << endl;
}
int main()
{
	test();//Without passing, he will use the default parameters
	test(10);//When passing a parameter, use the specified argument
	return 0;
}

Classification of 2.C++ default parameters

All default parameters are given from left to right

#include<iostream>
using namespace std;
void test(int a = 0,int b = 0,int c =0)
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;
}
int main()
{
	test();
	test(10,20,30);
	return 0;
}


By semi-default, only a few parameters are given in turn from right to left. When you pass in a parameter, it is important to note that the parameters for which you do not have a default parameter are given constants.

#include<iostream>
using namespace std;
void test(int a ,int b = 0,int c =0)
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;
}
int main()
{
	test(1);//You have given default parameters to both b and c, but a has no default parameters and you need to pass in the value of a
	test(10,20,30);//If you pass in three values, the default parameters you give are not used.
	return 0;
}

Default parameters cannot occur in both definitions and declarations, and if they occur at the same time, the compiler cannot determine which parameter is being used and an error will occur. The default parameter must be a constant or a global variable.

5. Function overloading in C++.

1. Definition of function overload

Function overload: Declare several functions with the same name under the same scope, and the list of parameters (parameter type, number of parameters, parameter order) of these functions with the same name must be different to handle problems with similar functions but different data types. (You can do this with a template class)


Call the same function to sum different data types.

2. Implementation of Function Overload

6. References in C++.

1. Implementation of References

A reference is not a new definition of a variable, but an alias for an existing variable. The compiler does not open up a new space for the pointer. It uses the same memory space as his reference variable.
Usage characteristics of references:
1. References must be initialized when they are defined.
2. A variable can have multiple references, but a reference can only refer to one entity and is his entire life cycle.

#include<iostream>
using namespace std;
int main()
{
	int a = 10;
	int& b = a; //The lifetime of b is the entire function and can only be a reference to entity a
	cout << b << endl;
}

References can be used as return values, or as parameters, because when you pass a reference, the constructor is not called to produce new variables, and when you pass a value, you need to open up space to construct variables, which greatly reduces function execution time, saves space to some extent, and meets the requirements of C++ language efficiency.

2. Comparison of reference and pointer

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

In C++, the bottom level of references is achieved by pointers. References are aliased entities. This is achieved by opening up the bottom space to store the address of the variable being pointed to when defining references.
Comparison of references and pointers:
1. References need to be initialized when they are defined. The pointer does not require initialization, that is, you can either point to null or not, or to an object.
2. After the reference is initialized, he can no longer reference other objects. The pointer can point to objects of the same type.
3. There is a null pointer, but there is no null reference
4. When measuring length, the size of the reference is the size it points to the entity, and the size of the pointer is always the size of the addressing range (4 bytes under 32 bits, 8 bytes under 64 bits).
5. There are multilevel pointers, but only one level of references.
6. Accessing the entity space, the quotation is handled directly by the compiler, and the pointer requires you to dereference it.
7. References are safer than pointers. There are wild pointers for pointers, but there are no wild references for references.

6. Inline Functions

Functions decorated with inline are called inline functions, which expand where the C++ compiler calls them at compile time, without the overhead of the function stack, and improve the efficiency of the program.

summary

That's what you learned today.

Topics: C++