What is a class ? What is an object ? For object-oriented C++ language learning, the understanding of classes and objects is the core foundation in the entire language learning. Popular understanding is that a class is actually a model, a variable type, and the object is the specific variable defined by this type, just like int a; in this sentence, int corresponds to the class, and a corresponds to the object. This should be easy for everyone to understand, but it should be noted that int is a built-in type of C++, not a real class.
Therefore, in summary: a class is the abstraction and generalization of an object, and an object is the concreteness and instance of a class. Please understand this sentence.
So what do classes in C++ look like? To put it simply, a class in C++ is actually a structure containing functions! Because members in a C++ class can not only contain basic variables like structures in C language, they can also contain functions. The former are called member variables , and the latter are called member methods .
Keywords are defined using the class class. For example, the following defines a C++ class, student class:
classStudent{public:intnum;charname[100];intscore;intprint(){cout<<num<<<<name<<<<score;return0;}};
As you can see, the form is very similar to the C language structure. The members include variables and functions. From now on, we will get used to calling them attributes and methods. It is worth mentioning that everyone should pay attention to the fact that there is also a public thing in the class, which is an access control attribute that controls member access rights. In addition to public , there are three types: private and protected . Among them, private means private. The members declared by it can only be accessed by members of the class and cannot be accessed by the outside world. It is the most closed permission; protected is slightly more public than private. In addition to being accessible by its own members within the class, Its subclasses can also be accessed (we will expand on the concept of subclasses in detail later); and publicly declared members can be accessed by any object of the class and are completely public data . Here you can simply understand it, and then you can experience it while experimenting with the code. It is worth reminding that there is a semicolon after the class definition. Please be careful not to throw it away!
Okay, after understanding the basic appearance of a class, let's show you another way of writing. In the way we just looked at, the member functions are written in the class. If there are many member functions in the class, read It will be a lot messy. Therefore, C++ also supports another way of writing, that is, member functions only declare the function prototype within the class and define the function body outside the class. In this way, you can see a list of all member functions in the class, like a directory. It is clear at a glance and there are many specifications.
The method of declaring a function prototype in a class is the same as the function prototype declaration of the general C language. The method of defining a function outside the class requires adding the :: scope qualifier to the class name. Let’s take the Student class just now as an example. The code defined outside the class is as follows:
classStudent{public:intnum;//student number charname[100];//name intscore;//score intprint();//declare the print function within the class}; intStudent::print()//define complete outside the class print function {cout<<num<<<<name<<<<score;return0;}
Please note that after the print function is declared in the class, we define it completely later. In the function header, between the return value and the function name, the class name plus :: is used to indicate which class the function belongs to.
Please familiarize yourself with the above two usages, and then use the computer to understand by yourself, and choose according to the situation.