Do you still remember the sentence from the last lesson: "A class is the abstraction and generalization of an object, and an object is the concreteness and instance of a class"? After learning the definition of a class, the next step is to create and use objects !
1. Creation of objects:
A class is a structure that contains functions. It is a self-defined data type. Use it to define variables, which are objects. This is the so-called "object is a concrete instance and instance of a class." When an object of this class is defined, it can also be said Instantiate an object, that's what it means!
The use of objects is the same as the use of structures. They mainly access the members inside, and they are all accessed using . , such as:
StudentA;A.num=101;strcpy(A.name,dotcpp);A.score=100;A.print();
It should be noted that the member variables in the class here are all declared as public types . If they are declared as private types, they will be prohibited from reporting errors if they are directly accessed mainly through objects and variables in the main function. The reason is that the private type Variables are private types and do not allow external access.
For private variables that we want to protect but want to control, we usually declare them as private types, and then define a special assignment method of the public type at the same time. Since internal members can access privately declared variables, we can use this public externally. method to indirectly control these private members to achieve the effect of encapsulation and protection, and this public type method is also called an external interface of this class. Please understand this sentence!
2. Pointer to object:
Like ordinary variables, objects are also a continuous memory space, so you can also create a pointer to the object, that is, the object pointer , to store the address of the object.
Then the creation method is similar to using a general type pointer, and the definition method is as follows:
Class name * pointer name;
For example, define Student *p; define a pointer p of Clock type. It should be clear that the object is not created here, and of course the constructor will not be called. Next, you can assign the address of a class object of the same type to this pointer, and then access the members in the object through ->.
The code is as follows:
Student*p;StudentA;p=&A;p->print();
The above is how to use object pointers. In addition to using them when assigning values and accessing members, it is also recommended to use pointers when passing parameters, because they pass addresses and do not copy assignments between objects, thus reducing Memory overhead and improve efficiency.
3. Object reference:
Reference is a new type in C++. An object reference is an alias for a class object. In essence, the address of the class object is assigned to the reference type. Both of them point to a piece of memory space. So how to define use? Show it to you below.
StudentA;Student&Aq=A;
As shown in the above code, define an object of type Student, then use & to define a reference type of this type, and assign the A object to Aq as initialization.
Things to note are:
1. Like pointers, both must be of the same type before they can be referenced.
2. Unless the return value or formal parameter of a function is used, other reference types must be initialized at the same time!
3. The reference type does not create a new object, so the constructor will not be called.
So since it is an alias of a class object, the usage method is the same as the class object, using the alias.member method to access, such as:
StudentA;Student&Aq=A;Aq.print();
As you can see, when using a reference type, the essence is still the stored address. Therefore, no matter the parameter definition, there will not be too much memory overhead. It has the advantage of pointers. At the same time, it is used the same as the class object itself. When making function arguments, , just pass in the reference object directly without adding address characters, so it looks more intuitive and convenient. This is the advantage of reference types.