C++ Classes & Objects

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

Classes are used to specify the form of objects. They are user-defined data types that encapsulate a combination of data and functions. The data in a class is called member variables, and the functions are called member functions. A class can be viewed as a template that can be used to create multiple objects with the same properties and behaviors.

C++ class definition

To define a class, you need to use the keywordclassThen specify the name of the class, and the class body is enclosed in a pair of curly braces. The body contains the member variables and member functions of the class.

Defining a class is essentially defining a blueprint for a data type. It defines what objects of the class include and what operations can be performed on that object.

In the following example, we use the keywordclassDefine the Box data type, containing three member variables: length, breadth, and height:

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

KeywordspublicIt determines the access attributes of class members. Within the scope of a class object, public members are accessible from outside the class. You can also specify class members as...privateorprotected, which we will explain later.

Defining C++ Objects

Classes provide the blueprint for objects, so basically, objects are created based on classes. Declaring an object of a class is like declaring a variable of a basic type. The following statements declare two objects of the Box class:

Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box

The objects Box1 and Box2 each have their own data members.

access data members

The public data members of a class object can be accessed using the direct member access operator..to access.

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

Example

#include <iostream> using namespace std; class Box { public: double length; // Length double breadth; // Width double height; // Height // Member function declaration double get(void); void set( double len, double bre, double hei ); }; // Member function definition 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; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box Box Box3; // Declare Box3 of type Box double volume = 0.0; // Used to store volume // Box 1 details Box1.height = 5.0; Box1.length = 6.0; Box1.breadth = 7.0; // Box 2 details Box2.height = 10.0; Box2.length = 12.0; Box2.breadth = 13.0; // Volume of box 1 volume = Box1.height * Box1.length * Box1.breadth; cout << "Volume of Box1:" << volume <<endl; // Volume of box 2 volume = Box2.height * Box2.length * Box2.breadth; cout << "Volume of Box2:" << volume <<endl; // Box 3 details Box3.set(16.0, 8.0, 12.0); volume = Box3.get(); cout << "Volume of Box3:" << volume <<endl; return 0; }

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

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

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

Class & Object Details

So far, we have gained a basic understanding of C++ classes and objects. The following list also includes some other concepts related to C++ classes and objects, and you can click the corresponding links to learn more.

ConceptsDescription
Class Member FunctionsClass member functions are those whose definitions and prototypes are written inside the class definition, just like other variables in the class definition.
Class access modifiersClass members can be defined as public, private, or protected. By default, they are defined as private.
Constructors & DestructorsA class constructor is a special function that is called when a new object is created. A class destructor is also a special function that is called when the created object is deleted.
C++ Copy ConstructorA copy constructor is a special constructor that, when creating an object, uses a previously created object of the same class to initialize the newly created object.
C++ Friend FunctionsFriend FunctionsCan access the 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 place where the function is called.
The this Pointer in C++Every object has a special pointerthis, which points to the object itself.
Pointer to a Class in C++Pointers to classes are used in the same way as pointers to structures. In fact, a class can be viewed as a structure with functions.
C++ Static Members of a ClassBoth data members and function members of a class can be declared static.
other extensions