Happy File C++ Classes & Objects

C++ classes & objects

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.

A class is used to specify the form of an object, which contains data representation and methods for manipulating the data. The data and methods in a class are called members of the class. Functions within 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. A 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;   // 盒子的长度
      double breadth;  // 盒子的宽度
      double height;   // 盒子的高度
};

The keyword  public  determines the access attributes of class members. Within class object scope, 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 a C++ object

Classes provide the blueprint for objects, so basically, objects are created based on classes. Objects of classes are declared just like variables of primitive types. The following statement declares two objects of class Box:

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

Objects Box1 and Box2 have their respective data members.

access data member

Public data members of objects of a class can be accessed using the direct member access operator .

To better understand these concepts, let's try the following example:

example

#include <iostream>
 
using namespace std;
 
class Box
{
   public:
      double length;   // 长度
      double breadth;  // 宽度
      double height;   // 高度
      // 成员函数声明
      double get(void);
      void set( double len, double bre, double hei );
};
// 成员函数定义
double Box::get(void)
{
    return length * breadth * height;
}
 
void Box::set( double len, double bre, double hei)
{
    length = len;
    breadth = bre;
    height = hei;
}
int main( )
{
   Box Box1;        // 声明 Box1,类型为 Box
   Box Box2;        // 声明 Box2,类型为 Box
   Box Box3;        // 声明 Box3,类型为 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;
 
 
   // box 3 详述
   Box3.set(16.0, 8.0, 12.0); 
   volume = Box3.get(); 
   cout << "Box3 的体积:" << volume <<endl;
   return 0;
}

When the above code is compiled and executed, it produces the following result:


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

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 subsequent tutorials.

Class & Object Detailed Explanation{#detail}

So far, we've had a basic understanding of C++ classes and objects. The following list also lists some other C++ class and object-related concepts, you can click on the corresponding link to learn.

concept describe
class member function Member functions of a class are those functions whose definition and prototype are written inside 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 A class constructor is a special function that is called when a new object is created. A class's destructor is also a special function that is called when an object created is deleted.
C++ copy constructor A copy constructor is a special constructor that uses a previously created object in the same class to initialize a newly created object when creating an object.
C++ friend functions Friend functions can access private and protected members of a class.
C++ intrinsics By inlining a function, the compiler attempts to expand the code in the body of the function at the point where the function is called.
The this pointer in C++ Every object has a special pointer  this , which points to the object itself.
Pointer to class in C++ A pointer to a class behaves like a pointer to a structure. In fact, a class can be thought of as a structure with functions.
Static members of C++ classes Both data members and function members of a class can be declared static.

Guess you like

Origin blog.csdn.net/weixin_72686492/article/details/130348545