Object Oriented Programming: Introduction - Online Article


Structured programming can be roughly divided into two categories:

  1. Procedural
  3. Object Oriented

Structured programming is basically dividing a program into smaller modules that are easier to manage. All of what we have covered up to this point is part of procedural programming. In this the programmer divides the program into smaller functions (rather than putting the entire code within one function). Each function would have its own variables and sharing of variables between various functions is also possible (using global variables).

What we will see henceforth is related to object oriented programming (OOP). Large programs were difficult to handle in procedural programming and OOP was developed to reduce complexity of software development. In C++, OOP is implemented using classes. The main concepts in OOP are:

  1. Data Abstraction 
  2. Data encapsulation (hiding) 
  3. Polymorphism 
  4. Inheritance

Data Abstraction

The fundamental data types (like int, float, double, char etc.) might suffice for simple programs. It is difficult for a programmer to always think in terms of the fundamental data types alone. For example, what data type would you use if you want to model a car in a computer application? A car has many properties and each one will be of a different data type (the speed would be an integer, the colour would be a string and so on). We could use a structure to solve our problem but structures also have their limitations. When you create a structure, the user (another programmer who might use your structure or you yourself) can directly manipulate with the member data of the structure. Let’s say that we have created a structure called ‘car’ which has 2 data types for speed and color. You can create an instance of the structure car (say ferrari) and then assign the value for ‘speed’ as 99999. Or you could also set the colour as ‘xyzer’. This would lead to incorrect data and you wouldn’t want the user to enter such values. Thus a structure wouldn’t permit us to control the way data is stored or accessed. A user can simply modify anything by directly accessing it. Wouldn’t it be more convenient if we could have some control over our data type?

Whenever you create such a complex data type, there will also be some operations that you would want to perform on that data type. In C++, these operations (which are implemented through functions) and the data can be bound together as a class. A C++ class is a data type that the programmer defines. Data abstraction refers to the capability of a programmer to create new, user-defined data types. Classes are also called abstract data types (ADTs or user-defined data types) because they are created using the process of abstraction. So, what is the process of abstraction?

Let us consider a simple example of a person. Every person in this world has many attributes (like age, date of birth, name, gender, marital status, salary, number of children, hobbies, strengths, weaknesses, diseases, nationality etc.). If we are designing a patient database application for a hospital, we might not require storing attributes like salary, strengths and weaknesses of a person. So while designing this application we will only choose the particular features of the patient that we are interested in (like age, date of birth, nationality, disease history, gender etc.).

Now if we have to design another system for a corporation, we will consider the person as an employee. An employee database system wouldn’t need to store information like disease history, haemoglobin level etc. In this case we would only be interested in date of birth, salary, qualification etc.

In both cases, our requirement is to store detail information about a person. In one case we have a patient and in one case we have an employee. Depending on our application we selected what we wanted. Abstraction is the process in which we selectively choose what is needed for our application discarding the unnecessary attributes (based on the requirements). You’ll appreciate ADT as you progress through this chapter.

Data Encapsulation

Data encapsulation/ data hiding is an important feature of object oriented programming. The mechanism of hiding data is to put them in a class and make them private. The data is now hidden and safe from any accidental manipulations, i.e. no function (from outside the class) can change the member data. Actually there are two things you can hide: implementation of functions (to the user it doesn’t matter as to how you’ve implemented a particular function) and data. In procedural programming it is possible to only hide the implementation details but you cannot hide/ protect data. OOP lets you achieve this. Why do we need to really hide anything? Who are we hiding the data from? These questions will be answered later in this chapter. A simple example is the case of the ‘car’ we considered earlier. We don’t want the user to directly access ‘speed’ and modify it. By making ‘speed’ private, we prevent the user from doing this.


Polymorphism means having many forms. Polymorphism can be seen frequently in the English language. There are many English words, which have a different meaning depending on the context of use. The statements “close a book”, “close the file”, “close the door” and “close the deal” all make use of the verb ‘to close’ but the meaning of each statement depends on the context. Another example is the sentence, “I’ve cracked the exam”. The meaning of cracked in this case is different from the crack used in a sentence like, “The pot cracked”. In both sentences the word is the same but its interpretation varies depending on the context.

In the same way you can think of many programming examples. For instance, consider the + operator. When it is used on numbers it will act as an addition operator, adding two numbers mathematically and giving the result. When the + acts on two strings, the result will be the concatenation of the two strings (we’ll take a look at string objects which permit us to use + later). For example:

“new” + “delhi” = “newdelhi”.

Thus, though the operator is the same (+), it can perform different actions depending on the type of the operands. This is a simple example of polymorphism. A few other examples of polymorphism are:

The << and >> operators are bit-shifting operators and they are also used for displaying information on the screen or for storing values in a variable (the circumstance decides whether they are used to shift bits or as input and output operators).

The division operator (/) when operating on two integers will produce an integer as the result of division. But if one of the operands are floating point numbers then the result will also be a floating-point number.

In all these cases, the same operator performs different functions depending on the situation. This is called polymorphism.


Just as the name implies, inheritance refers to children inheriting property from their parents. In C++, the parents are called the parent classes and the children are called the derived (or child) classes. The idea of inheritance is to prevent classes from being redefined over and over again. If a programmer has already created a class and you want to make use of the same class with some additional features, then you needn’t re-write the entire class description again. Instead, you can derive a class from the original one (hence all the existing features of the class will be available in your class also) and you can add the extra features you need to your class. This is called re-usability of code. Instead of re-writing, we can re-use through the concept of inheritance. Let’s take the example of animals: a lion belongs to the cat family; the cat family comes under the mammals’ category and the mammals category will come under the general group called animals. Using inheritance, if a lion is being described then only the unique features of a lion need to be defined (you needn’t define the features of animals, mammals and cats). Thus the class ‘lion’ will be inherited from the class ‘cat’ which will in turn be inherited from the class ‘mammals’ which is inherited from ‘animals’.

About the Author:

No further information.


Rajesh Kumar Gupta on 2009-03-03 22:12:18 wrote,

@gajendra kumar jena,
it is ok for a beginner.