0% found this document useful (0 votes)
19 views32 pages

CH 2-OOP Concepts

Uploaded by

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

CH 2-OOP Concepts

Uploaded by

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

CH.

2 : - Object Oriented Programming


Concepts
• What is object-oriented programming?

• Object-oriented programming (OOP) is a computer programming model that organizes


software design around data, or objects, rather than functions and logic. An object can be
defined as a data field that has unique attributes and behavior.
• Object Oriented Programming (OOP) is playing an increasingly significant role in the
analysis, design and implementation of software systems. OOP languages provide the
programmer the ability to create class hierarchies, instantiate objects and send
messages between objects to process themselves.
• Object oriented means to organize software as a collection of discrete ,real-world objects
that incorporate both data structure and behavior.

• OOP focuses on the objects that developers want to manipulate rather than the logic
required to manipulate them. This approach to programming is well-suited for programs
that are large, complex and actively updated or maintained. This includes programs for
manufacturing and design, as well as mobile applications; for example, OOP can be used
for manufacturing system simulation software.
• OOMD :-

• Intention of object oriented modeling and design is to learn how to apply object -
oriented concepts to all the stages of the software development life cycle. Object-
oriented modeling and design is a way of thinking about problems using models
organized around real world concepts. The fundamental construct is the object,
which combines both data structure and behaviour in a single entity.

• Object Oriented models are useful for understanding problems, communicating


with application experts, modeling enterprises, preparing documentation and
designing programs and databases.

• Object Oriented Programming is a new way of organizing code and data that
promises increased control over the complexity of software development process.

• The prime purpose of C++ programming was to add object orientation to


the C programming language, which is in itself one of the most powerful
programming language.
Need of OOP :-
 The use of structured programming made more complex programs, less complex and
easier to understand. But even with structured programming, when a program reaches
a certain size, its complexity exceeds that which a programmer can manage.
 To deal with this approach, a new way to program, was invented “Object Oriented
Programming”. This method of programming reduces the program complexity by
incorporated rating features like Inheritance, Encapsulation and Polymorphism.
 OOP is a way of programming help the programmer to comprehend and manage large
and complex programs.
 Object Oriented Programming models real-world objects with software counterparts. It
takes advantages of class relationships where objects of certain class have the same
characteristics.
 OOP allows us to decompose a problem into a number of entities called objects.
 OOP takes advantages of inheritance and multiple inheritance relationship where
newly created classes of objects are derived by absorbing characteristics of existing
classes and adding unique characteristics of their own.
Object Oriented Concepts :- A class is defined in C++ using keyword class
Classes are user-defined data types that act as the followed by the name of class. The body of
blueprint for individual objects, attributes and class is defined inside the curly brackets and
methods. terminated by a semicolon at the end.
A C++ class is like a blueprint for an object.
Objects are instances of a class created with
For Example: Consider the Class of Cars. There
specifically defined data. Objects can correspond
may be many cars with different names and
to real-world objects or an abstract entity. When
brand but all of them will share some common
class is defined initially, the description is the only
properties like all of them will have 4 wheels,
object that is defined.
Speed Limit, Mileage range etc. So here, Car is
Methods are functions that are defined inside a
the class and wheels, speed limits, mileage are
class that describe the behaviors of an object.
their properties.
Each method contained in class definitions starts
with a reference to an instance object. Additionally,
A Class is a user defined data-type which has
the subroutines contained in an object are called
data members and member functions.
instance methods. Programmers use methods for
Data members are the data variables and
reusability or keeping functionality encapsulated
member functions are the functions used to
inside one object at a time.
manipulate these variables and together these
Attributes are defined in the class template and
data members and member functions defines
represent the state of an object. Objects will have
the properties and behavior of the objects in a
data stored in the attributes field. Class attributes
Class.
belong to the class itself.
In the above example of class Car, the data
member will be speed limit, mileage etc and
member functions can be apply brakes,
Object Oriented Concepts :-

• Classes :-
• Class is a user defined data type with data elements
and functions .
• A class is a way that binds data and functions that
operate on the data together in a single unit. Like other
user defined data types, it also needs to be defined
before using its objects in the program.
• A class definition specifies a new data type that can be
treated as a built in data type.
• The data members describe the state of the object. The
operations defines the behavior of the object.
• Class is used to define abstractions.
Declaration of Class :-
 The keyword ‘class’ is used to declare a class in C++. The declaration of a class
always ends with the semicolon (;). All data members and member functions are
declared within the braces { and } which are called the body of a class.
 Syntax for declaration of a class :
class class_name
{
//body of a class
};
 The class specifies type and scope of its members. The keyword class indicates that
the class name which follows in an abstract data type. The body of a class is
enclosed within the curly braces followed by a semicolon(;).
 The body of a class contains declaration of variables and functions, collectively known
as data members and functions are known as member functions.
 This members are grouped under three section : private,protected and public which
defines the visibility of members.
 Consider simple class example :
Class student Class : student
{
Data Members:
int rollno;
rollno
char name[20];
name
public :
void getdata() Functions:
{ getdata()
rollno = 10; display()
name = “Ramesh”;
} Fig. : Class
void display()
{
cout<<“Roll no :”<<rollno;
cout<<“Name :”<<name;
}
};//end of class
• The class called student contains two data members and two member functions.
• The data members are private by default whereas both the member functions
are public.
• The general notation of representation of class student is shown in figure.
class MyClass { // The class
class MyClass { // The class public: // Access specifier
public: // Access specifier int myNum; // Attribute (int variable)
int myNum; // Attribute (int variable) string myString; // Attribute (string variable)
string myString; // Attribute (string variable) };
};
int main() {
MyClass myObj; // Create an object of MyClass

// Access attributes and set values


myObj.myNum = 15;
myObj.myString = "Some text";

// Print attribute values


cout << myObj.myNum << "\n";
cout << myObj.myString;
return 0;
}
Objects:-
 An object is an instance of a class. A class provides blueprints for the object. An object
is variable of type class.
 Objects are discrete ,distinguishable entities. Objects are basic run-time entities in an
OOP environment. Any real word object can be treated as an object in the OOP such
as paragraph in a document or a window on a workstation or the white queen in a
chess game are some examples of objects.
 Objects can be concrete such as a file in a file system or conceptual such as
scheduling policy in a multiprocessing operating system. Each object has its own
inherent identity.
 In a program, objects interact by sending messages to one another. Each object
contains data and code to manipulate the data.
 Objects can interact without having to know details of each other’s data or code. It is
sufficient to know the type of message accepted and the type of response returned by
the objects.
 Objects in a class have the same attributes .Objects derive their individuality from
differences in their attribute values and relationships to other objects. For Example :
table, chair, desk, are members of class furniture.
 In our day today life, we come across a number of objects.
 For example, PC, television, radio, pen, pencil, stack, queue, program file, table, car,
telephone, flower, mango etc.
 An object will have the following two characteristics :
i. State or attribute.
ii. Behavior or operation.
 The state or attribute tells the built–in characteristics of an object. They remain the
same till disturbed or modified. For example, a flower has attributes such as, shape,
color, smell and so on .
 The behavior or operation of an object tells about its action. It can be explicitly defined.
For example, flower blossoms, bud etc.
 When we create a object of a class:
o Memory is allocated.
o Constructor is called implicitly.
o Memory gets initialized.
 The process of creating object is called class instantiation.
 We can create object as:
Class_name objectname;
OR
Class_name object1, object2,……….objectn;
 For example :-
student s;
student s1, s2; //more than one object of class student
 The another way is, we can create object by placing their name immediately after the
class declaration.
 Class student
{
……………….
……………….
}s1, s2;
Objects :-
In C++, Object is a real world entity, for example, chair, car, #include <iostream>
pen, mobile, laptop etc. using namespace std;
class Student {
In other words, object is an entity that has state and public:
behavior. Here, state means data and behavior means int id;//data member (also instance variable)
functionality. string name;//data member(also instance variable)
};
Object is a runtime entity, it is created at runtime. int main() {
Object is an instance of a class. All the members of the class Student s1; //creating an object of Student
can be accessed through object. s1.id = 201;
Student s1; //creating an object of Student s1.name = "Sonoo Jaiswal";
cout<<s1.id<<endl;
cout<<s1.name<<endl;
return 0;
}
 Data Abstraction :-
 Abstraction is the selective examination of certain aspects of a problem.
 The goal of abstraction is to isolate those aspects that are important for some purpose
and suppress those aspects that are unimportant.
 In other words, we can say that abstraction is the act of representing essential features
without including the background details or explanations.
 Data abstraction is the process of defining a data type, often called Abstract Data Type
(ADT), together with the principle of data hiding.
 The definition of an ADT involves specifying the internal representation of the ADTs data
as well as the functions to be used by others to manipulate the ADT.
 In OOP, classes use the concept of abstraction and are defined as a list of abstract
attributes such as size, weight and cost and functions to operate these attributes.
 Classes are also known as ADTs.
What is data abstraction?
Data abstraction is the reduction of a particular body of
data to a simplified representation of the whole.
Abstraction, in general, is the process of removing
characteristics from something to reduce it to a set of
essential elements.
Benefits of Data Abstraction
Data abstraction provides two important advantages −
Class internals are protected from inadvertent user-level
errors, which might corrupt the state of the object.
The class implementation may evolve over time in
response to changing requirements or bug reports
without requiring change in user-level code.

Inheritance
In C++, inheritance is a process in which one object
acquires all the properties and behaviors of its parent
object automatically. In such way, you can reuse, extend
or modify the attributes and behaviors which are defined
in other class.
In C++, the class which inherits the members of another
class is called derived class and the class whose
members are inherited is called base class. The derived
class is the specialized class for the base class.
 In inheritance process, we can create new classes known as derived or child or sub class
from the existing class known as base or parent class.
 Inheritance is a process by which object of one class (derived class) can acquire the
properties of objects of another class(base class).
 The concept of inheritance provides the idea of reusability. This means that we can add
additional features to an existing class without modifying it. This is made possible by
deriving a new class from the existing one. The new class will have the combined
features of both the classes.
 The new class will have the combined features of base class. The basic advantage of
using inheritance is to increase reliability and decrease in maintenance cost. Because
the base and derived classes can be shared by many users.
 For example, plant is base, through which two types like flowering and non-flowering is
derived.
Example of Inheritance :
Plant

Flowering Non-Flowering

One seed Many seeds

Custard
Mango Neem Papaya
apple
Polymorphism :-
 The term "Polymorphism" is the combination of "poly" + "morphs"
which means many forms. It is a greek word.
 Let's consider a real-life example of polymorphism.
 A lady behaves like a teacher in a classroom, mother or daughter in a home and
customer in a market. Here, a single person is behaving differently according to the
situations.
 The types of polymorphism are shown in Fig:
 In Polymorphism “poly” means many and “morphism” means form, which means
many forms of one things.
 In other words, “Polymorphism means one thing different or many forms” i.e. the
ability to take more than one form.
 Polymorphism plays an important role in allowing objects having different internal
structures to share the same external interfaces.
 In OOP, polymorphism refers to the fact that a single operation can have different
behavior in different objects. In other words, different objects react differently to the
same message.
 For example, consider the operation of addition. For two numbers, the addition should
generate the sum. The operation of addition is expressed by a single operator +. You
can use the expression x + y to denote the sum of x and y for many different types of
x and y integers, floating point numbers and complex numbers and even the
concatenation of two strings.
• Compile time polymorphism is also called as Early Binding
or Static Binding.
• Run time polymorphism is also called as Late Binding or
Dynamic Binding.
Data Binding : -
Binding refers to the process of converting identifiers (such
as variable and performance names) into addresses.
Binding is done for each variable and functions. For
functions, it means that matching the call with the right
function definition by the compiler. It takes place either at
compile time or at runtime.
• There are two types of Binding :-
1. Static Binding (Early Binding)
2. Dynamic Binding(Late Binding)

Early Binding (compile-time time polymorphism) As


the name indicates, compiler (or linker) directly associate
an address to the function call. It replaces the call with a
machine language instruction that tells the mainframe to
leap to the address of the function.
Late Binding : (Run time polymorphism) In this, the
compiler adds code that identifies the kind of object at
BASIS FOR
STATIC BINDING DYNAMIC BINDING
COMPARISON
Event Occurrence Events occur at compile Events occur at run time are
time are "Static Binding". "Dynamic Binding".

Information All information needed to All information need to call a


call a function is known at function come to know at run
compile time. time.

Private, static and final Other than private, static and


methods show static final methods show dynamic
binding. Because, they binding. Because, they can be
cannot be overridden. overridden.

Actual object is not used for Actual object is used for


binding. binding.
Advantage Efficiency. Flexibility.
Time Fast execution. Slow execution.
Alternate name Early Binding. Late Binding.
Example Overloaded function call, Virtual function in C++,
overloaded operators. overridden methods in java.
Encapsulation:-

• The wrapping up of data and functions into a single


unit i.e. class is called as Encapsulation.
• In other words, “the binding of data and function into a
single unit(class) is called as encapsulation.”
• The data is not accessible to the outside world and
only those functions which are wrapped in the class
can access it.
• These functions which are wrapped in the class provide
the interface between the objects data and the
program.
• Encapsulation is a mechanism that keeps the data and
code safe from external interference and misuse. This
insulation of the data from direct access by the
program is called data hiding which is also known as
• Message Passing :-
 An OOP consists of a set of objects . These objects communicate with
each other. The process of programming in an object oriented language,
therefore, involves the following three basic steps :-
 Step1 : Create class which define object and their behavior.
 Step2 : Create object from class definitions.
 Step3 : Establish communication between objects.
 Objects communicate with one another by sending and receiving
information. This is same as people pass messages to one another. The
concept of message passing makes it easier to talk about building
systems that directly model or simulate their real-world counterparts.
 The message for an object is a request for execution of a procedure and
therefore will invoke a function , (procedure) in the receiving object that
generates the desired result. Message Passing involves specifying the
name of the object, the name of the function, (message) and the
information to be sent.
 Example :-
 employee . salary (name);

Object Message Information

 Objects have life cycle. They can be created and destroyed.


Communication with an object is feasible as along as it is alive. The
another way of representing message passing is shown in fig. :-

Message
emp Salary()

Receive
information
Message passing in C++ is the communication
between two or more objects using a logical entity
called a message.

• message passing is a technique for invoking


behaviour (i.e., running a program) on a
computer.
• The invoking of a program sends a message to a
process (which may be an actor or object) and
relies on the process and the supporting
infrastructure to select and invoke the actual
code to run.
• Message passing differs from conventional
programming where a process, subroutine, or
function is directly invoked by name.
• All communication between objects is done via
message is called message passing, as people
exchange information similarly the sending and
receiving of information by the object is said to
be message passing, to make possible message
passing the following things have to be followed
to be done :

• We have to create classes that define objects


• Advantages and Applications of OOP:-
 Advantages of OOP :-
1. Data abstraction concept consists of focusing on the essential,
inherent aspects of an entity and ignoring its accidental
properties. The use of abstraction preserves the freedom to make
decisions as long as possible.
2. By the use of property inheritance, we can eliminate redundant
code and extend the use of existing class.
3. The property of data encapsulation helps to programmer to build
secure programs that cannot be invaded by code in other parts of
the program.
4. We can have multiple instances of an object to co-exist without
any interference.
5. We can map objects in the problem domain to the object in the
program.
6. We can modularize the program by dividing it into classes and
objects.
8. Interface descriptions with external systems become much
simpler by the property of message passing
i.e. the communication between objects.
9. Object Oriented Programming supports reusability.
Reusable software reduces design, coding and testing cost by

amortizing effort over several design. Reducing the amount of

code also simplifies understanding.


10. The OOP, complex software’s can be easily managed.
11. OOP saves development time and gives higher productivity.
12. In OOP, small projects(programs) can be easily upgraded to
large one.
13. Message passing in OOP provides the interface with external
systems.
1. Modularity for easier troubleshooting
2. Reuse of code through inheritance
3. Flexibility through polymorphism
4. Effective problem solving

Applications

1. Client-Server Systems
2. Object-Oriented Databases
3. Object-Oriented Databases
4. Real-Time System Design
5. Simulation and Modeling System
6. Hypertext and Hypermedia
7. Neural Networking and Parallel
Programming
8. Office Automation Systems
9. CIM/CAD/CAM Systems
10. AI Expert Systems
 Applications of OOP :-
 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 areas for application of OOP includes :-
1. Real-time systems, for example, oil exploration plant.
2. CAM/CAD systems.
3. Object Oriented databases.
4. Artificial Intelligence (AI) and expert systems, for example, Disease
Knowledge base.
5. Hypertext, hypermedia and expertext, for example, web page
designing.
6. Decision support and office automationnsystems.
7. Neural networks and Parallel programming .
8. Simulation and Modeling.
THANK YOU

You might also like