0% found this document useful (0 votes)
12 views

OOP_Chapter1

Oop

Uploaded by

Satabdi Saikia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

OOP_Chapter1

Oop

Uploaded by

Satabdi Saikia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

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 1980s. 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.
Object-oriented programming – As the name
suggests uses objects in programming. Object-oriented
programming aims to implement real-world entities like
inheritance, hiding, polymorphism, etc. in programming.
The main aim of OOP is to bind together the data and the
functions that operate on them so that no other part of
the code can access this data except that function.
There are some basic concepts that act as the building
blocks of OOPs i.e.
– Class
– Objects
– Encapsulation
– Abstraction
– Polymorphism
– Inheritance
– Dynamic Binding
– Message Passing
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.
Object-oriented approach
– The main objective of object-oriented approach is to reduce some of the imperfection
encounter in the procedural approach.
– OOP treats data as a critical element in the program development and does not allow
it to flow freely around the system.
– It ties data more closely to the functions that operate on it and protects it from
unintentional modification by other functions.
– The combination of data and methods make up an object.
– The data of an object can be accessed only by the method associated with the object.
– However, method of one object can access the method of other objects.
• Some characteristics of object-oriented paradigm are:
• Emphasis is on data rather than procedure.
• Data structures are designed such that they characterize the objects.
• Methods that operate on the data of an object are tied together in the data
structure.
• Data is hidden and cannot be accessed by external functions.
• Object may communicate with each other through methods.
• New data and methods can be easily added whenever necessary.
• Follow bottom-up approach in program design.
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.
Basic concepts of OOP
• Encapsulation:
– Encapsulation is the mechanism that binds
together code and data it manipulates and
keeps both safe from outside interference and
misuse. In normal terms, Encapsulation is
defined as wrapping up data and information
under a single unit.
– A real time example would be , different departments
of a company(finance, sales, support etc.). Each
department handling its own related data. There may
be situations, when sales may require to access some
data of finance. Sales may not be able to access these
data directly. Sales get permission and access from
head of finance. Finance provide required data in a
particular fashion. This is encapsulation.
Example of encapsulation
#include <iostream>
using namespace std; • We can not access any function
class temp{ from the class directly. We need an
int a;
object to access that function that
int b;
public: is using the member variables of
int solve(int input){ that class.
a=input; • The function which we are making
b=a/2; inside the class must use only
return b; member variables, only then it is
} called encapsulation.
}; • If we don’t make a function inside
the class which is using the
int main() {
int n; member variable of the class then
cin>>n; we don’t call it encapsulation.
temp half; • Increase in the security of data
int ans=half.solve(n); • It helps to control the modification
cout<<ans<<endl; 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 Inheritance
Modes of Inheritance: There are 3 modes
of inheritance.
Public Mode: If we derive a subclass from
a public base class. Then the public
member of the base class will become
public in the derived class and protected
members of the base class will become
protected in the derived class.
Protected Mode: If we derive a subclass
from a Protected base class. Then both
public members and protected members
of the base class will become protected in
the derived class.
Private Mode: If we derive a subclass
from a Private base class. Then both
public members and protected members
of the base class will become Private in
the derived class.
Basic concepts of OOP
• Polymorphism:
– Polymorphism comes from Greek word “Poly” => “many”
and “morphism” => “form”.
– Polymorphism in C++ means that the same entity behaves
differently in different scenarios.

– A person at the same time is a mother, a housewife, and an employee.


So the same person possesses different behavior in different
situations. This is called polymorphism.

Example: Suppose we have to write a function to add some integers,


sometimes there are 2 integers, and sometimes there are 3 integers. We
can write the Addition Method with the same name having different
parameters, the concerned method will be called according to
parameters.
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.
AppIications of OOP
• The most popular application of object-oriented programming, up to now,
has been in the area of user interface design such as windows.
• Real business systems are often much more complex and contain many
more objects with complicated attributes and methods. OOP is useful in
these types of applications because it can simplify a complex problem. The
promising areas for application of OOP include:

1. Real-time systems
2. Simulation and modeling
3. Object-oriented databases
4. Hypertext, hypermedia
5. AI and expert systems
6. Neural networks and parallel programming
7. Decision support and office automation systems
8. CIM/CAD system
Thank you !

You might also like