OOP means Object Oriented Programming and is used in different languages and C++ is one of them. What really distinguish C++ from C is that C++ supports Object Oriented Programming. It is one of the most powerful techniques and influential developments ever used in computer programming. Now-a-days it has gained a widespread use. Like C++ many languages support OOP like Java, PHP VB.NET, etc.
Basically there are two types of programming, namely procedural language, that is, programs written in steps of code where C or some other languages like Pascal uses and the second approach is OOP where C++ follows. To consider the advantages of both, the organization of a program in the later approach is well defined.
Related links: Types of Inheritance in C++
As a programmer, you must have a basic familiarity with programming in general, before we move forward. It is not necessary to give you the full explanation about the current discussion, but anyhow, to some extent and give you some indication of the flavor of the Object Oriented Programming languages. If you don’t have any knowledge of this concept, reading the major three concepts that OOP has, will give you a better understanding.
Some authors have divided OOP into two phases that is classic and modern. The first phase in OOP can be traced back to a language called SIMULA and especially SIMULA 67. This language was very popular in 1960s. It was this language that first instituted classes and objects, leading to the term Object Oriented programming. Later, the scientist gained enough experience with OOP and they discover some limitations of this concept. It was Xerox who developed Smalltalk language and released it to the outside world. Smalltalk was a pioneer language to the Graphical User Interface (GUI). Later C language became a popular language because of the UNIX operating system which was written in C. Basically all credit goes to the developer of C language “Denis Ritchie“ which became the candidate for C++ language developed by “Bjarne Stroustrup“. The modern OOP working was first completed by David Ungar and Randall Smith. After sometime, Sun Microsystems picked up the project developed by the two scientists. But to some extent, it did become the dominant project.
The OOP language is divided into four major concepts.
Encapsulation in C++
Encapsulation is one of the major cores of OOP. It is the mechanism that binds code and data together and keeps them safe and away from outside inference and misuse. In short, it isolates a data from outside world. Encapsulation can be called as the core concept of OOP because it is encapsulation that reduces the maintenance burden, and limiting your exposure to vulnerabilities. The programmer has to design a well-defined interface to control the access of a particular code and data. A class defines the data and code which is shared by a set of objects. A class is a logical construct and an object is a physical construct which exists in reality that is like a chair or a table. A class comprises of code and data which are collectively called members of the class.
Inheritance in C++
It is the technique used in OOP that one object acquires the properties of another object without redefining in order to create well defined classes. This technique also supports the hierarchical classification in which each object would not need to be redefined by all its characteristics explicitly. It prevents the programmer from unnecessary work and it is the inheritance concept that does this job. Inheritance gives us the facility to an object to inherit only those qualities that make it unique within its class. In general, it can inherit any attributes from its parent. Again to be well noted that it is the inheritance that get both data and functionality. We can later add any new data and methods needed. Different names have been used in different times for a parent class and a child class. The concept of parent and child class was developed to manage generalization and specialization in OOP and it is represented by a is-a relationship. But now-a-days, the following terms in object oriented programming is commonly used names given to parent and child class.
- Super Class: Parent Class
- Sub Class: Child Class
- Base Class: Parent Class
- Derived Class: Child Class
The generalization means that an object encapsulates common state behavior for a category of objects. The concept of inheritance makes code readable and avoids repetition. Suppose we have different shapes and they have same or different colors. With the concept of inheritance included later on to design a shape class that has a color attribute and to inherit the color attributes.
C++ also supports multiple inheritances. It is the process that a child class can have multiple parent classes. In other words, it occurs when a class is derived from more than one classes. A good example for a better understanding in a real life would be a child with characteristics of his parents. For a novice programmer it is not only difficult to handle multiple inheritances but also allows ambiguity for the compiler. There are programming languages such as Java and the .NET that does not support multiple inheritances.
Polymorphism in C++
Photo Credit: j2sejava.wordpress.com
This concept was first identified by Christopher Strachey in 1967. Later, developed by Hindley and Milner. It is not important to understand how polymorphism is implemented but how it works. C++ offers two ways to retrieve the type of objects and expressions while the program is running, at compile time and run time. The basic idea behind polymorphism is that the compiler does not know which function to call at compile-time. This is done at run time. The term polymorphism refers to the fact that it is used in object oriented programming that the decision to be used will not be known at compiler time. The decision to be postponed until the program is actually executed which respond at run time. The run time is referred to late binding. Polymorphism is realized by this feature.