Download Object oriented programing using c++ and more Study notes Object Oriented Programming in PDF only on Docsity!
OOP using C++
Chapter 1
Introduction
- With the advancement of languages such as C, structured
programming become very popular and was the paradigm of
the 1980 s. However, as the programs grew larger, even the
structured approach failed to show the desired results in
terms of bug-free, easy-to-maintain and reusable programs.
- Object-oriented programming (OOP) is an approach to
program organization and development, which attempts to
eliminate some of the pitfalls of conventional programming
methods by incorporating the best of structured programming
features with several new concepts.
What is object-oriented programming?
- Object-oriented programming is a programming model that
organizes software design around ‘objects’ and ‘classes’, which
contain both data and methods.
Class : The building block of C++ that leads to Object-Oriented programming is a Class. It is a user- defined data type, which holds its own data members and member functions, which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object. Eg : A Car can be a class defining a model by concept. Car can have different properties like color, cost, max speed etc. These are called data members. Object: Object is an instance of a class. It can be considered an entity with some characteristic and behavior. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated. Eg. Maruti can be consider an object of car.
Object-oriented programming has several advantages over
procedural programming:
i. OOP provides data hiding whereas in procedural
programming, global data can be accessed from anywhere
ii. OOP provides ability to simulate real-world event much
more effectively
iii. OOP helps to make the code easier to maintain, modify
and debug
iv. OOP makes it possible to create full reusable applications
with less code and shorter development time.
Basic concepts of OOP
• Abstraction :
- Abstraction refers to the act of representing essential features without including the back ground details or explanations. Abstraction means displaying only essential information and hiding the details. Data abstraction refers to providing only essential information about the data to the outside world, hiding the background details or implementation. Consider a real-life example of a man driving a car. The man only knows that pressing the accelerator will increase the speed of the car or applying brakes will stop the car but he does not know how on pressing the accelerator the speed is actually increasing, he does not know about the inner mechanism of the car or the implementation of an accelerator, brakes, etc. in the car. This is what abstraction is.
Example of encapsulation
#include using namespace std; class temp{ int a; int b; public: int solve(int input){ a=input; b=a/2; return b; } }; int main() { int n; cin>>n; temp half; int ans=half.solve(n); cout<<ans<<endl; }
- We can not access any function from the class directly. We need an object to access that function that is using the member variables of that class.
- The function which we are making inside the class must use only member variables, only then it is called encapsulation.
- If we don’t make a function inside the class which is using the member variable of the class then we don’t call it encapsulation.
- Increase in the security of data
- It helps to control the modification of our data members
Basic concepts of OOP
• Inheritance :
– Inheritance is the process by which objects of one
class acquires the properties of another class.
– This is important because it supports the concept
of hierarchical classification.
Example of Polymorphism
Benefits of OOP
- Through inheritance, we can eliminate redundant code and extend the use of existing classes.
- We can build programs from the standard working modules that communicate with one another, rather than having to start writing the code from scratch. This leads to saving of development time and higher productivity.
- The principle of data hiding helps the programmer to build secure programs that cannot be affected by code in other parts of the program.
- It is possible to have multiple objects to coexist without any interference.
- It is easy to partition the work in a project based on objects.
- Object-oriented system can be easily upgraded from small to large systems.
- Message passing techniques for communication between object make the interface descriptions with external systems much simpler
- Software complexity can be easily managed.
Thank you!