C++ Classes and Objects


Go to main Page

Classes and Objects in C++

In this article, we will learn about Objects and Classes, as well as how to use them in C++ with the help of a examples.In previous instructive lessons, we learned about functions and variables. It's also appealing to group together similar features and facts so that it's more logical and easier to deal with.
Assume we need to store the length, width, and height of a rectangular space in order to measure its volume.To create this program we have to use three variables: length,width,height and we have to use to functions Calculatearea() and CalculateVolume().
In any case, rather than creating variables and functions in C++, we can also enclose these related information and functions in a single location (by making objects). This programming perspective is referred to as object oriented programming.
Before learning objects we have to understand the concept of Classin C++.

C++ Class

A class is a schematic of an object.
We can understand a room which has table, chair and desk.We built the house room class on these definitions. The object is a room.

How to create class in C++

In C++, a class is identified by using the keyword class followed by the class name.
The curly represent the class's body, which itself is proceeded by a semicolon.

For example:

class Room {
    public:
        float length;
        float width;
        float height;   

        float Area(){   
            return length * width;
        }

        float Volume(){   
            return length * width * height;
        }

};

C++ Objects

When a class is defined, only the object's specifications are defined; no memory or capacity is reserved. To use the class's data and access features, we must first create objects.

Syntax

We can create objects of Room class as follows:


void function1() {
    Room room1, room2;
}

int main(){ 
    Room room3, room4;
}

Access Members Functions and Data Member in C++

Using a. (dot) sign, we will access the data individuals and part elements of a class.For example,

room3.calculateParameter();

This will execute the Room class's calculateArea() function for target room2. Likewise, data members can be handled as follows:

room3.width = 2.2;

Object and Class in C++ Programming

Example 1


    //Program to calculate area of a room in C++ Programming Using Objects and Classes
#include 
using namespace std;

//We are creating class here
class Room {  

   public: // we are setting variables public 
    float length;
    float width;
    float height;

    float area() {
        return length * width;
    }

    float volume() {
        return length * width * height;
    }
};

int main() {

    // Creating a room class object
    Room room1;

    // assign values to data members
    room1.length = 2.5;
    room1.width = 10.8;
    room1.height = 12.2;

    // Calculation and Display of Area
    cout << "Area of Room =  " << room1.area() << endl;
    
    // Calculation and Display of Volume
    cout << "Volume of Room =  " << room1.volume() << endl;

    return 0;
}

Example

Area of Room =  27
Volume of Room =  329.4

In the above example we used Room Class and room1 object to calculate the area and volume of room and in the main() part we have assigned the values of length,width and height .

Public and Private in C++ Classess and Objects

We keep the variables and functions private if we do not want to access variables and functions out of class.
We keep the variables and functions public if we want to access variables and functions out of class.

Using public and private in C++ Class

Example 2


    
//Example of  public and private in C++ Class

#include 
using namespace std;

class Room {

   private:
    float length;
    float width;
    float height;

   public:

    //Here is the function to initialize private variables
    void getData(float len, float wid, float hgt) {
        length = len;
        width = wid;
        height = hgt;
    }

    float area() {
        return length * width;
    }

    double volume() {
        return length * width * height;
    }
};

int main() {

    // create object of Room class
    Room room1;

    //here we are passing the values of private variables as arguments
    room1.getData(2.5, 3.8, 9.2);
//calculating and displaying the Area
    cout << "Area of Room: " << room1.area() << endl;
//calculating and displaying the Volume
    cout << "Volume of Room :  " << room1.volume() << endl;

    return 0;
}

Output


 Area of Room: 9.5
Volume of Room :  87.4