C++ adds object-oriented programming to the C language, and C++ supports object-oriented programming. Classes are a core feature of C++ and are often referred to as user-defined types.
The class is used to specify the form of the object, which contains the data representation and the methods used to process the data. The data and methods in a class are called members of a class. A function is called a member of a class in a class.
C++ class definition
Defining a class is essentially a blueprint that defines a data type. This doesn't actually define any data, but it defines what the name of the class means, that is, it defines what the class's objects include and what operations can be performed on that object.The
class definition begins with the keyword class followed by the name of the class. The body of the class is contained in a pair of curly braces. The class definition must be followed by a semicolon or a statement list. For example, we use the keyword class to define the Box data type as follows:
The keyword public determines the access properties of the class members. Within the scope of a class object, public members are accessible outside the class. You can also specify that the members of the class are private or protected, which we'll explain later.
Define C++ objects
The class provides a blueprint for the object, so basically, the object is created from the class. The object of the statement class is just like the variable of the statement base type. The following statement states two objects of the class Box:
Objects Box1 and Box2 have their own data members.
Access data membersThe public data members of the
class's objects can be accessed using the direct member access operator (.). To better understand these concepts, let's try the following example:
When the above code is compiled and executed, it produces the following results:
Box1 Volume:210 Box2 volume:1560
It's important to note that private and protected members cannot be accessed directly using the direct member access operator (.). We will learn how to access private and protected members in a follow-up tutorial.class & Detailed object description
So far, we have a basic understanding of C++ classes and objects. The following list also lists other C++ classes and object-related concepts that can be learned by clicking on the appropriate links.
|Class member functions The member functions of the class||are functions that write definitions and prototypes 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 it is defined as private.|
|Constructor & The constructor of the destructor||class is a special function that is called when a new object is created. The class's destructor is also a special function that is called when the created object is deleted.|
|C++ Copy Constructor||A copy constructor that is a special constructor that initializes a newly created object with an object created earlier in the same class when the object is created. Td>|
|C++ The friend function||friend Function can access the private and protected members of the class.|
|C++ Inline Functions||Through inline functions, the Compiler attempts to extend the code in the body of the function where the function is called.|
|C++ The this pointer in the||each Object has a special pointer this that points to the object itself.|
|C++ Pointer to class in class< Td> Pointer to class is like pointer to structure. In fact, a class can be thought of as a structure with functions.|
|C++ Both the datamember and the function member of the static member of the class||can be declared static.|