Everyone, it’s time to learn again!
Today! What spiritual food are we bringing? It’s classes! A class is a collection of objects that have the same attributes and operations, providing a unified abstract description for all objects belonging to that class. It includes two main parts: attributes (data variables) and operations (member functions).
In short, a class can be understood as a user-defined data type, and the variables declared with this data type are called objects.
Class Definition
During design, attributes and behaviors are encapsulated together to represent things.
Syntax:
class ClassName {
AccessModifier:
Attributes; // Member variables
Behaviors; // Member functions
};
A class is an abstraction of a category of things, and if needed, instantiate an object (define a variable).
The definition of a class is similar to that of a regular variable, i.e., “ClassName ObjectName”. The space occupied by an object is the sum of the memory sizes of each data member in the class.
Access to members of a class uses the ” . “ operator (dot operator). When the object is a pointer, use the “->” operator for access.
Access Modifiers
public: The public keyword is used to specify public members. Public members can be accessed both inside and outside the class. This means any function can access public members without special access restrictions.
protected: The protected keyword is used to specify protected members. Protected members can be accessed within the class and by derived classes, but not from outside the class. This means only in derived classes can protected members be accessed, but nowhere else.
private: The private keyword is used to specify private members. Private members can only be accessed within the class. This means no other functions or classes can access private members except the class itself.
Class Constructors and Destructors
Each object in C++ needs initialization settings and cleanup work before destruction.
Initialization: Constructor
Cleanup work: Destructor
These two functions are automatically called by the compiler. If we do not provide them, the compiler will provide an empty implementation of the constructor and destructor.
Constructor
Automatically called when creating an object, purpose: to assign values to member attributes.
ClassName() {}
Note: The function name must match the class name.
Destructor
Cleanup work performed before the object is destroyed.
~ClassName() {}
Note: The function name and type must match, with a ~ added before the name.
Difference Between Class and Structure
A structure is a collection of one or more variables, which may be of different types. Structures group these variables under a name for easier usage. Compared to structures, classes also contain functions that operate on variables. Although structures can also include some operations and usage functions, they are generally not used this way. In short, classes are generally used for data abstraction and further inheritance, while structures are typically used for data grouping.
The variables and functions within a structure are generally public and can be used outside the structure’s scope; however, the variables and functions of a class are generally private and can only be used within the class’s scope.
For their inheritors, by default, the members of a structure are public, while the members of a class are private.