Classes and Objects

Encapsulation refers to the wrapping up of data and its associated meaningful functions into a single entity. One mechanism that allows programmers to encapsulate data is called a class.  Classes are also called abstract data types as they enforce the object-oriented programming concepts of data abstraction and encapsulation.

Once a class is defined, one can create variables from it. A class variable is called an object or an instance. In other words, an object is an identifiable entity having certain characteristics and behaviour as defined by a class.

Taking an example, using C++,

class Name {


      //data members and methods that remain private i.e. cannot be accessed outside the class

   protected:      //data members and methods that are protected i.e. accessible from members of their same class and also from members of their derived class (see Inheritance)


    int mem1;    void met1();

    //data members and methods that are public i.e. accessible from anywhere

} Ob1, Ob2;

Here, ‘Name’ is the name of the class and ‘Ob1’ and ‘Ob2’ are its objects. The body of the declaration can contain members, either in the form of data or of function declarations.  These data members and member functions/methods are classified into three categories: private, public and protected. These are reserved words and called access specifiers. As described in the comments, the access specifiers essentially determine where a member of the function can be used in the entire program. By default, all members of a class declared with the class keyword have private access unless otherwise specified.

To access the class methods and data members, objects are used as follows:

Ob1.met1(); //accessing public member function met1() through the first object ‘Ob1’

cin≫ Ob2.mem1; //accessing public data member mem1 through the second object ‘Ob2’

Now how does it work in the memory? For each object of the class Name, a different set of values can be stored. This means mem1 for Ob1 (accessed by Ob1.mem1) can store, say, 4, while mem1 for Ob2 (accessed by Ob2.mem1) can store, say, 20. To relate, one can compare this with a school classroom list – here, the C++ class becomes ‘A’ (for classroom section) and it can hold the names of students, with their roll numbers, personal information, marks in different subjects, etc. Thus, classes can be used to streamline storage of students’ details while selectively privatizing personal information.

See also: Inheritance, Polymorphism, Why Object-Oriented Programming is better?


Merriam Webster defines ‘polymorphism’  as the ‘quality or state of existing in or assuming different forms’. In simpler terms, polymorphism is the ability of a message/data to be interpreted in more than one form. Thus, one interface can be used in different situations.

There are two types of polymorphism:

When the arguments of methods are resolved during compilation stage, it is called Static Binding or Early Binding of arguments. Alternatively, it is also called Compile Time Polymorphism. This type of polymorphism is achieved by overloaded functions and operators.

Overloading means having two or more meanings and enforces polymorphism in a program.

  • Overloaded Functions

A function with the same name having several different definitions, which can be distinguished by the number and types of arguments is said to be overloaded. This process is known as function overloading.

It reduces the number of comparisons in a program, reduces its length and thus, makes it faster.

  • Overloaded Operators

An operator capable of carrying out more than one action is called an overloaded operator. For example, the addition operator with numbers carries out addition and while the same ‘+’ sign can be used for concatenation of strings too. For example:

5 + 7 = 12

‘A’ + ‘BC’ = ‘ABC’

When the arguments of methods are resolved during run time, it is called Run Time Polymorphism. This type of polymorphism is achieved using virtual functions or through function overriding.

A virtual function is a method/member function of an abstract class whose functionality (or implementation details) are given through derived classes called concrete classes. A function is virtual when the keyword ‘virtual’ is used in the declaration of it.

For example, the function draw() is virtual in the class shape (abstract class) in C++

class shape



   virtual void draw() = 0;  //virtual function (can be defined in the concrete class)


Now, there can be multiple definitions of draw() in the derived, concrete classes, thereby implementing polymorphism.

Why Object-Oriented Programming is better?

Before we can even discuss why object-oriented languages are far better for real-world applications than those that only follow procedural programming, we need to understand what this really means.

A programming paradigm defines the methodology required for designing and implementing a program using the key features of a programming language. So essentially, a programming paradigm describes the logical flow and implementation of a code. Let’s take a simple example, say you had to take the details of some five students along with their marks in Computer Science. There are several approaches to the condition, a two-dimensional array with 5 rows and 2 columns could store the names of the students with their marks or, a structure can be used to do the same thing and one might even consider using a class and create objects for the values, wherein the member functions can be used to take and display the required data.

While there are several types of programming paradigms, let’s limit ourselves to Procedural Programming and Object-Oriented Programming (OOP). Refer back to the previous example – a structure would group together the names of the student and the marks and through separate functions successfully carrying out the task – this is procedural programming. Now if we were to club the names, marks and the functions required to take the details and display them into a single class, we’d be using object-oriented programming. In OOP, Data and its associated functions are clubbed together in classes, whose instances, called objects are used in the program.

Thus, the main difference between the two is the fact that OOP encloses data and its associated functions into one using classes while procedural programming separates the two. This fact makes a program that follows procedural programming over OOP highly susceptible to design changes i.e. change in the definition of a type changes the design of the program.

There are several benefits to using OOP paradigm but the most important is the fact that it has a real-world application as it can depict relationships between objects through inheritance. It allows implementation of polymorphism, data hiding, and other such object-oriented objects. OOP languages allow for smart programming, reuse of code, easy understanding and even easier redesign and maintenance.

Let’s summarise the features of these two paradigms:

Procedural Programming
Object Oriented Programming (OOP)