Friday, January 4, 2013

C++ as an object oriented language

I'm sure that you have at least a little idea about what the object oriented concept is. Before going in depth about C++ as an object oriented language I hope we have to clarify some more things about it.

The major purpose of C++ programming was to add object orientation to the C programming language, which is in itself one of the most powerful languages.

What is an object oriented language?

The core of the pure object oriented programming is to create an object, in code that has certain properties and methods. While designing C++ modules, we try to see the entire world in terms of objects. For an instance a Student is an object which has certain properties such as name, studentID, address, GPA etc . It also has certain methods such as doExam, register and so on.

Is C++ a pure object oriented language?

I think if you have already followed the previous posts of C++, you should know the answer for this partially.     If this language is a  pure object oriented language, you should be able to write the programs completely using objects. C++ supports object orientation but Object orientation is not intrinsic to the language. Did you notice that C++ programs always have a main method and it is not a member of an object.

The big arguments people have against declaring C++ as "pure" is that it still requires at least one non object orientation bit, main() and that not everything is an object. for an instance, int,long,float are just primitive variable types that do not belong to any class. 

As a conclusion, I can say that C++ is not a pure object oriented language but you can still follow object oriented concepts in C++. Let's call C++ as a hybrid object oriented language as it is based on C which is a pure procedural language.

For your knowledge, keep in mind that C# and java are pure object languages.

So, we are close to enter to the world of Object Orientation. I think the best is to be familiar with the terms used in Object Orientation.


An object is the basic unit of object oriented programming. That is, both data and function that operate on data are bundled as a unit called as object.


We can call a class as a blue print of an object. A class doesn't define any data but it does define what the class name means, that is what an object of the class will consist of and what operations can be performed on such an object.


This says how OO provides only essential information to the outside world and hide their background details. that is to represent the needed information in program without presenting the details .

Just think how a database works. You just know what is stored in a database not how because the background details of a database are always hidden and it displays the abstract data all the time. Similar way C++ classes provides different methods to the outside world without giving internal detail about those methods and data.


Encapsulation refers to the mechanism that allow each object to have its own data and methods. It is an inherent in the concept of an abstract data type. Object-oriented programming provides you framework to place the data and the relevant functions together in the same object.

Object-oriented languages provide more powerful and flexible encapsulation mechanisms for restricting interactions between components. When used carefully, these mechanisms allow the software developers to restrict the interactions between components to those that are required to achieve the desired functionality


Inheritance mainly helps in code re usability. It is the process of forming a new class from an existing class. The existing class is called as the super class /base class and the new class is called a child class/ derived class.
This is a very important concept of object oriented programming since this feature helps to reduce the code size.


The ability to use an operator or function in different ways in other words giving different meaning or functions to the operators or functions is called polymorphism.

Polymorphism refers to the capability of having methods with the same names and parameter types exhibit different behavior depending on the receiver. In other words, you can send the same message to two different objects and they can respond in different ways.


The concept of overloading is also a branch of polymorphism. When the exiting operator or function is made to operate on new data type it is said to be overloaded.

More generally, the capability of using names to mean different things in different contexts is called overloading. This also includes allowing two methods to have the same name but different parameters types, with different behavior depending on the parameter types.

Method overriding

Method overriding, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super classes or parent classes. The implementation in the subclass overrides (replaces) the implementation in the super class by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class.

I guess you had some knowledge about Object Oriented programming in C++ from this post. I wanted to start from the OOP basics because up to this post I described you C++ only as a procedural language where you found methods everywhere.  

from the next post onward, I'll describe how to apply OOP in C++ starting from simple programs as I always did :)