SlideShare una empresa de Scribd logo
1 de 10
Why Do We Need Object-
Oriented Programming?
Muhammad Hammad Waseem
m.hammad.wasim@gmail.com
Why Do We Need Object-Oriented
Programming?
• Object-Oriented Programming was developed because limitations
were discovered in earlier approaches to programming.
• To appreciate what OOP does, we need to understand what these
limitations are and how they arose from traditional programming
languages.
Procedural Languages
• C, Pascal, FORTRAN, and similar languages are procedural languages.
• That is, each statement in the language tells the computer to do something:
• Get some input, add these numbers, divide by 6, display that output.
• A program in a procedural language is a list of instructions.
• For very small programs, no other organizing principle (often called a
paradigm) is needed.
• The programmer creates the list of instructions, and the computer carries
them out.
Division into Functions
• When programs become larger, a single list of instructions becomes
unwieldy.
• Few programmers can comprehend a program of more than a few
hundred statements unless it is broken down into smaller units.
• For this reason the function was adopted as a way to make programs
more comprehensible to their human creators.
• A procedural program is divided into functions, and (ideally, at least)
each function has a clearly defined purpose and a clearly defined
interface to the other functions in the program.
Division into Functions
• The idea of breaking a program into functions can be further
extended by grouping a number of functions together into a larger
entity called a module (which is often a file).
• Dividing a program into functions and modules is one of the
cornerstones of structured programming, the somewhat loosely
defined discipline that influenced programming organization for
several decades before the advent of Object-Oriented Programming.
Problems with Structured Programming
• As programs grow ever larger and more complex, even the structured
programming approach begins to show signs of strain.
• Analyzing the reasons for failures reveals that there are weaknesses in
the procedural paradigm itself.
• No matter how well the structured programming approach is implemented,
large programs become excessively complex.
• What are the reasons for these problems with procedural languages?
• There are two related problems.
• First, functions have unrestricted access to global data.
• Second, unrelated functions and data, the basis of the procedural paradigm,
provide a poor model of the real world.
Unrestricted Access
• In a procedural program, one written in C for example, there are two kinds
of data.
• Local data is hidden inside a function, and is used exclusively by the function.
• However, when two or more functions must access the same data—and this is true
of the most important data in a program—then the data must be made global
• In a large program, there are many functions and many global data items.
• The problem with the procedural paradigm is that this leads to an even
larger number of potential connections between functions and data
• This large number of connections causes problems in several ways.
• First, it makes a program’s structure difficult to conceptualize.
• Second, it makes the program difficult to modify.
• A change made in a global data item may result in rewriting all the functions that access that
item.
Real-World Modeling
• The more important problem with the procedural paradigm is that its arrangement of separate
data and functions does a poor job of modeling things in the real world.
• In the physical world we deal with objects such as people and cars. Such objects aren’t like data
and they aren’t like functions.
• Complex real-world objects have both attributes and behavior.
• Attributes: (sometimes called characteristics) are, for people, eye color and job titles; and, for
cars, horsepower and number of doors.
• As it turns out, attributes in the real world are equivalent to data in a program:
• they have a certain specific values, such as blue (for eye color) or four (for the number of doors).
• Behavior: Behavior is something a real-world object does in response to some stimulus. Behavior
is like a function: you call a function to do something.
• If you ask your boss for a raise, she will generally say yes or no.
• If you apply the brakes in a car, it will generally stop.
• Saying something and stopping.
• So neither data nor functions, by themselves, model real world objects effectively.
The Object-Oriented Approach
• The fundamental idea behind object-oriented languages is to combine into a single unit
both data and the functions that operate on that data. Such a unit is called an object.
• An object’s functions, called member functions in C++, typically provide the only way to
access its data.
• If you want to read a data item in an object, you call a member function in the object.
• It will access the data and return the value to you.
• You can’t access the data directly. The data is hidden, so it is safe from accidental alteration.
• Data and its functions are said to be encapsulated into a single entity.
• Data encapsulation and data hiding are key terms in the description of object-oriented
languages.
• If you want to modify the data in an object, you know exactly what functions interact
with it: the member functions in the object. No other functions can access the data. This
simplifies writing, debugging, and maintaining the program.
The Object-Oriented Approach
• A C++ program typically consists of a number of objects,
• which communicate with each other by calling one another’s member
functions.
• The member functions in C++ are called methods.
• Also, data items are referred to as attributes or instance variables.
• Calling an object’s member function is referred to as sending a
message to the object.
• These terms are not official C++ terminology, but they are used with
increasing frequency, especially in object-oriented design.

Más contenido relacionado

La actualidad más candente

1 unit (oops)
1 unit (oops)1 unit (oops)
1 unit (oops)
Jay Patel
 
Issues in knowledge representation
Issues in knowledge representationIssues in knowledge representation
Issues in knowledge representation
Sravanthi Emani
 

La actualidad más candente (20)

Object Oriented programming - Introduction
Object Oriented programming - IntroductionObject Oriented programming - Introduction
Object Oriented programming - Introduction
 
Composition in OOP
Composition in OOPComposition in OOP
Composition in OOP
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
Object-oriented concepts
Object-oriented conceptsObject-oriented concepts
Object-oriented concepts
 
object oriented programing lecture 1
object oriented programing lecture 1object oriented programing lecture 1
object oriented programing lecture 1
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programming
 
Object Oriented Programming Lecture Notes
Object Oriented Programming Lecture NotesObject Oriented Programming Lecture Notes
Object Oriented Programming Lecture Notes
 
OOPS Characteristics
OOPS CharacteristicsOOPS Characteristics
OOPS Characteristics
 
1669609053088_oops_final.pptx
1669609053088_oops_final.pptx1669609053088_oops_final.pptx
1669609053088_oops_final.pptx
 
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
Introduction to Object-Oriented Programming & Design Principles (TCF 2014)
 
Oops
OopsOops
Oops
 
09 grasp
09 grasp09 grasp
09 grasp
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
1 unit (oops)
1 unit (oops)1 unit (oops)
1 unit (oops)
 
SEMINAR
SEMINARSEMINAR
SEMINAR
 
Issues in knowledge representation
Issues in knowledge representationIssues in knowledge representation
Issues in knowledge representation
 
Object oriented programming concepts
Object oriented programming conceptsObject oriented programming concepts
Object oriented programming concepts
 
Class and object
Class and objectClass and object
Class and object
 

Destacado

Oop lec 2(introduction to object oriented technology)
Oop lec 2(introduction to object oriented technology)Oop lec 2(introduction to object oriented technology)
Oop lec 2(introduction to object oriented technology)
Asfand Hassan
 

Destacado (20)

Need of object oriented programming
Need of object oriented programmingNeed of object oriented programming
Need of object oriented programming
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
 
Object-Oriented Programming Concepts
Object-Oriented Programming ConceptsObject-Oriented Programming Concepts
Object-Oriented Programming Concepts
 
Object oriented programming (oop) cs304 power point slides lecture 01
Object oriented programming (oop)   cs304 power point slides lecture 01Object oriented programming (oop)   cs304 power point slides lecture 01
Object oriented programming (oop) cs304 power point slides lecture 01
 
Oop lec 2(introduction to object oriented technology)
Oop lec 2(introduction to object oriented technology)Oop lec 2(introduction to object oriented technology)
Oop lec 2(introduction to object oriented technology)
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP
 
[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects[OOP - Lec 06] Classes and Objects
[OOP - Lec 06] Classes and Objects
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
 
[OOP - Lec 03] Programming Paradigms
[OOP - Lec 03] Programming Paradigms[OOP - Lec 03] Programming Paradigms
[OOP - Lec 03] Programming Paradigms
 
[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing
 
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 
[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 08] Encapsulation (Information Hiding)
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 
[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Presentation on Core java
Presentation on Core javaPresentation on Core java
Presentation on Core java
 
Core java slides
Core java slidesCore java slides
Core java slides
 

Similar a [OOP - Lec 02] Why do we need OOP

Introduction to object oriented language
Introduction to object oriented languageIntroduction to object oriented language
Introduction to object oriented language
farhan amjad
 

Similar a [OOP - Lec 02] Why do we need OOP (20)

Chapter 1
Chapter 1Chapter 1
Chapter 1
 
OOP-1.pptx
OOP-1.pptxOOP-1.pptx
OOP-1.pptx
 
C++ notes.pdf
C++ notes.pdfC++ notes.pdf
C++ notes.pdf
 
Unit 2.pptx
Unit 2.pptxUnit 2.pptx
Unit 2.pptx
 
Unit 2.pptx
Unit 2.pptxUnit 2.pptx
Unit 2.pptx
 
SE-IT JAVA LAB OOP CONCEPT
SE-IT JAVA LAB OOP CONCEPTSE-IT JAVA LAB OOP CONCEPT
SE-IT JAVA LAB OOP CONCEPT
 
C++ chapter 1
C++ chapter 1C++ chapter 1
C++ chapter 1
 
Presentation c
Presentation cPresentation c
Presentation c
 
Object oriented programming 1 introduction to oop
Object oriented programming 1 introduction to oopObject oriented programming 1 introduction to oop
Object oriented programming 1 introduction to oop
 
Programming using C++ - slides.pptx
Programming using C++ - slides.pptxProgramming using C++ - slides.pptx
Programming using C++ - slides.pptx
 
Introduction to object oriented language
Introduction to object oriented languageIntroduction to object oriented language
Introduction to object oriented language
 
CPP16 - Object Design
CPP16 - Object DesignCPP16 - Object Design
CPP16 - Object Design
 
1. OBJECT ORIENTED PROGRAMMING USING JAVA - OOps Concepts.ppt
1. OBJECT ORIENTED PROGRAMMING USING JAVA - OOps Concepts.ppt1. OBJECT ORIENTED PROGRAMMING USING JAVA - OOps Concepts.ppt
1. OBJECT ORIENTED PROGRAMMING USING JAVA - OOps Concepts.ppt
 
Lecture 1.pptx
Lecture 1.pptxLecture 1.pptx
Lecture 1.pptx
 
OOPS_Unit_1
OOPS_Unit_1OOPS_Unit_1
OOPS_Unit_1
 
Unit 1 introduction to c++.pptx
Unit 1 introduction to c++.pptxUnit 1 introduction to c++.pptx
Unit 1 introduction to c++.pptx
 
chapter-6-oops.pdf
chapter-6-oops.pdfchapter-6-oops.pdf
chapter-6-oops.pdf
 
Abhiram
AbhiramAbhiram
Abhiram
 
Oop.pptx
Oop.pptxOop.pptx
Oop.pptx
 
Oop basic overview
Oop basic overviewOop basic overview
Oop basic overview
 

Más de Muhammad Hammad Waseem

Más de Muhammad Hammad Waseem (18)

[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
 
[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion
 
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
 
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
 
[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++
 
[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)
 
[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 07] Comments in C/C++[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 07] Comments in C/C++
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
 
[ITP - Lecture 05] Datatypes
[ITP - Lecture 05] Datatypes[ITP - Lecture 05] Datatypes
[ITP - Lecture 05] Datatypes
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++
 
[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 03] Introduction to C/C++[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 03] Introduction to C/C++
 
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
 
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 

Último

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Último (20)

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 

[OOP - Lec 02] Why do we need OOP

  • 1. Why Do We Need Object- Oriented Programming? Muhammad Hammad Waseem m.hammad.wasim@gmail.com
  • 2. Why Do We Need Object-Oriented Programming? • Object-Oriented Programming was developed because limitations were discovered in earlier approaches to programming. • To appreciate what OOP does, we need to understand what these limitations are and how they arose from traditional programming languages.
  • 3. Procedural Languages • C, Pascal, FORTRAN, and similar languages are procedural languages. • That is, each statement in the language tells the computer to do something: • Get some input, add these numbers, divide by 6, display that output. • A program in a procedural language is a list of instructions. • For very small programs, no other organizing principle (often called a paradigm) is needed. • The programmer creates the list of instructions, and the computer carries them out.
  • 4. Division into Functions • When programs become larger, a single list of instructions becomes unwieldy. • Few programmers can comprehend a program of more than a few hundred statements unless it is broken down into smaller units. • For this reason the function was adopted as a way to make programs more comprehensible to their human creators. • A procedural program is divided into functions, and (ideally, at least) each function has a clearly defined purpose and a clearly defined interface to the other functions in the program.
  • 5. Division into Functions • The idea of breaking a program into functions can be further extended by grouping a number of functions together into a larger entity called a module (which is often a file). • Dividing a program into functions and modules is one of the cornerstones of structured programming, the somewhat loosely defined discipline that influenced programming organization for several decades before the advent of Object-Oriented Programming.
  • 6. Problems with Structured Programming • As programs grow ever larger and more complex, even the structured programming approach begins to show signs of strain. • Analyzing the reasons for failures reveals that there are weaknesses in the procedural paradigm itself. • No matter how well the structured programming approach is implemented, large programs become excessively complex. • What are the reasons for these problems with procedural languages? • There are two related problems. • First, functions have unrestricted access to global data. • Second, unrelated functions and data, the basis of the procedural paradigm, provide a poor model of the real world.
  • 7. Unrestricted Access • In a procedural program, one written in C for example, there are two kinds of data. • Local data is hidden inside a function, and is used exclusively by the function. • However, when two or more functions must access the same data—and this is true of the most important data in a program—then the data must be made global • In a large program, there are many functions and many global data items. • The problem with the procedural paradigm is that this leads to an even larger number of potential connections between functions and data • This large number of connections causes problems in several ways. • First, it makes a program’s structure difficult to conceptualize. • Second, it makes the program difficult to modify. • A change made in a global data item may result in rewriting all the functions that access that item.
  • 8. Real-World Modeling • The more important problem with the procedural paradigm is that its arrangement of separate data and functions does a poor job of modeling things in the real world. • In the physical world we deal with objects such as people and cars. Such objects aren’t like data and they aren’t like functions. • Complex real-world objects have both attributes and behavior. • Attributes: (sometimes called characteristics) are, for people, eye color and job titles; and, for cars, horsepower and number of doors. • As it turns out, attributes in the real world are equivalent to data in a program: • they have a certain specific values, such as blue (for eye color) or four (for the number of doors). • Behavior: Behavior is something a real-world object does in response to some stimulus. Behavior is like a function: you call a function to do something. • If you ask your boss for a raise, she will generally say yes or no. • If you apply the brakes in a car, it will generally stop. • Saying something and stopping. • So neither data nor functions, by themselves, model real world objects effectively.
  • 9. The Object-Oriented Approach • The fundamental idea behind object-oriented languages is to combine into a single unit both data and the functions that operate on that data. Such a unit is called an object. • An object’s functions, called member functions in C++, typically provide the only way to access its data. • If you want to read a data item in an object, you call a member function in the object. • It will access the data and return the value to you. • You can’t access the data directly. The data is hidden, so it is safe from accidental alteration. • Data and its functions are said to be encapsulated into a single entity. • Data encapsulation and data hiding are key terms in the description of object-oriented languages. • If you want to modify the data in an object, you know exactly what functions interact with it: the member functions in the object. No other functions can access the data. This simplifies writing, debugging, and maintaining the program.
  • 10. The Object-Oriented Approach • A C++ program typically consists of a number of objects, • which communicate with each other by calling one another’s member functions. • The member functions in C++ are called methods. • Also, data items are referred to as attributes or instance variables. • Calling an object’s member function is referred to as sending a message to the object. • These terms are not official C++ terminology, but they are used with increasing frequency, especially in object-oriented design.