1 Chapter 14 Object-Oriented Software Development Dale/Weems.

Slides:



Advertisements
Similar presentations
1 Data Structures CSCI 132, Spring 2014 Lecture 8 Implementing Queues.
Advertisements

1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
1 class Rectangle{ private: int numVertices; float *xCoord, *yCoord; public: void set(float *x, float *y, int nV); float area(); }; Inheritance Concept.
Inheritance and Composition (aka containment) Textbook Chapter –
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
1 Another Way to Define A Class - Inheritance. 2 Inheritance Concept Rectangle Triangle Polygon class Polygon { private: int width, length; public: void.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Chapter 10 Inheritance, Polymorphism, and Scope. 2 Knowledge Goals Understand the hierarchical nature of classes in object-oriented programming Understand.
1 Chapter 6 Object-Oriented Software Development.
1 Lecture 29 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Chapter 10 Classes Continued
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Abstract Data Type (ADT) a data type whose properties (domain and operations) are specified (what) independently of any particular implementation (how)
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
1 Chapter 14-1 Object- Oriented Software Development Dale/Weems.
1 Chapter 14-2 Object- Oriented Software Development Dale/Weems.
Chapter 12: Adding Functionality to Your Classes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
CMSC 202 Inheritance.
1 Chapter 14 Object-Oriented Software Development.
1 Chapter 14 Object- Oriented Software Development Dale/Weems.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
1 Chapter 14 Object-Oriented Software Development Dale/Weems/Headington.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Design.ppt1 Top-down designs: 1. Define the Problem IPO 2. Identify tasks, Modularize 3. Use structure chart 4. Pseudocode for Mainline 5. Construct pseudocode.
Classes Structured Programming 256 Chapter 8 Classes - Part I OOP & Class Object Terminology File Management.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 12: Inheritance and Composition.
1 Overview of Object-Oriented Software Design and Java Programming Putting the Pieces Together!
Object-Oriented Programming Chapter Chapter
Object Oriented Programming
1 Final Exam Tues 3/16/10 (2-3:50) (Same classroom) Old Textbook - Chapters 11-16, 18 Focus is on 15, 16 and 18 Final Exam Tues 3/16/10 (2-3:50) (Same.
10/18/2011ecs40 fall Software Development & Object- Oriented Programming ecs40 Fall 2012: Software Development & Object- Oriented Programming #02:
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
1 Another way to define a class Inheritance..!!. 2 Why Inheritance ? Inheritance is a mechanism for building class types from existing class types defining.
Inheritance and Polymorphism
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
1 Chapter 12 Classes and Abstraction. 2 Chapter 12 Topics Meaning of an Abstract Data Type Declaring and Using a class Data Type Using Separate Specification.
Chapter 12 Classes and Abstraction
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Concept Polygon Rectangle Triangle class Rectangle{
Inheritance Saras M. Srivastava LEARN BY EXAMPLES PGT – Comp. Sc.
Inheritance CMSC 202, Version 4/02.
Polymorphism.
Review: Two Programming Paradigms
Chapter Structured Types, Data Abstraction and Classes
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Types of Programming Languages
Introduction to Structured Data Types and Classes
Inheritance Basics Programming with Inheritance
Lecture 22 Inheritance Richard Gesick.
Advanced Java Topics Chapter 9
Programming Techniques Course
Computer Programming with JAVA
Chapter 8: Class Relationships
Chapter 14 Object-Oriented Software Development
Another way to define a class
Lecture 6: Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Presentation transcript:

1 Chapter 14 Object-Oriented Software Development Dale/Weems

2 Chapter 14 Topics l Structured Programming vs. Object-Oriented Programming Using Inheritance to Create a New C++ class Type Using Composition (Containment) to Create a New C++ class Type l Static vs. Dynamic Binding of Operations to Objects l Virtual Member Functions

3 Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT Operations Data

4 Object-Oriented Programming Language Features 1. Data abstraction 2. Inheritance of properties 3. Dynamic binding of operations to objects

5 OOP Terms C++ Equivalents ObjectClass object or class instance Instance variablePrivate data member MethodPublic member function Message passingFunction call ( to a public member function)

6 What is an object? OBJECT Operations Data set of methods (public member functions) internal state (values of private data members)

7 Inheritance Hierarchy Among Vehicles vehicle wheeled vehicleboat bicyclecar four-door two-door Every car is a wheeled vehicle.

8 Inheritance l Inheritance is a mechanism by which one class acquires (inherits) the properties (both data and operations) of another class l The class being inherited from is the Base Class (Superclass) l The class that inherits is the Derived Class (Subclass) l The derived class is specialized by adding properties specific to it

9 class Time Specification // Specification file (“time.h”) class Time { public: void Set ( int hours, int minutes, int seconds); void Increment (); void Write () const; Time ( int initHrs, int initMins, int initSecs); // Constructor Time (); // Default constructor private: int hrs; int mins; int secs; }; 9

10 Class Interface Diagram Private data: hrs mins secs Set Increment Write Time Time class

11 // Specification file (“exttime.h”) #include “time.h” enum ZoneType{EST, CST, MST, PST, EDT, CDT, MDT, PDT}; class ExtTime : public Time // Time is the base class { public: void Set(int hours, int minutes, int seconds, ZoneType timeZone); void Write () const; ExtTime ( int initHrs, int initMins, int initSecs, ZoneType initZone); ExtTime (); private: ZoneType zone; // Additional data member }; Using Inheritance to Add Features 11

12 class ExtTime:public Time l Says class Time is a public base class of the derived class ExtTime l As a result, all public members of Time (except constructors) are also public members of ExtTime l In this example, new constructors are provided, new data member zone is added, and member functions Set and Write are overridden

13 Class Interface Diagram Private data: hrs mins secs ExtTime class Set Increment Write Time Set Increment Write ExtTime Private data: zone

14 Client Code Using ExtTime #include “exttime.h”. ExtTime thisTime ( 8, 35, 0, PST); ExtTime thatTime; // Default constructor called thatTime.Write(); // Outputs 00:00:00 EST cout << endl; thatTime.Set (16, 49, 23, CDT); thatTime.Write(); // Outputs 16:49:23 CDT cout << endl; thisTime.Increment (); thisTime.Write (); // Outputs 08:35:02 PST cout << endl; 14

15 Constructor Rules for Derived Classes l At run time, the base class constructor is implicitly called first, before the body of the derived class’s constructor executes l If the base class constructor requires parameters, they must be passed by the derived class’s constructor

16 Implementation of ExtTime Default Constructor ExtTime::ExtTime ( ) // Default Constructor // Postcondition: // hrs == 0 && mins == 0 && secs == 0 // (via an implicit call to base class default // constructor) // && zone == EST { zone = EST; }

17 ExtTime::ExtTime ( /* in */ int initHrs, /* in */ int initMins, /* in */ int initSecs, /* in */ ZoneType initZone) : Time (initHrs, initMins, initSecs) // Constructor initializer // Pre: 0 <= initHrs <= 23 && 0 <= initMins <= 59 // 0 <= initSecs <= 59 && initZone is assigned // Post: zone == initZone && Time set by base // class constructor { zone = initZone; } Implementation of Another ExtTime Class Constructor 17

18 Implementation of ExtTime::Set function void ExtTime::Set ( /* in */ int initHrs, /* in */ int initMins, /* in */ int initSecs, /* in */ ZoneType initZone) // Pre: 0 <= initHrs <= 23 && 0 <= initMins <= 59 // 0 <= initSecs <= 59 && initZone is assigned // Post: zone == timeZone && Time set by base // class function { Time::Set (hours, minutes, seconds); zone = timeZone; } 18

19 Implementation of ExtTime::Write Function void ExtTime::Write ( ) const // Postcondition: // Time has been output in form HH:MM:SS ZZZ // where ZZZ is the time zone abbreviation { static string zoneString[8] = { “EST”, CST”, MST”, “PST”, “EDT”, “CDT”, “MDT”, “PDT” }; Time::Write (); cout << ‘ ‘ << zoneString[zone]; } 19

20 Responsibilities l Responsibilities are operations implemented as C++ functions l Action responsibilities are operations that perform an action l Knowledge responsibilities are operations that return the state of private data variables

21 What responsibilities are Missing? The Time class needs int Hours() int Minutes() int Seconds() The ExtTime class needs ZoneType zone()

22 Composition (or Containment) l Composition (containment) is a mechanism by which the internal data (the state) of one class includes an object of another class

23 An Entry Object #include "Time.h" #include "Name.h" #include class Entry { public: string NameStr() const; // Returns a string made up of first name and last name string TimeStr() const; // Returns a string made up of hour, colon, minutes Entry(); // Default constructor Entry(……) // Parameterized constructor private: Name name; Time time; }

24 Order in Which Constructors are Executed Given a class X, l if X is a derived class its base class constructor is executed first l next, constructors for member objects (if any) are executed (using their own default constructors if none is specified) l finally, the body of X’s constructor is executed

25 In C++... When the type of a formal parameter is a parent class, the argument used can be l the same type as the formal parameter or, l any descendant class type

26 Static Binding l Static binding is the compile-time determination of which function to call for a particular object based on the type of the formal parameter(s) l When pass-by-value is used, static binding occurs

27 Static Binding Is Based on Formal Parameter Type void Print ( /* in */ Time someTime) { cout << “Time is “; someTime.Write ( ); cout << endl; } CLIENT CODE OUTPUT Time startTime(8, 30, 0); ExtTime endTime(10,45,0,CST); Print ( startTime); Time is 08:30:00 Print ( endTime); Time is 10:45:00

28 Dynamic Binding l Dynamic binding is the run-time determination of which function to call for a particular object of a descendant class based on the type of the argument l Feclaring a member function to be virtual instructs the compiler to generate code that guarantees dynamic binding

29 Virtual Member Function // Specification file ( “time.h”) class Time { public:... virtual void Write () const; // Forces dynamic binding... private: int hrs; int mins; int secs; };

30 Dynamic binding requires pass-by-reference void Print ( /* in */ Time& someTime) { cout << “Time is “; someTime.Write ( ); cout << endl; } CLIENT CODE OUTPUT Time startTime ( 8, 30, 0); ExtTime endTime (10,45,0,CST); Print ( startTime); Time is 08:30:00 Print ( endTime); Time is 10:45:00 CST

31 Using virtual functions in C++ l Dynamic binding requires pass-by-reference when passing a class object to a function l In the declaration for a virtual function, the word virtual appears only in the base class l If a base class declares a virtual function, it must implement that function, even if the body is empty l A derived class is not required to re-implement a virtual function; if it does not, the base class version is used

32 Slicing Problem l Insert Figure 14-6, page 752

33 Object-Oriented Design l Identify the Objects and Operations l Determine the relationship among objects l Design and Implement the driver

34 Implementation of the Design l Choose a suitable data representation n Built-in data type n Existing ADT n Create a new ADT l Create algorithms for the abstract operations n Top-down design is often the best technique to create algorithms for the function tasks

35 Case Study l Beginning of the Appointment Calendar Program l Class Entry composed of a Name class and a Time class l Current Time class represents active time (seconds with Increment) l Need a static Time class for time in the appointment calendar Is inheritance appropriate?