C++ Object Oriented Programming
C++ supports procedural programming style, object based programming style and object oriented programming style.
Various Programming Paradigms
A programming paradigm defines the methodology of designing and implementing programs using the key features and other building blocks (such as keywords, function preprocessor directions etc.) of a programming language. A programming paradigm gives you an idea how problems are generally analysed and solved in a particular programming language.
Let's discuss about the following :
- Procedural Programming
- Object Based Programming
- Object Oriented Programming
Procedural programming lays more emphasis on procedure than data. As C++ is superset of C, which uses procedural programming style, C++ can also be used as procedural programming language but with some enhanced features such as tighter type checking, reference variables, inline functions default arguments etc.
Procedural programming paradigm separates the functions and the data manipulated by them. This leads to many problems when it comes to extending the software or maintaining the software as it is being explained in the following lines :
- Procedural Programming is susceptible to design changes
- Procedural Programming leads to increased time and cost overheads during design changes
Object Based Programming
Object based Programming is a newer paradigm that implements some features of object oriented programming but not all. In object based programming, data and its associated meaningful functions are enclosed in one single entity a class. Classes enforce information hiding and abstraction thereby separating the implementation details (how the process is actually taking place) and the uses interface (i.e., how the uses views and uses it). For example, you can consider a calculator, its interface would include a small display screen and a few buttons that is available to user and implementation details i.e., how actual calculations are taking place are hidden from user.
In other words, users of a class are allowed to access its interface but they cannot access its implementation details.
- Object based programming localizes the implementation details
- Object based programming is subset of Object Oriented Programming
- Advantages and Limitations of Object based programming
Object Oriented Programming
The object oriented programming paradigm is superset of object based programming. It offers all the features of object based programming and overcomes its limitation by implementing inheritance so that real-world relations among objects can be represented programmatically.
To understand the object oriented programming paradigm, let us take an example. We have to prepare lot of dishes that involve baking ; for instance, cake, biscuits, pie, dhokla, pastries, buns etc. We have to write programs for it. Using procedural programming paradigm, we'll have to write separate programs for every recipe and each program will be containing instructions for operating oven. Ovens are used to make a lot of different dishes. We don't want to create a new oven every time we encounter a new recipe. Having solved a program once, it would be nice to be able to reuse the solution in future programs. But the procedural programming paradigm suffers from this drawback. Though functional software reuse is theoretically possible, but it hasn't worked very well in practice. The large amount of interaction between conventional functions and its surroundings makes reusing them difficult.
However, if the same problem is solved using Object Oriented approach, it'll not only be less complex but also make software reuse feasible and possible. The object-oriented approach views a problem in terms of objects involved rather than procedure for doing it.
Object - An Object is an identifiable entity with some characteristics and behaviour.
Class - A Class is a group of objects that share common properties and relationships.
Basic Concepts of OOP
The object oriented programming has been developed with a view to overcome the drawbacks of conventional programming approaches. The OOP approach is based on certain concepts that help it attain its goal of overcoming the drawbacks or shortcomings of conventional programming approaches. These general concepts of OOP are given below :
- Data Abstraction
- Data Encapsulation
Now let's discuss these concepts in little details to understand their meaning.
Abstraction is the concept of simplifying a real world concept into its essential elements.
Abstraction refers to the act of representing essential features without including the background details or explanations.
Encapsulation is the most fundamental concept of OOP, it is the way of combining both data and the functions that operate on that data under a single unit.
The wrapping up of data and operations / functions (that operate on the data) into a single unit (called class) is known as Encapsulation.
The act of partitioning a program into individual components is called modularity.
The justification for partitioning a program is that
- it reduces its complexity to some degree and
- it creates a number of well-defined, documented boundaries within the program.
Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules.
Inheritance is the capability of one class of things to derive capabilities or properties from another class.
Polymorphism is the key to the power of object oriented programming, it is so important that languages that don't support polymorphism cannot advertise themselves as OO languages.
Polymorphism is the ability for a message or data to be processed in more than one form.
Advantages and Disadvantages of OOP
The advantage offered by OOP are :
- Re-use of code
- Ease of comprehension
- Ease of fabrication and maintenance
- Easy redesign and extension
« Previous Tutorial Next Tutorial »