Introduction To Object-Oriented Programming In C++.
With this tutorial, we are starting a series of tutorials to learn about C++ as an Object-oriented programming language.
As we know, prior to object-oriented programming (OOP), programs were written using procedural languages. Procedural languages stress functions. The bigger problems are broken down into smaller sub-problems and written as functions.
=> See Here To Explore The Full C++ Tutorials list.
Procedural languages did not pay attention to data. As a result, the possibility of not addressing the problem in an effective manner was high. Also, as data was almost neglected, data security was easily compromised.
All these drawbacks of procedural programming were overcome by object-oriented programming.
In this tutorial, we will discuss the fundamentals of object-oriented programming and in short all the features of OOP.
What You Will Learn:
Object-Oriented Programming In C++
Object-oriented programming revolves around data. The main programming unit of OOP is the object. An object is a representation of a real-time entity and consists of data and methods or functions that operate on data. This way, data, and functions are closely bound and data security is ensured.
In OOP, everything is represented as an object and when programs are executed, the objects interact with each other by passing messages. An object need not know the implementation details of another object for communicating.
Apart from objects, OOP supports various features which are listed below:
Using OOP, we write programs using classes and objects by utilizing the above features. A programming language is said to be a true object-oriented programming language if everything it represents is using an object. Smalltalk is one language which is a pure object-oriented programming language.
On the other hand, programming languages like C++, and Java are said to be partial object-oriented programming languages.
Why C++ Is Partial OOP?
C++ language was designed with the main intention of using object-oriented features to C language.
Although C++ language supports the features of OOP like Classes, objects, inheritance, encapsulation, abstraction, and polymorphism, there are few reasons because of which C++ is classified as a partial object-oriented programming language.
We present a few of these reasons below:
#1) Creation of class/objects is Optional
In C++, the main function is mandatory and is always outside the class. Hence, we can have only one main function in the program and can do without classes and objects.
This is the first violation of Pure OOP language where everything is represented as an object.
#2) Use of Global Variables
C++ has a concept of global variables that are declared outside the program and can be accessed by any other entity of the program. This violates encapsulation. Though C++ supports encapsulation with respect to classes and objects, it doesn’t take care of it in case of global variables.
#3) Presence of a Friend Function
C++ supports a friend class or function that can be used to access private and protected members of other classes by making them a friend. This is yet another feature of C++ that violates OOP paradigm.
To conclude, although C++ supports all the OOP features mentioned above, it also provides features that can act as a workaround for these features, so that we can do without them. This makes C++ a partial Object-oriented programming language.
Here we will introduce various OOP features that are used for programming.
Classes & Objects
An object is a basic unit in object-oriented programing. An object contains data and methods or functions that operate on that data. Objects take up space in memory.
A class, on the other hand, is a blueprint of the object. Conversely, an object can be defined as an instance of a class. A class contains a skeleton of the object and does not take any space in the memory.
Let us take an Example of a car object. A car object named “Maruti” can have data such as color; make, model, speed limit, etc. and functions like accelerate. We define another object “ford”. This can have similar data and functions like that of the previous object plus some more additions.
Similarly, we can have numerous objects of different names having similar data and functions and some minor variations.
Thus instead of defining these similar data and functions in these different objects, we define a blueprint of these objects which is a class called Car. Each of the objects above will be instances of this class car.
Abstraction is the process of hiding irrelevant information from the user. For Example, when we are driving the car, first we start the engine by inserting a key. We are not aware of the process that goes on in the background for starting the engine.
Using abstraction in programming, we can hide unnecessary details from the user. By using abstraction in our application, the end user is not affected even if we change the internal implementation.
Encapsulation is the process using which data and the methods or functions operating on them are bundled together. By doing this, data is not easily accessible to the outside world. In OOP we achieve encapsulation by making data members as private and having public functions to access these data members.
Using inheritance object of one class can inherit or acquire the properties of the object of another class. Inheritance provides reusability of code.
As such we can design a new class by acquiring the properties and functionality of another class and in this process, we need not modify the functionality of the parent class. We only add new functionality to the class.
Polymorphism means many forms.
Polymorphism is an important feature of OOP and is usually implemented as operator overloading or function overloading. Operator overloading is a process in which an operator behaves differently in different situations. Similarly, in function overloading, the same function behaves differently in different situations.
OOP supports dynamic binding in which function call is resolved at runtime. This means that the code to be executed as a result of a function call is decided at runtime. Virtual functions are an example of dynamic binding.
In OOP, objects communicate with each other using messages. When objects communicate, information is passed back and forth between the objects. A message generally consists of the object name, method name and actual data that is to be sent to another object.
Advantages Of OOP
Let us discuss some of the advantages of OOP.
OOP allows the existing code to be reused through inheritance. We can easily acquire the existing functionality and improve on it without having to rewrite the code again. This results in less bloated code.
As we modularize the program in OOP, it’s easy to modify or troubleshoot the program if a problem occurs or new feature or enhancement is to be added. Modularization also helps in code clarity and makes it more readable.
OOP helps us with flexible programming using the polymorphism feature. As polymorphism takes many forms, we can have operators or functions that will work with many objects and thus save us from writing different functions for each object.
Maintaining code is easier as it is easy to add new classes, objects, etc without much restructuring or changes.
#5) Data and Information Hiding
OOP aids us in data hiding thereby keeping information safe from leaking. Only the data that is required for the smooth functioning of the program are exposed to the user by hiding intrinsic details.
OOP is the most important and flexible programming paradigm of modern programming. It is specifically useful in modeling real-world problems and thus is very popular.
We have discussed the various features of OOP in this tutorial. Going ahead we will discuss all these features in detail.
The next topic in this series “OOP with C++” will focus on classes and objects in detail.
=> Click Here For The Absolute C++ Training Series.