Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The C++ class and the object


May 11, 2021 C++


Table of contents


The C++ class and the object

Object-oriented programming has been added to the C language, and C++ supports object-oriented programming. Classes are the core attributes of C+ and are often referred to as user-defined types.

Class is used to specify the form of an object, which contains data notation and methods for processing data. T he data and methods in the class are called members of the class. A function in a class is called a member of a class.

The definition of the C++ class

Defining a class is essentially a blueprint for defining a data type. This does not actually define any data, but it defines what the name of the class means, that is, it defines what the object of the class includes and what can be done on that object.

The class definition begins with the keyword class, followed by the name of the class. T he body of the class is contained in a pair of parentheses. T he class must be defined with a sign or a list of declarations. For example, we use the keyword class to define a Box data type, 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 class members. W ithin 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 cover later.

Define the C++ object

The class provides a blueprint for the object, so basically, the object is created from the class. D eclares the object of the class, just as it declares a variable of the base type. The following statement declares two objects of the class Box:

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

Objects Box1 and Box2 have their own data members.

Access data members

Public data members of class 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;   // 长度
      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 produces the following results:

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

It is important to note that private members and protected members cannot use the direct member access operator (.) for direct access. We'll learn how to access private members and protected members in a later tutorial.

Classes and objects are detailed

So far, we've got a basic understanding of the classes and objects in C+ . There are other concepts related to C++ classes and objects in the following list, which you can learn by clicking on the appropriate links.

concept describe
Class member function The member function of the class refers to the functions of the definition and prototype writing within the class definition, just like other variables in the class definition.
Class access modifier Class members can be defined as public, private, or protected.By default, it is defined as private.
Constructor & destructor The constructor of the class is a special function that is called when creating a new object.The destructor of the class is also a special function that is called when deleting the created object.
C ++ copy constructor The copy constructor is a special constructor that is to initialize the newly created object when creating an object, using an object you created in the same class to initialize the newly created object.
C ++ friend function Friends function You can access the classes of Private and Protected members.
C ++ inline function Through the inline function, the compiler attempts to extend the code in the function body in the place where the function is called.
THIS pointer in C ++ Each object has a special pointer this It points to the object itself.
Pointer to the class in C ++ The pointer to the class is like a pointer to the structure.In fact, the class can be seen as a structure with a function.
Static member of C ++ class Class data members and function members can be declared static.