C++ Classes & Objects
C++ adds object-oriented programming on the basis of C. C++ supports object-oriented programming. Classes are a core feature of C++ and are often called user-defined types.
Classes are used to specify the form of objects. They are user-defined data types that encapsulate a combination of data and functions. The data in a class is called member variables, and the functions are called member functions. A class can be viewed as a template that can be used to create multiple objects with the same properties and behaviors.
C++ class definition
To define a class, you need to use the keywordclassThen specify the name of the class, and the class body is enclosed in a pair of curly braces. The body contains the member variables and member functions of the class.
Defining a class is essentially defining a blueprint for a data type. It defines what objects of the class include and what operations can be performed on that object.

In the following example, we use the keywordclassDefine the Box data type, containing three member variables: length, breadth, and height:
KeywordspublicIt determines the access attributes of class members. Within the scope of a class object, public members are accessible from outside the class. You can also specify class members as...privateorprotected, which we will explain later.
Defining C++ Objects
Classes provide the blueprint for objects, so basically, objects are created based on classes. Declaring an object of a class is like declaring a variable of a basic type. The following statements declare two objects of the Box class:
The objects Box1 and Box2 each have their own data members.
access data members
The public data members of a class object can be accessed using the direct member access operator..to access.

To better understand these concepts, let's try the following example:
Example
When the above code is compiled and executed, it produces the following results:
Box1 的体积:210 Box2 的体积:1560 Box3 的体积:1536
It should be noted that private members and protected members cannot be accessed directly using the direct member access operator (.). We will learn how to access private and protected members in later tutorials.
Class & Object Details
So far, we have gained a basic understanding of C++ classes and objects. The following list also includes some other concepts related to C++ classes and objects, and you can click the corresponding links to learn more.
| Concepts | Description |
|---|---|
| Class Member Functions | Class member functions are those whose definitions and prototypes are written inside the class definition, just like other variables in the class definition. |
| Class access modifiers | Class members can be defined as public, private, or protected. By default, they are defined as private. |
| Constructors & Destructors | A class constructor is a special function that is called when a new object is created. A class destructor is also a special function that is called when the created object is deleted. |
| C++ Copy Constructor | A copy constructor is a special constructor that, when creating an object, uses a previously created object of the same class to initialize the newly created object. |
| C++ Friend Functions | Friend FunctionsCan access the private and protected members of a class. |
| C++ Inline Functions | With inline functions, the compiler attempts to expand the code in the function body at the place where the function is called. |
| The this Pointer in C++ | Every object has a special pointerthis, which points to the object itself. |
| Pointer to a Class in C++ | Pointers to classes are used in the same way as pointers to structures. In fact, a class can be viewed as a structure with functions. |
| C++ Static Members of a Class | Both data members and function members of a class can be declared static. |