CE103 Algorithms and Programming I
C++ Classes and Objects
In this tutorial, we will learn about objects and classes and how to use them in C++ with the help of examples.
In previous tutorials, we learned about functions and variables. Sometimes it's desirable to put related functions and data in one place so that it's logical and easier to work with.
Suppose, we need to store the length, breadth, and height of a rectangular room and calculate its area and volume.
To handle this task, we can create three variables, say, length, breadth, and height along with the functions calculateArea()
and calculateVolume()
.
However, in C++, rather than creating separate variables and functions, we can also wrap these related data and functions in a single place (by creating objects). This programming paradigm is known as object-oriented programming.
But before we can create objects and use them in C++, we first need to learn about classes.