C++ classes and objects!

C++ adds object-oriented programming to the C language, and C++ supports object-oriented programming. Classes are the 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 data representation and methods for processing data. The data and methods in the 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 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 operations can be performed on this object.

The class definition starts 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 declaration list.

For example, we use the keyword  class to  define the 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 attributes of class members. Within the scope of the 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.

 

Define C++ objects

Classes provide a blueprint for objects, so basically, objects are created from classes. Declaring objects of a class is like declaring variables of basic types. The following statement declares two objects of class Box:

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

The objects Box1 and Box2 have their own data members.

 

Access data member

The public data members of objects of the class can be accessed using the direct member access operator (.). In order to better understand these concepts, let us try the following examples:

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

The volume of Box1: 210
The volume of 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 members and protected members in subsequent tutorials.

 

Class & Object Detailed

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.

Guess you like

Origin blog.csdn.net/u010608296/article/details/113108467