Back to: C++ Tutorials For Beginners and Professionals
Introduction to Object-Oriented Programming (OOPs) Concept:
In this article, I will give a brief Introduction to Object-Oriented Programming i.e. OOPs Concept.
What is OOPS?
Object-Oriented Programming is a strategy or a style of developing applications or developing software. It is a methodology. Other methodologies are also there like Structured Programming, Procedural Programming, Modular Programming. or Aspect-Oriented programming. But nowadays, one of the well-known and famous styles is Object Orientation.
Nowadays, all most all the latest programming languages support object orientation and this object orientation is more related to the designing of software and this deals with internal design but not external that is visible designing. So, it is nowhere related to the users of the software, it is related to the programmers who are working on developing software.
With the help of object orientation, the application development or the programming becomes more and more systematic and we can follow the procedures of engineering to develop software. Like other engineering, how any product is developed, in the same way, a software product is developed by adopting object orientation.
If we talk a little bit about other engineering like a civil engineer is constructing a building then first of all he/she will make a plan. They will make a design. While making a design, they may be having many options but they will select one of the designs and finalize it. Then once it is finalized as a blueprint on paper then they will start constructing. In the same way, an electronic engineer, when he is manufacturing any device, will come up with some design that is the circuit design of that device on paper. Once it is finalized then he will start manufacturing the device.
So, on paper we make blueprints. We are not using paper nowadays. We have many computer editing tools. So let us just imagine on paper we are working on. In the same way for developing software we have to do some paperwork, come up with a blueprint, and then start developing software. And a team of programmers can work over a single software and the work can be distributed easily and the work can be reused between the programmers very easily.
So, Object Orientation all depends on how you see the system or how you understand the system. Looking at the system and your perspective is more important. So, if you understand the system perfectly and if your perspective is very clear then you can develop a better system.
Not just Object-Oriented Programming, there are other programming styles also available like Procedural Programming, Modular Programming. or Structure Programming, or Aspect Programming but Object-Oriented Programming is a well known and successful pattern for software development, and most of the latest languages that are used for application development, they are object-oriented.
Object-Oriented vs Modular Programming
Now I will explain to you object orientation by comparing it with modular programming. The reason is that people who came to learn C++, already know the C language. The C programming language supports Modular or Procedural Programming. So based on that I can give you the idea of how object orientation is different from modular programming. Let us compare Object-Oriented vs Modular Programming through some examples.
So first we are taking an example of a bank. If you’re developing an application for a bank using modular programming then how do you see the system, how do you see the working of a bank, and what will be your design. That basically depends on how you understand it and how you see the system. So let us see how we are looking at the bank system using modular programming.
You can open an account, you can deposit some amount, you can withdraw some amount, you can check your account balance or you can apply for a loan, and so on. So, these are the things that you can do at the bank.
So ‘OpenAcc’, ‘Deposit’, ‘Withdraw’, ‘CheckBal’ and ‘Apply Loan’ are functions. All these are nothing but functions. You can do the specific operations by calling that specific function. So, if you’re developing software for a bank, it is nothing but a collection of functions. So, the bank application will be based on the functions and the user of your application will be utilizing these functions for performing his required task. So, you will develop software in the form of a set of functions in modular programming. Now for object orientation, we would take some different examples.
The government provides a lot of services like electric service, water supply service, education, transport, and even government can have banks and so on. These are departments of a government. Now, what you can do in this department as a user? You can apply for a new connection, or you can close your connection if you want if you have extra connections, or you can make a bill payment, then what these are? These are functions belonging to the Electric Department.
Now in the same way bank is also there, the same functions like account open, deposit, withdraw, check balance, apply for a loan, etc. are also there. These are functions belonging to Electric bank.
What do we call these? We call them objects. So, the complete system for the government or a complete software for a government is a collection of sets of objects. Now each object is having its relevant functions. So, a complete software is a collection of objects and objects containing functions as well as data related to those functions.
And in modular programming, the system was a collection of functions. So, if you compare them now, in modular programming we are looking at the very close level and in object-oriented programming, we are looking at a little far away level.
So, if you look at things closer like if you go to the bank then everything inside that is nothing but a set of functions. When initially computer science was introduced, computers were used for performing some simple or very small tasks. Then later on people started using them for larger and bigger tasks.
At that time, when C language or procedural language was there, everything was not computerized in the system. For any organization, at that time, everything was not computerized, rather they computerized some part of it, like the account part of the system or payroll part of the business. So, when the programmers were working on any particular department then they were looking at everything as functions, but now we are developing software at a larger scale for a big organization as a complete one single software then you can see all the things like a set of objects.
Back to the example, electric-related things are functions related to electricity and transport-related function are related to transport. So, we have increased our level of looking at the system because earlier we were solving small problems but now, we are solving bigger problems.
Then there was a need for some methodology that should support software development easily. So yes, object orientation is best suitable for large-size systems.
Why Object Orientation?
Let us talk about a manufacturing firm which manufactures some goods. Let us see automobile companies that manufacture cars or vehicles. If you look at that manufacturing farm then it may be working in the form of departments like one is an inventory department that maintains the stock of raw material and one is manufacturing that is the production work that they do and one department will be looking at sales and one department is looking at marketing and one is about payroll and one is for accounts and so on. So, there may be many departments.
Now if you are developing software only for payroll purposes or only for inventory purposes then you may look at the system just like the modular approach and in that you can find the functions like placing an order and checking the item in stock. These types of things you can have a set of functions in that, so you can develop the software only for the inventory system as a collection of functions but when you are developing a software for the entire organization then you have to see the things in the form of objects.
So, the inventory items are an object, an employee is an object, an account is an object, and a product manufacturer is an object, the machines that are used for production are an object. So, all these things are objects. Here, you need to see the things in the form of objects and define their data and the functions that they’re performing. We are looking at the system at a higher level. So, we can adopt object orientation.
So that’s it. This is the introduction to object orientation in comparison with modular programming.
Principles of Object-Oriented Programming (OOPs):
Now, will learn about the principles of object-oriented programming. These are four principles provided by object-oriented programming. They are as follows:
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
For a better understanding, please have a look at the below image.
As we were comparing software development or software engineering with other engineering disciplines, we want the software development should also be done just like other engineering. So, the object orientation supports a programmer to develop any application just like how other engineers develop their products like the electronic engineer is making some device or an automobile engineer is making some vehicle, so software engineers also want to follow their approach. So that approach gives these four principles.
When we come to software development that is programming. What are the main elements of programming? There are two main elements of programming that are data and operations on the data that are functions.
The functions perform operations on the data. There may be many functions and there may be a huge collection of data. So, when you develop software, the software is for performing operations on the data only. So, the programs are based on the data. Data is primary then the operations on the data are the set of functions on them.
If you take an example of an automobile car which will have metal or wires or cables and plastic. These are the things that are used for making a vehicle. If we talk about electronics then electronics circuits, PCBs, or boards are used for making an electronic device. Same way what is used for making software? We need data and the functions on the data. Now let us learn about the principles. First of all, abstraction.
Abstraction:
If you take the car example then all the mechanisms of the car will be hidden inside the cabin or bonnet and what is visible outside? Steering and the gearbox and pedals (for acceleration) and some other things are visible to the user. The user does not need to know about the working of the car.
Now in the same way, if we take the example of television, the complete circuit is inside the box, and what is visible outside? The buttons or the Remote-Control buttons for operating it. The viewer does not need to know about the working of the television. So, when you don’t know the internal details, this is nothing but abstraction.
In the same way, we want to achieve abstraction also in software. We don’t want to show internal detail. We don’t want to see the implementation of the function like how they are working but just we want the names of the function.
How it is done in object-oriented programming using C++?
Here we have written data and functions inside a class called ‘My’. The class will contain data and functions. So, what is visible here? Only functions. Who is the user of a class? Programmer only, not the user of the software. Suppose I wrote the class then the other programmer is should bother about just calling the function not working of the functions i.e. how the functions implemented internally.
We don’t want to know the internal details, just the function name is sufficient. That is the meaning of abstraction. We can group a set of functions together with the help of the class. So, this is encapsulation. So, a class gives two things that are abstraction and encapsulation.
Abstraction in what sense? Just we get the function name. We don’t know the workings of a function. When we are using it, we don’t know how they are working. Like the ‘printf’ function we have used in C language, thousands of times. You may have used the ‘printf’ function but ever wondered how it is working? How it is displayed on the screen? No, that is abstract for you.
So, without knowing how the printf is working, we have used it many times. That is Abstraction. Does it mean that you cannot write the function like printf by yourself? If you study you can write a function like printf by yourself. So, in the same way when some code has been written in a class for some purpose then we should enjoy using it on the program that includes the library and you should use the objects of the class and call the functions. We don’t need to worry about how they are working. Just use them. So, the class helps us achieve abstraction. Next is encapsulation. So, what is encapsulation? Let us look.
Encapsulation:
We already discussed that the television box is a packed box. Everything is inside that box. So, what is hiding inside? Data and what is visible outside? Functions. Data means the cables, ICs and everything that is there inside T.V is data and the buttons are visible to the user which are nothing but functions.
In the same way, when you visit a bank, the bank is an object. If you want to see all your account details and your cash and everything inside is hidden. All your records are hidden inside. Just you can call the function that is the Deposits or Withdraws. You can’t say that I am having one hundred crores in my account in this bank, I will directly enter into the locker rooms and take the cash by myself. No, you cannot do that, you have to follow a function.
In the same way, you cannot say that I am an electrical engineer, I know how television works. I’ll open the box and connect the cables and use it. You cannot do that. You should use the function. To make it safe from mishandling.
We hide the data and make the functions visible and we put the things together in one place. A class binds the data and the functions together that is encapsulation and along with this, in classes, we make data private. We want to avoid the mishandling of the data so we make it private and we make functions public.
You can see that on television, everything is hidden inside. The functions are coming out, the buttons are coming out. In a car everything is hidden inside only the dashboard is coming out. So, functions are visible. You can use those functions. So, we hide the data and show the functions and we also achieve data hiding along with encapsulation. So, data hiding comes as a part of encapsulation. Now let’s talk about inheritance and polymorphism.
Inheritance:
Inheritance and polymorphism are interrelated with each other. With the help of the car example, I will try to explain these two concepts. Suppose you have written a class for cars.
Then from that Car, we can have BMW i.e. BMW is a car. So, what is BMW? All you find in the car is also available in BMW. So, BMW is inheriting from Car. Somebody has to design a car with four wheels and seats and the driver seat, gearbox and steering, etc. And that Car design is to be followed by all the cars in the world. So, BMW is the car means it is inheriting everything from that Car.
In the same way, ‘Toyota’ is also a car and ‘Suzuki’ is also a car. So, all of these are cars. They are inheriting all the features of the Car plus they have some extra features. It is nothing but inheritance.
Like if you take a ‘Toyota’, they will launch a car then from that Car only they will launch a new model. So, by making some changes in one model or some advancement they will launch a new car. They will not redesign a car from the beginning again, they will launch a new car from an existing car only. This is inheritance.
We also want that inheritance to be followed in programming. Suppose we have written some classes then tomorrow I want another class in which I want all these features plus extra features, so we should be able to inherit or borrow all these features. We will understand inheritance in detail while doing some programming. Now the last one is polymorphism.
Polymorphism:
This OOP concept lets programmers use the same word to mean different things in different contexts. One form of polymorphism in C++ is method overloading. That’s when different meanings are implied by the code itself. The other form is method overriding. That’s when the different meanings are implied by the values of the supplied variables. For example, to convince the customer differently, to draw something, for example, shape, triangle, rectangle, etc.
In the next article, I am going to discuss Class and Objects in C++ with Examples. Here, in this article, I give a brief Introduction to Object-Oriented Programming i.e. OOPs Concept. I hope you enjoy this Introduction to Object-Oriented Programming i.e. OOPs Concept article. I would like to have your feedback. Please post your feedback, question, or comments about this article.
Guys,
Please give your valuable feedback. And also, give your suggestions about OOPs Concept Introduction. If you have any better real-time examples, you can also put them in the comment section. If you have any key points related to OOPs Concept, you can also share the same.
I just love the explanation of the every concepts
I felt explanation is bit boring and concepts are not clear.
Thank you for your efforts.
l like this tutorial. it was useful for me . thanks.
Deep Copy and Shallow Copy
I don’t know why, but I didn’t like this part of the course very much, and in general, you didn’t clearly explain the difference between shallow copy and deep copy. I wish you had some examples and some comparisons and that you could tell where we should use each one.