[C++ Learning] Introduction to C++ (1)

written in front

Welcome to the world of C++, an exciting language.

Well, whenever I start reading a C++ book, the first sentence must be this,

I don't know if programming books in other languages ​​are like this, so let this sentence be the first sentence on my C++ sharing road.

Table of contents

written in front

1. How to learn C++?

1. C language

2. Object-Oriented C++

3. Template C++

4. STL

2. The first C++ program

3. Namespace

usage of namespace

4. Input and output

Write at the end:


1. How to learn C++?

In this article, I also plan to introduce a little C++ grammar, but before that, I want to talk about how to learn C++?

Recently, I read Effective C++ written by Scott Meyers, and I think he wrote it very well.

I will use the first article in his book as an introduction to C++, which is to answer the question of how to learn C++.

Item 1: Treat C++ as a language federation

To learn C++, you need to learn from these four aspects:

1. C language

Yes, C++ is a language based on C language. If you don’t have a solid C language foundation, it will be difficult for you to learn C++.

It is suggested here to learn C language quickly, and then learn C++, don't be greedy.

2. Object-Oriented C++

In human terms, it is object-oriented, and C++ includes object-oriented features, namely the so-called encapsulation, inheritance, polymorphism,

In this column of C++ learning, I also plan to start with classes and objects and start explaining C++.

3. Template C++

That is, the template, here is the part of the generic programming of C++, in fact, the template is powerful, it brings a new programming specification,

That is: template metaprogramming, but I will not go so deep here, it is basically enough to learn the characteristics of templates

4. STL

STL is a standard template library, which is a very important part of C++, and every C++ programmer needs to work with STL,

It is naturally necessary to learn STL well, and then I will explore the bottom layer of some classic containers/adapters of STL, so that we can better understand STL.

certainly,

After learning the above knowledge, you can say that you are a programmer who knows C++, although it is far from being proficient,

The knowledge of C++ is naturally more than these parts, as well as the new syntax of C++11, C++ will continue to be updated in the future (although very slowly)

It also has many important new features that are different from C language, and these new features are what I will introduce next,

The content of C++ introductory, connected with C language, let us enter the world of C++ together!

(PS: Take a look at the handsome photos of the C++ patriarch, and thank the patriarch for the meal, otherwise there would be no such job as a C++ programmer)

2. The first C++ program

#include <iostream>
using namespace std;

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

output:

hello world!

How to put it, in fact, C++ and C language are indeed inseparable.

We all know the C language, the top is the header file, the bottom is the main function, and return 0,

At a glance, cout and cin are related to input and output.

So what is the second line?

This is about the first knowledge point of C++:

3. Namespace

Let's look at this code:

#include <stdio.h>

int printf = 0;

int main()
{
	return 0;
}

Obviously, this code does not compile,

Because printf is a function in stdio.h, this is the problem of naming conflicts in C language,

1. Our conflict with Curry

2. We conflict when we collaborate on projects with other people

You have no way of knowing what name Curry used, and you don't know what other people used,

In order to solve such problems, C++ provides such a keyword: namespace

usage of namespace

For example, we want the previous code to no longer conflict:

 We found that this can be run successfully,

This is the namespace, and within { } of this namespace is a domain. 

In fact, we have learned the scope of variables in the C language stage, (remember the principle of local priority)

For example, this code:

#include <stdio.h>

int a = 10;

int main()
{
	int a = 1;
	printf("%d\n", a);
	printf("%d\n", ::a);

	return 0;
}

We directly access a, and the preferred choice is the variable in the local domain,

What if we want to access the global variable a?

You can use scope qualifiers, as above,

output:

1
10

For another example, look at this code:

#include <stdio.h>

namespace xl {
	int a = 10;
}

int main()
{
	int a = 1;
	printf("%d\n", a);
	printf("%d\n", xl::a);

	return 0;
}

We can access variables inside a namespace like this,

In fact, our program will not actively search the namespace domain,

Only if we specify it ourselves will we go to:

#include <stdio.h>

namespace xl {
	int a = 10;
}

int main()
{
	printf("%d\n", a);

	return 0;
}

Such code cannot be compiled.

At this point, you probably know about namespaces.

In addition to directly specifying the namespace to use variables, you can also expand the namespace,

(In fact, it is to expose variables to the outside)

Look at the code:

#include <stdio.h>

namespace xl {
	int a = 10;
}

using namespace xl;

int main()
{
	printf("%d\n", a);

	return 0;
}

In this way, we can also access the a variable,

 At this time, we should also be able to roughly know the code at the beginning,

using namespace std; What does it mean? In fact, it expands the namespace of the C++ standard library.

Of course, the direct expansion like ours is just for the convenience of our usual code practice,

In fact, when working on a project, we will not directly expand such a large namespace,

Otherwise, why add this namespace.

Let's take a look at the unexpanded code:

#include <iostream>

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

In this way we have solved this problem, look at the code:

#include <stdio.h>

namespace xl {
	int printf = 10;
}

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

output:

hello
10

In fact, the C language cannot solve such a situation.

Replenish:

Namespaces support nesting, see an example:

#include <stdio.h>

namespace xl {
	namespace me {
		int printf = 10;
	}
}

int main()
{
	printf("hello\n");
	printf("%d\n", xl::me::printf);
	return 0;
}

This is the nesting of namespaces, which is still used in some scenarios.

In addition, namespaces with the same name will be automatically merged together, see an example:

#include <stdio.h>

namespace xl {
	int x = 10;
}

namespace xl {
	int y = 20;
}

int main()
{
	printf("%d\n", xl::x);
	printf("%d\n", xl::y);
	return 0;
}

Extended knowledge:

I don’t know if you have ever thought about why C++ header files are not like C language header files,

The header files of C language have the suffix of .h, for example: stdio.h, string.h, etc.,

The C++ header file does not have .h, for example: iostream, vector, etc.,

In fact, under some very old compilers, the so-called #include <iostream.h> header files with .h are supported.

For example, in VC6.0, these header files are not wrapped by namespaces, but later,

C++ stuffs the content of all these header files into the std namespace. In order to distinguish it from before,

Just change this part of the header file stuffed into the namespace to one without the .h ending, like #include <iostream>

Then it is really easy to use with a namespace, C++ gradually eliminated the header files with the end of .h,

So now the header files of the C++ standard library basically don't have .h.

Replenish:

If we don't expand the whole namespace, but there are some variables, objects or functions that we need to call frequently,

How to solve it? We can specify access

Let's see an example:

#include <iostream>
using std::cout;
using std::endl;

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

With using std::, you can specify access.

In this way, you don't need to expand the entire namespace, just expand the commonly used ones.

4. Input and output

cout, cin these are objects,

Compared with printf, cout can automatically identify the type.

Let's go straight to the example:

#include <iostream>
using namespace std;

int main()
{
	//   << 流插入运算符   endl 我们可以直接理解成 \n
	cout << "hello world" << endl;
	
	int n = 10; //自动识别类型
	cout << n << endl;

	double x = 1.1;
	//  >> 流提取运算符,他也能自动识别类型
	cin >> x;
	cout << x << endl;

	return 0;
}	

enter:

2.22

output:

hello world
10
2.22
2.22

Replenish:

Someone may ask, can cout specify the precision like printf?

The answer is yes, but it is very troublesome. If there is such a need, it is recommended to use printf directly

Because the C language and C++ are compatible, whichever is convenient will do.

Replenish:

Some people may say that printf and scanf in C language are faster than cin and cout in C++,

This is indeed the case, because C++ needs to be compatible and synchronized with the C language, it will be slower,

However, there is also a way to turn off the synchronization of C++ to C language.

If in some algorithmic competitions, the C language will indeed be faster,

But in normal times, with our current computer configuration, I think it can be ignored.

Write at the end:

The above is the content of this article, thank you for reading.

If you feel that you have gained something, you can give the blogger a like .

If there are omissions or mistakes in the content of the article, please private message the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/130579506