A class is a template for creating objects, a class can create multiple objects, each object is a variable of a class type, and the process of creating an object is also called an instantiation of the class. Each object is a concrete instance of the class (Instance), which has member variables and member functions for the class.
I. Definition of Class
The definition of a simple class:
classstudent{ Public: //member Variables Char*name; intAge ; floatscore; //member functions voidsay () {cout<<name<<"The Age is"<<age<<", the results are"<<score<<Endl; }};
classis a new keyword in C + + that is specifically used to define classes. Studentis the name of the class, and the first letter of the class name is generally capitalized to distinguish it from the other identifiers. { }internal is the member variables and member functions contained in the class, collectively referred to as the members of the Class (Member), { } and the enclosing part is sometimes called the class body, similar to the concept of the function body.
Note: At the end of the class definition, there is a semicolon ; , which is part of the class definition, indicating that the class definition is complete and cannot be omitted.
The class is just a template, which does not consume memory space after compilation, so you cannot initialize member variables when defining a class because there is no place to store the data . You can assign a value to a member variable only after the object is created.
Two. Create an object and access the members of the class
The object is created in the following way:
#include <iostream>using namespacestd;//classes are usually defined outside of a functionclassstudent{ Public: //the variables that the class contains Char*name; intAge ; floatscore; //the functions that the class contains voidsay () {cout<<name<<"The Age is"<<age<<", the results are"<<score<<Endl; }};intMain () {//Creating ObjectsStudent Stu; Stu.name="Xiao Ming"; Stu.age= the; Stu.score=92.5f; Stu.say (); return 0;}
Operation Result:
Xiaoming's age is 15, the score is 92.5.
Stu is an object that occupies memory space, so you can assign a value to its member variable, or you can read its member variable. Classes are usually defined outside of a function, but can also be defined inside a function, but are rarely used.
Three. Using the object pointer
The classic pointers in C are still widely used in C + +, especially pointers to objects, without which some functionality cannot be implemented.
The object created in the code above Stu allocates memory on the stack and needs to use & the address that gets it, for example:
*pstu = &stu;
Pstu is a pointer to the Student type of data, which is the object created through Student.
Similarly, you can create objects in the heap area with the new keyword:
New Student;
Such as:
#include <iostream>using namespacestd;classStudent { Public: Char*name; intAge ; floatscore; voidsay () {cout<< name <<", age is"<< Age <<", score is"<< score <<Endl; }};intMain () {//Student Stu;//stu.name = "JACK";//stu.age =;//stu.score = 92.5f;//Stu.say ();//Student Stu; //creating objects in the Stack area//Student *pstu = &stu;Student*pstu =NewStudent;//creating objects in a heap areaPstu->name ="Jack"; Pstu-age =Ten; Pstu-Score =92.5f; Pstu-say (); DeletePstu;//Delete Object return 0;}
Note: objects created on the stack have a name, such as Stu, so it is not necessary to use the pointer to point to it. But the object created by new is not the same, it allocates memory on the heap, has no name, can only get a pointer to it, so you must use a pointer variable to receive the pointer, otherwise you will not be able to find this object, there is no way to use it.
That is, the object created with new on the heap is anonymous and cannot be used directly, it must be pointed to by a pointer, and the pointer is used to access its member variables or member functions.
Stack memory is automatically managed by the program, you cannot use Delete to delete objects created on the stack, the heap memory is managed by the programmer, and the object can be deleted after it is used. In real-world development, new and delete are often paired to ensure that objects that are no longer in use are deleted in a timely manner, preventing useless memory buildup.
C + + Language Basics (3)-Classes and objects