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.


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:

class Box { public: double length; // Length of the box double breadth; // Box width double height; // Box height };

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:

Box Box1; // statement Box1,Type is Box Box Box2; // statement Box2,Type is Box

Objects Box1 and Box2 have their own data members.

Access data members

The 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:


#include <iostream> using namespace std; class Box { public: double length; // length double breadth; // width double height; // height }; int main( ) { Box Box1; // statement Box1,Type is Box Box Box2; // statement Box2,Type is Box double volume = 0.0; // For storage volume // box 1 Detailed Box1.height = 5.0; Box1.length = 6.0; Box1.breadth = 7.0; // box 2 Detailed Box2.height = 10.0; Box2.length = 12.0; Box2.breadth = 13.0; // box 1 volume of volume = Box1.height * Box1.length * Box1.breadth; cout << "Box1 volume of:" << volume <<endl; // box 2 volume of volume = Box2.height * Box2.length * Box2.breadth; cout << "Box2 volume of:" << volume <<endl; return 0; }

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.

< Td> Pointer to class is like pointer to structure. In fact, a class can be thought of as a structure with functions.
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.
C++ The friend functionfriend 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
C++ Both the datamember and the function member of the static member of the class can be declared static.