Take you to learn C++ classes & objects of the series every day

C++ Classes & Objects

C++ adds object-oriented programming on the basis of 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, it contains data representation and methods for manipulating the 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 consists of, 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;    // the length of the box 
      double breadth;   // the width of the box 
      double height;    // the height of the box 
};

The keyword public determines the access attributes of class members. In 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.

Defining C++ Objects

Classes provide the blueprint for objects, so basically, objects are created from classes. Declare an object of a class just like declaring a variable of a primitive type. The following statement declares two objects of class Box:

Box Box1;           // Declare Box1, type Box 
Box Box2;           // Declare Box2, type Box

Objects Box1 and Box2 have their own data members.

Access data members

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

#include <iostream>

using namespace std;

class  Box 
{ 
   public :
       double length;    // length 
      double breadth;   // width 
      double height;    // height
};

int main( )
{
   Box Box1;         // Declare Box1, type Box 
   Box Box2;         // Declare Box2, 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;

   // the volume of box 1
   volume = Box1.height * Box1.length * Box1.breadth;
   cout << "Volume of Box1:" << volume << endl ;

   // the volume of box 2
   volume = Box2.height * Box2.length * Box2.breadth;
   cout << "The volume of Box2:" << volume << endl ;
    return  0 ;
}

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

Volume of Box1  : 210 Volume of 
Box2  : 1560

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

Class & Object Details

So far, we have 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 the corresponding link to learn.

concept describe
class member function Member functions of a class are those functions whose definitions and prototypes 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 The constructor of a class is a special function that is called when a new object is created. The destructor of a class is also a special function that is called when the created object is deleted.
C++ copy constructor A copy constructor is a special constructor that initializes a newly created object with a previously created object in the same class when an object is created.
C++ friend functions Friend functions can access the private and protected members of the class.
C++ inline functions By inlining a function, the compiler tries to expand the code in the function body where the function is called.
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.

Leave a Comment

Your email address will not be published. Required fields are marked *