SlideShare a Scribd company logo
1 of 37
CSC 103 – Object Oriented
     Programming

            Lecture 7

  Reference: Ch. 1, Robert Lafore, 3rd ed.


             9th March, 2011

          Engr. M. Anwar-ul-Haq
What is Object Oriented
         Programming?
• Object-oriented programming (OOP) is
  a programming paradigm that uses "objects"
  – data structures consisting of data
  fields and methods together with their
  interactions – to design applications and
  computer programs.

• Programming techniques may include features
  such as data abstraction, encapsulation,
  modularity, polymorphism & inheritance. Many
  programming languages now support OOP.
              OOP Spring, 2011. Engr. Anwar, Foundation   2
                  University (FUIEMS), Islamabad
Need for OOP
• 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.


              OOP Spring, 2011. Engr. Anwar, Foundation   3
                  University (FUIEMS), Islamabad
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.

• Which languages are Procedural and which are Object
  Oriented?
                 OOP Spring, 2011. Engr. Anwar, Foundation    4
                     University (FUIEMS), Islamabad
Division into Functions
• Few programmers can comprehend a
  program of more than a few hundred
  statements unless it is broken down into
  smaller units.

• The term function is used in C++ and C. A
  subroutine, a subprogram, or a procedure
  in other languages.
             OOP Spring, 2011. Engr. Anwar, Foundation   5
                 University (FUIEMS), Islamabad
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), but the principle is similar: a
  grouping of components that carries out
  specific tasks.


               OOP Spring, 2011. Engr. Anwar, Foundation   6
                   University (FUIEMS), Islamabad
Division into Functions/Modules
• 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.


             OOP Spring, 2011. Engr. Anwar, Foundation   7
                 University (FUIEMS), Islamabad
Problems with Structured
           Programming
• Analyzing the reasons for these 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.
             OOP Spring, 2011. Engr. Anwar, Foundation   8
                 University (FUIEMS), Islamabad
Problems with Structured
           Programming
• 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.


             OOP Spring, 2011. Engr. Anwar, Foundation   9
                 University (FUIEMS), Islamabad
Unrestricted Access
• There are two kinds of data. Local data is
  hidden inside a function, and is used exclusively
  by the function. In the inventory program a
  display function might use local data to
  remember which item it was displaying.

• 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, as our collection of inventory
  items is. Global data can be accessed by any
  function in the program.
               OOP Spring, 2011. Engr. Anwar, Foundation   10
                   University (FUIEMS), Islamabad
Unrestricted Access




   OOP Spring, 2011. Engr. Anwar, Foundation   11
       University (FUIEMS), Islamabad
Unrestricted Access
• 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.

• A change made in a global data item may result
  in rewriting all the functions that access that
  item.

               OOP Spring, 2011. Engr. Anwar, Foundation   12
                   University (FUIEMS), Islamabad
Unrestricted Access




   OOP Spring, 2011. Engr. Anwar, Foundation   13
       University (FUIEMS), Islamabad
Real-World Modeling
• The second—and 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.

• Complex real-world objects have both
  attributes and behavior.
             OOP Spring, 2011. Engr. Anwar, Foundation   14
                 University (FUIEMS), Islamabad
Real-World Modeling
• Attributes
  – People: eye color and job etc.
  – Cars: gear, horsepower and number of doors.
  – Rectangle: Length & Width
  – Fractions: Numerator & Denominator
  – Complex Numbers: Real & Imaginary
• Behavior
  – Something a real-world object does in
    response to some stimulus.
  – Rectangle: Perimeter and Area
  – Cars: changeGear etc. Anwar, Foundation
              OOP Spring, 2011. Engr.              15
                  University (FUIEMS), Islamabad
Real-World Modeling


  So neither data nor functions, by
themselves, model real world objects
             effectively.




         OOP Spring, 2011. Engr. Anwar, Foundation   16
             University (FUIEMS), Islamabad
Other problems of procedural
           languages.
• Difficulty of creating new data types.

• Several built-in data types: integers,
  floating-point numbers, characters, and so
  on.

• Why do you need to invent your own data
  type?
              OOP Spring, 2011. Engr. Anwar, Foundation   17
                  University (FUIEMS), Islamabad
New Data Types
• Being able to create your own types is
  called extensibility.

• You can extend the capabilities of the
  language. Traditional languages are not
  usually extensible.



             OOP Spring, 2011. Engr. Anwar, Foundation   18
                 University (FUIEMS), Islamabad
Object-Oriented Approach
• Combine into a single unit both data and
  the functions that operate on that data.
  Such a unit is called an object.




             OOP Spring, 2011. Engr. Anwar, Foundation   19
                 University (FUIEMS), Islamabad
Object-Oriented Approach
• 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.
               OOP Spring, 2011. Engr. Anwar, Foundation   20
                   University (FUIEMS), Islamabad
Object-Oriented Approach




      OOP Spring, 2011. Engr. Anwar, Foundation   21
          University (FUIEMS), Islamabad
Characteristics of Object-
       Oriented Languages
• Objects
  – When you approach a programming problem in an
    object-oriented language, you no longer ask how the
    problem will be divided into functions, but how it will
    be divided into objects. Thinking in terms of objects,
    rather than functions.

  – What kinds of things become objects in object-
    oriented programs? Your imagination, limits.



                 OOP Spring, 2011. Engr. Anwar, Foundation    22
                     University (FUIEMS), Islamabad
Objects in OOP
• Physical objects
  –   Automobiles in a traffic-flow simulation
  –   Electrical components in a circuit-design program
  –   Countries in an economics model
  –   Aircraft in an air-traffic-control system

• Elements of the computer-user environment
  –   Windows
  –   Menus
  –   Graphics objects (lines, rectangles, circles)
  –   The mouse, keyboard, disk drives, printer

                  OOP Spring, 2011. Engr. Anwar, Foundation   23
                      University (FUIEMS), Islamabad
Objects in OOP
• Human entities
  –   Employees
  –   Students
  –   Customers
  –   Salespeople

• Components in computer games
  –   Cars in an auto race
  –   Positions in a board game (chess, checkers)
  –   Animals in an ecological simulation
  –   Opponents and friends in adventure games

                 OOP Spring, 2011. Engr. Anwar, Foundation   24
                     University (FUIEMS), Islamabad
Classes
• Objects are members of classes.

• You can declare as many variables of type
  int as you need in your program:
  – int day;
  – int count;
  – int divisor;
  – int answer;
               OOP Spring, 2011. Engr. Anwar, Foundation   25
                   University (FUIEMS), Islamabad
Classes
• A class is thus a description of a number
  of similar objects. This fits our non-
  technical understanding of the word class.




             OOP Spring, 2011. Engr. Anwar, Foundation   26
                 University (FUIEMS), Islamabad
Inheritance
• Class of animals is divided into mammals,
  amphibians, insects, birds, and so on. The
  class of vehicles is divided into cars,
  trucks, buses, and motorcycles.

• The principle in this sort of division is that
  each subclass shares common
  characteristics with the class from which
  it’s derived.
              OOP Spring, 2011. Engr. Anwar, Foundation   27
                  University (FUIEMS), Islamabad
Inheritance
• Cars, trucks, buses, and motorcycles all have
  wheels and a motor; these are the defining
  characteristics of vehicles.

• In addition to the characteristics shared with
  other members of the class, each subclass also
  has its own particular characteristics: Buses, for
  instance, have seats for many people, while
  trucks have space for hauling heavy loads.

               OOP Spring, 2011. Engr. Anwar, Foundation   28
                   University (FUIEMS), Islamabad
Inheritance




OOP Spring, 2011. Engr. Anwar, Foundation   29
    University (FUIEMS), Islamabad
Inheritance
• The original class is called the base class.

• Other classes can be defined that share its
  characteristics, but add their own as well. These
  are called derived classes.

• As functions do in a procedural program,
  inheritance shortens an object-oriented program
  and clarifies the relationship among program
  elements.
               OOP Spring, 2011. Engr. Anwar, Foundation   30
                   University (FUIEMS), Islamabad
Reusability
• Once a class has been written, created,
  and debugged, it can be distributed to
  other programmers for use in their own
  programs. This is called reusability.

• It is similar to the way a library of functions
  in a procedural language can be
  incorporated into different programs.
              OOP Spring, 2011. Engr. Anwar, Foundation   31
                  University (FUIEMS), Islamabad
Reusability
• A programmer can take an existing class
  and, without modifying it, add additional
  features and capabilities to it.

• This is done by deriving a new class from
  the existing one. The new class will inherit
  the capabilities of the old one, but is free
  to add new features of its own.
             OOP Spring, 2011. Engr. Anwar, Foundation   32
                 University (FUIEMS), Islamabad
Reusability
• Example: A class that creates a menu system,
  such as that used in Windows or other Graphic
  User Interfaces (GUIs). This class works fine,
  and you don’t want to change it, but you want to
  add the capability to make some menu entries
  flash on and off.


• Being able to reuse classes on a second project
  provides an increased return on their original
  programming investment.

               OOP Spring, 2011. Engr. Anwar, Foundation   33
                   University (FUIEMS), Islamabad
Creating New Data Types
• Objects give the programmer a convenient
  way to construct new data types.

• OO programming also provides the
  capability of Polymorphism and
  Overloading.



            OOP Spring, 2011. Engr. Anwar, Foundation   34
                University (FUIEMS), Islamabad
C++ and C
• C++ is derived from the C language. Strictly
  speaking, it is a superset of C.

• Almost every correct statement in C is also a
  correct statement in C++, although the reverse is
  not true.

• However, C++ has many other new features as
  well, including an improved approach to
  input/output (I/O).
               OOP Spring, 2011. Engr. Anwar, Foundation   35
                   University (FUIEMS), Islamabad
Summary
• OOP is a way of organizing programs. The
  emphasis is on the way programs are
  designed, not on coding details.

• In particular, OOP programs are organized
  around objects, which contain both data
  and functions that act on that data. A class
  is a template for a number of objects.
             OOP Spring, 2011. Engr. Anwar, Foundation   36
                 University (FUIEMS), Islamabad
Summary
• Inheritance allows a class to be derived from an
  existing class without modifying it.

• The derived class has all the data and functions
  of the parent class, but adds new ones of its
  own.

• Inheritance makes possible reusability, or using
  a class over and over in different programs.



               OOP Spring, 2011. Engr. Anwar, Foundation   37
                   University (FUIEMS), Islamabad

More Related Content

What's hot

Need of object oriented programming
Need of object oriented programmingNeed of object oriented programming
Need of object oriented programmingAmar Jukuntla
 
Object Oriented Programming : A Brief History and its significance
Object Oriented Programming : A Brief History and its significanceObject Oriented Programming : A Brief History and its significance
Object Oriented Programming : A Brief History and its significanceGajesh Bhat
 
OOP Unit 1 - Foundation of Object- Oriented Programming
OOP Unit 1 - Foundation of Object- Oriented ProgrammingOOP Unit 1 - Foundation of Object- Oriented Programming
OOP Unit 1 - Foundation of Object- Oriented Programmingdkpawar
 
Java object oriented programming concepts - Brainsmartlabs
Java object oriented programming concepts - BrainsmartlabsJava object oriented programming concepts - Brainsmartlabs
Java object oriented programming concepts - Brainsmartlabsbrainsmartlabsedu
 
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop Kumar
 
Cs8392 u1-1-oop intro
Cs8392 u1-1-oop introCs8392 u1-1-oop intro
Cs8392 u1-1-oop introRajasekaran S
 
Object Oriented Programming Principles
Object Oriented Programming PrinciplesObject Oriented Programming Principles
Object Oriented Programming PrinciplesAndrew Ferlitsch
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSkillwise Group
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops conceptsNilesh Dalvi
 
Std 12 computer chapter 6 object oriented concepts (part 1)
Std 12 computer chapter 6 object oriented concepts (part 1)Std 12 computer chapter 6 object oriented concepts (part 1)
Std 12 computer chapter 6 object oriented concepts (part 1)Nuzhat Memon
 
1 unit (oops)
1 unit (oops)1 unit (oops)
1 unit (oops)Jay Patel
 
Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)indiangarg
 
OOP programming
OOP programmingOOP programming
OOP programminganhdbh
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingAmit Soni (CTFL)
 

What's hot (20)

Need of object oriented programming
Need of object oriented programmingNeed of object oriented programming
Need of object oriented programming
 
Object Oriented Programming : A Brief History and its significance
Object Oriented Programming : A Brief History and its significanceObject Oriented Programming : A Brief History and its significance
Object Oriented Programming : A Brief History and its significance
 
OOP Unit 1 - Foundation of Object- Oriented Programming
OOP Unit 1 - Foundation of Object- Oriented ProgrammingOOP Unit 1 - Foundation of Object- Oriented Programming
OOP Unit 1 - Foundation of Object- Oriented Programming
 
Java object oriented programming concepts - Brainsmartlabs
Java object oriented programming concepts - BrainsmartlabsJava object oriented programming concepts - Brainsmartlabs
Java object oriented programming concepts - Brainsmartlabs
 
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop
 
Cs8392 u1-1-oop intro
Cs8392 u1-1-oop introCs8392 u1-1-oop intro
Cs8392 u1-1-oop intro
 
Oop ppt
Oop pptOop ppt
Oop ppt
 
Object Oriented Programming Principles
Object Oriented Programming PrinciplesObject Oriented Programming Principles
Object Oriented Programming Principles
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPT
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 
Std 12 computer chapter 6 object oriented concepts (part 1)
Std 12 computer chapter 6 object oriented concepts (part 1)Std 12 computer chapter 6 object oriented concepts (part 1)
Std 12 computer chapter 6 object oriented concepts (part 1)
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
 
1 unit (oops)
1 unit (oops)1 unit (oops)
1 unit (oops)
 
Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)
 
Oops
OopsOops
Oops
 
Ah java-ppt2
Ah java-ppt2Ah java-ppt2
Ah java-ppt2
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
OOP programming
OOP programmingOOP programming
OOP programming
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
2 Object Oriented Programming
2 Object Oriented Programming2 Object Oriented Programming
2 Object Oriented Programming
 

Similar to oop Lecture 7

Sulthan's_JAVA_Material_for_B.Sc-CS.pdf
Sulthan's_JAVA_Material_for_B.Sc-CS.pdfSulthan's_JAVA_Material_for_B.Sc-CS.pdf
Sulthan's_JAVA_Material_for_B.Sc-CS.pdfSULTHAN BASHA
 
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.pptsagarjsicg
 
Object Oriented programming - Introduction
Object Oriented programming - IntroductionObject Oriented programming - Introduction
Object Oriented programming - IntroductionMadishetty Prathibha
 
Introduction to Software - Coder Forge - John Mulhall
Introduction to Software - Coder Forge - John MulhallIntroduction to Software - Coder Forge - John Mulhall
Introduction to Software - Coder Forge - John MulhallJohn Mulhall
 
Object Oriented Approach For Software Development
Object Oriented Approach For Software DevelopmentObject Oriented Approach For Software Development
Object Oriented Approach For Software DevelopmentJessica Tanner
 
Need of OOPs and Programming,pop vs oop
Need of OOPs and Programming,pop vs oopNeed of OOPs and Programming,pop vs oop
Need of OOPs and Programming,pop vs oopJanani Selvaraj
 
Object Oriented Programming - 2. OOP Concept
Object Oriented Programming - 2. OOP ConceptObject Oriented Programming - 2. OOP Concept
Object Oriented Programming - 2. OOP ConceptAndiNurkholis1
 
NL based Object Oriented modeling - EJSR 35(1)
NL based Object Oriented modeling - EJSR 35(1)NL based Object Oriented modeling - EJSR 35(1)
NL based Object Oriented modeling - EJSR 35(1)IT Industry
 
INVESTIGATION OF ATTITUDES TOWARDS COMPUTER PROGRAMMING IN TERMS OF VARIOUS V...
INVESTIGATION OF ATTITUDES TOWARDS COMPUTER PROGRAMMING IN TERMS OF VARIOUS V...INVESTIGATION OF ATTITUDES TOWARDS COMPUTER PROGRAMMING IN TERMS OF VARIOUS V...
INVESTIGATION OF ATTITUDES TOWARDS COMPUTER PROGRAMMING IN TERMS OF VARIOUS V...ijpla
 
OOPs-Interview-Questions.pdf
OOPs-Interview-Questions.pdfOOPs-Interview-Questions.pdf
OOPs-Interview-Questions.pdfSamir Paul
 

Similar to oop Lecture 7 (20)

CS3391 -OOP -UNIT – I NOTES FINAL.pdf
CS3391 -OOP -UNIT – I  NOTES FINAL.pdfCS3391 -OOP -UNIT – I  NOTES FINAL.pdf
CS3391 -OOP -UNIT – I NOTES FINAL.pdf
 
Sulthan's_JAVA_Material_for_B.Sc-CS.pdf
Sulthan's_JAVA_Material_for_B.Sc-CS.pdfSulthan's_JAVA_Material_for_B.Sc-CS.pdf
Sulthan's_JAVA_Material_for_B.Sc-CS.pdf
 
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
 
OOP Java
OOP JavaOOP Java
OOP Java
 
Object Oriented programming - Introduction
Object Oriented programming - IntroductionObject Oriented programming - Introduction
Object Oriented programming - Introduction
 
Introduction to Software - Coder Forge - John Mulhall
Introduction to Software - Coder Forge - John MulhallIntroduction to Software - Coder Forge - John Mulhall
Introduction to Software - Coder Forge - John Mulhall
 
Object Oriented Approach For Software Development
Object Oriented Approach For Software DevelopmentObject Oriented Approach For Software Development
Object Oriented Approach For Software Development
 
Oop.pptx
Oop.pptxOop.pptx
Oop.pptx
 
Need of OOPs and Programming,pop vs oop
Need of OOPs and Programming,pop vs oopNeed of OOPs and Programming,pop vs oop
Need of OOPs and Programming,pop vs oop
 
130704798265658191
130704798265658191130704798265658191
130704798265658191
 
Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Object Oriented Programming - 2. OOP Concept
Object Oriented Programming - 2. OOP ConceptObject Oriented Programming - 2. OOP Concept
Object Oriented Programming - 2. OOP Concept
 
OOP ppt.pdf
OOP ppt.pdfOOP ppt.pdf
OOP ppt.pdf
 
NL based Object Oriented modeling - EJSR 35(1)
NL based Object Oriented modeling - EJSR 35(1)NL based Object Oriented modeling - EJSR 35(1)
NL based Object Oriented modeling - EJSR 35(1)
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
 
Unit-V.pptx
Unit-V.pptxUnit-V.pptx
Unit-V.pptx
 
INVESTIGATION OF ATTITUDES TOWARDS COMPUTER PROGRAMMING IN TERMS OF VARIOUS V...
INVESTIGATION OF ATTITUDES TOWARDS COMPUTER PROGRAMMING IN TERMS OF VARIOUS V...INVESTIGATION OF ATTITUDES TOWARDS COMPUTER PROGRAMMING IN TERMS OF VARIOUS V...
INVESTIGATION OF ATTITUDES TOWARDS COMPUTER PROGRAMMING IN TERMS OF VARIOUS V...
 
OOPs-Interview-Questions.pdf
OOPs-Interview-Questions.pdfOOPs-Interview-Questions.pdf
OOPs-Interview-Questions.pdf
 
Oop's
Oop'sOop's
Oop's
 

Recently uploaded

Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEaurabinda banchhor
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 

Recently uploaded (20)

Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSE
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptxINCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
INCLUSIVE EDUCATION PRACTICES FOR TEACHERS AND TRAINERS.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 

oop Lecture 7

  • 1. CSC 103 – Object Oriented Programming Lecture 7 Reference: Ch. 1, Robert Lafore, 3rd ed. 9th March, 2011 Engr. M. Anwar-ul-Haq
  • 2. What is Object Oriented Programming? • Object-oriented programming (OOP) is a programming paradigm that uses "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. • Programming techniques may include features such as data abstraction, encapsulation, modularity, polymorphism & inheritance. Many programming languages now support OOP. OOP Spring, 2011. Engr. Anwar, Foundation 2 University (FUIEMS), Islamabad
  • 3. Need for OOP • 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. OOP Spring, 2011. Engr. Anwar, Foundation 3 University (FUIEMS), Islamabad
  • 4. 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. • Which languages are Procedural and which are Object Oriented? OOP Spring, 2011. Engr. Anwar, Foundation 4 University (FUIEMS), Islamabad
  • 5. Division into Functions • Few programmers can comprehend a program of more than a few hundred statements unless it is broken down into smaller units. • The term function is used in C++ and C. A subroutine, a subprogram, or a procedure in other languages. OOP Spring, 2011. Engr. Anwar, Foundation 5 University (FUIEMS), Islamabad
  • 6. 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), but the principle is similar: a grouping of components that carries out specific tasks. OOP Spring, 2011. Engr. Anwar, Foundation 6 University (FUIEMS), Islamabad
  • 7. Division into Functions/Modules • 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. OOP Spring, 2011. Engr. Anwar, Foundation 7 University (FUIEMS), Islamabad
  • 8. Problems with Structured Programming • Analyzing the reasons for these 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. OOP Spring, 2011. Engr. Anwar, Foundation 8 University (FUIEMS), Islamabad
  • 9. Problems with Structured Programming • 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. OOP Spring, 2011. Engr. Anwar, Foundation 9 University (FUIEMS), Islamabad
  • 10. Unrestricted Access • There are two kinds of data. Local data is hidden inside a function, and is used exclusively by the function. In the inventory program a display function might use local data to remember which item it was displaying. • 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, as our collection of inventory items is. Global data can be accessed by any function in the program. OOP Spring, 2011. Engr. Anwar, Foundation 10 University (FUIEMS), Islamabad
  • 11. Unrestricted Access OOP Spring, 2011. Engr. Anwar, Foundation 11 University (FUIEMS), Islamabad
  • 12. Unrestricted Access • 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. • A change made in a global data item may result in rewriting all the functions that access that item. OOP Spring, 2011. Engr. Anwar, Foundation 12 University (FUIEMS), Islamabad
  • 13. Unrestricted Access OOP Spring, 2011. Engr. Anwar, Foundation 13 University (FUIEMS), Islamabad
  • 14. Real-World Modeling • The second—and 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. • Complex real-world objects have both attributes and behavior. OOP Spring, 2011. Engr. Anwar, Foundation 14 University (FUIEMS), Islamabad
  • 15. Real-World Modeling • Attributes – People: eye color and job etc. – Cars: gear, horsepower and number of doors. – Rectangle: Length & Width – Fractions: Numerator & Denominator – Complex Numbers: Real & Imaginary • Behavior – Something a real-world object does in response to some stimulus. – Rectangle: Perimeter and Area – Cars: changeGear etc. Anwar, Foundation OOP Spring, 2011. Engr. 15 University (FUIEMS), Islamabad
  • 16. Real-World Modeling So neither data nor functions, by themselves, model real world objects effectively. OOP Spring, 2011. Engr. Anwar, Foundation 16 University (FUIEMS), Islamabad
  • 17. Other problems of procedural languages. • Difficulty of creating new data types. • Several built-in data types: integers, floating-point numbers, characters, and so on. • Why do you need to invent your own data type? OOP Spring, 2011. Engr. Anwar, Foundation 17 University (FUIEMS), Islamabad
  • 18. New Data Types • Being able to create your own types is called extensibility. • You can extend the capabilities of the language. Traditional languages are not usually extensible. OOP Spring, 2011. Engr. Anwar, Foundation 18 University (FUIEMS), Islamabad
  • 19. Object-Oriented Approach • Combine into a single unit both data and the functions that operate on that data. Such a unit is called an object. OOP Spring, 2011. Engr. Anwar, Foundation 19 University (FUIEMS), Islamabad
  • 20. Object-Oriented Approach • 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. OOP Spring, 2011. Engr. Anwar, Foundation 20 University (FUIEMS), Islamabad
  • 21. Object-Oriented Approach OOP Spring, 2011. Engr. Anwar, Foundation 21 University (FUIEMS), Islamabad
  • 22. Characteristics of Object- Oriented Languages • Objects – When you approach a programming problem in an object-oriented language, you no longer ask how the problem will be divided into functions, but how it will be divided into objects. Thinking in terms of objects, rather than functions. – What kinds of things become objects in object- oriented programs? Your imagination, limits. OOP Spring, 2011. Engr. Anwar, Foundation 22 University (FUIEMS), Islamabad
  • 23. Objects in OOP • Physical objects – Automobiles in a traffic-flow simulation – Electrical components in a circuit-design program – Countries in an economics model – Aircraft in an air-traffic-control system • Elements of the computer-user environment – Windows – Menus – Graphics objects (lines, rectangles, circles) – The mouse, keyboard, disk drives, printer OOP Spring, 2011. Engr. Anwar, Foundation 23 University (FUIEMS), Islamabad
  • 24. Objects in OOP • Human entities – Employees – Students – Customers – Salespeople • Components in computer games – Cars in an auto race – Positions in a board game (chess, checkers) – Animals in an ecological simulation – Opponents and friends in adventure games OOP Spring, 2011. Engr. Anwar, Foundation 24 University (FUIEMS), Islamabad
  • 25. Classes • Objects are members of classes. • You can declare as many variables of type int as you need in your program: – int day; – int count; – int divisor; – int answer; OOP Spring, 2011. Engr. Anwar, Foundation 25 University (FUIEMS), Islamabad
  • 26. Classes • A class is thus a description of a number of similar objects. This fits our non- technical understanding of the word class. OOP Spring, 2011. Engr. Anwar, Foundation 26 University (FUIEMS), Islamabad
  • 27. Inheritance • Class of animals is divided into mammals, amphibians, insects, birds, and so on. The class of vehicles is divided into cars, trucks, buses, and motorcycles. • The principle in this sort of division is that each subclass shares common characteristics with the class from which it’s derived. OOP Spring, 2011. Engr. Anwar, Foundation 27 University (FUIEMS), Islamabad
  • 28. Inheritance • Cars, trucks, buses, and motorcycles all have wheels and a motor; these are the defining characteristics of vehicles. • In addition to the characteristics shared with other members of the class, each subclass also has its own particular characteristics: Buses, for instance, have seats for many people, while trucks have space for hauling heavy loads. OOP Spring, 2011. Engr. Anwar, Foundation 28 University (FUIEMS), Islamabad
  • 29. Inheritance OOP Spring, 2011. Engr. Anwar, Foundation 29 University (FUIEMS), Islamabad
  • 30. Inheritance • The original class is called the base class. • Other classes can be defined that share its characteristics, but add their own as well. These are called derived classes. • As functions do in a procedural program, inheritance shortens an object-oriented program and clarifies the relationship among program elements. OOP Spring, 2011. Engr. Anwar, Foundation 30 University (FUIEMS), Islamabad
  • 31. Reusability • Once a class has been written, created, and debugged, it can be distributed to other programmers for use in their own programs. This is called reusability. • It is similar to the way a library of functions in a procedural language can be incorporated into different programs. OOP Spring, 2011. Engr. Anwar, Foundation 31 University (FUIEMS), Islamabad
  • 32. Reusability • A programmer can take an existing class and, without modifying it, add additional features and capabilities to it. • This is done by deriving a new class from the existing one. The new class will inherit the capabilities of the old one, but is free to add new features of its own. OOP Spring, 2011. Engr. Anwar, Foundation 32 University (FUIEMS), Islamabad
  • 33. Reusability • Example: A class that creates a menu system, such as that used in Windows or other Graphic User Interfaces (GUIs). This class works fine, and you don’t want to change it, but you want to add the capability to make some menu entries flash on and off. • Being able to reuse classes on a second project provides an increased return on their original programming investment. OOP Spring, 2011. Engr. Anwar, Foundation 33 University (FUIEMS), Islamabad
  • 34. Creating New Data Types • Objects give the programmer a convenient way to construct new data types. • OO programming also provides the capability of Polymorphism and Overloading. OOP Spring, 2011. Engr. Anwar, Foundation 34 University (FUIEMS), Islamabad
  • 35. C++ and C • C++ is derived from the C language. Strictly speaking, it is a superset of C. • Almost every correct statement in C is also a correct statement in C++, although the reverse is not true. • However, C++ has many other new features as well, including an improved approach to input/output (I/O). OOP Spring, 2011. Engr. Anwar, Foundation 35 University (FUIEMS), Islamabad
  • 36. Summary • OOP is a way of organizing programs. The emphasis is on the way programs are designed, not on coding details. • In particular, OOP programs are organized around objects, which contain both data and functions that act on that data. A class is a template for a number of objects. OOP Spring, 2011. Engr. Anwar, Foundation 36 University (FUIEMS), Islamabad
  • 37. Summary • Inheritance allows a class to be derived from an existing class without modifying it. • The derived class has all the data and functions of the parent class, but adds new ones of its own. • Inheritance makes possible reusability, or using a class over and over in different programs. OOP Spring, 2011. Engr. Anwar, Foundation 37 University (FUIEMS), Islamabad