C++ Classes & Objects


C++ adds object-oriented programming based on the C language. C++ supports object-oriented programming. Classes are a core feature of C++ and are often called user-defined types.

Class is used to specify the form of an object, which contains data representation and methods for processing data. The data and methods in a class are called members of the class. Functions in a class are called members of the class.

C++ Class Definition

Defining a class is essentially a blueprint for defining 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 an object of the class contains and what operations can be performed on this object.

A class definition begins with the keyword class, followed by the name of the class. The body of the class is enclosed in a pair of curly braces. The class definition must be followed by a semicolon or a list of declarations. For example, we define the Box data type using the keyword class as follows:

class Box
{
   public:
      double length;   // Length of a box
      double breadth;  // Breadth of a box
      double height;   // Height of a box
};

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 the members of the class as private or protected, which we will explain later.

Definition of C++ Objects

Classes provide the blueprint of objects, so basically, objects are created based on classes. Declare objects of a class just like variables of primitive types. The following statements declare two objects of class Box:

Box Box1;          // 声明 Box1,类型为 Box
Box Box2;          // 声明 Box2,类型为 Box

The objects Box1 and Box2 have their respective data members.

Accessing data members

Public data members of objects of a class can be accessed using the direct member access operator (.). To better understand these concepts, let us try the following example:

#include <iostream>

using namespace std;

class Box
{
   public:
      double length;   // 长度
      double breadth;  // 宽度
      double height;   // 高度
};

int main( )
{
   Box Box1;        // 声明 Box1,类型为 Box
   Box Box2;        // 声明 Box2,类型为 Box
   double volume = 0.0;     // 用于存储体积
 
   // box 1 详述
   Box1.height = 5.0; 
   Box1.length = 6.0; 
   Box1.breadth = 7.0;

   // box 2 详述
   Box2.height = 10.0;
   Box2.length = 12.0;
   Box2.breadth = 13.0;

   // box 1 的体积
   volume = Box1.height * Box1.length * Box1.breadth;
   cout << "Box1 的体积:" << volume <<endl;

   // box 2 的体积
   volume = Box2.height * Box2.length * Box2.breadth;
   cout << "Box2 的体积:" << volume <<endl;
   return 0;
}

When the above code is compiled and executed, it will produce the following results:

Box1 的体积:210
Box2 的体积:1560

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 a later tutorial.

Detailed explanation of classes & objects

So far, we have a basic understanding of C++ classes and objects. The following list also lists some other concepts related to C++ classes and objects. You can click on the corresponding link to learn.

ConceptDescription
Class member functionThe member function of a class refers to those that define Functions and prototypes written inside the class definition are just like other variables in the class definition.
Class access modifierClass members can be defined as public, private or protected. By default it is defined as private.
Constructor & DestructorThe constructor of a class is a special function that is called when creating a new object. The destructor of a class is also a special function that is called when the created object is deleted.
C++ copy constructorThe copy constructor is a special constructor that uses previously created objects in the same class when creating an object. Initialize the newly created object.
C++ Friend FunctionFriend FunctionCan access private and protected members of a class.
C++ Inline functionsWith inline functions, the compiler attempts to expand the code in the function body at the point where the function is called.
This pointer in C++Every object has a special pointer this, which points to the object itself.
Pointers to classes in C++Pointers to classes behave like pointers to structures. In fact, a class can be thought of as a structure with functions.
Static members of C++ classesBoth data members and function members of a class can be declared static.