Preface
Before learning C++, it is assumed that everyone already has the foundation of C language. If you have not learned C language yet, it is recommended that you learn C language first.
The C++ language is a programming language designed to support object-oriented programming based on the C language. The object-oriented language in C++ is the class mechanism , so C++ is also called the "C language with classes." So what is a class?
We might as well start with the structure of C language. I believe you still have some impressions, such as C language code:
structstu{intnum;charsex;intmath_score;inten_score;intc_score;};intmain(){structstuA;return0;}
The above C code defines a structure type of struct stu, which has five member variables , and then defines a variable A of this structure type in the main function. This is the C language code we are familiar with. In fact, Classes in C++ are similar, but they are more advanced than C structures. They are no longer called structures, but called classes . In addition, in addition to many basic variable types, they can also contain many functions. For the first section For a preliminary understanding of the class, that’s enough for us to have a general understanding.
For example, the corresponding C++ code is:
classstu{intnum;charsex;intmath_score;inten_score;intc_score;inttotal_score(){returnmath_score+en_score+c_score;};};intmain(){classstuA;return0;}
Let's compare it carefully. C language uses struct to define, and C++ uses class to define. Secondly, there is an extra function in the class in C++. This is what makes the class in C++ different. In addition, the names are also different. The member variables in a C++ class are called attributes , and the functions in the class are called methods . That is, the class has two parts: attributes and methods.
Of course, in addition to this, there are many differences, which we will introduce slowly in the future.