Chapter 14 1 Object Oriented Software Development DaleWeems
Chapter 14 -1 Object. Oriented Software Development Dale/Weems 1
Chapter 14 Topics l l 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 Static vs. Dynamic Binding of Operations to Objects Virtual Member Functions 2
Two Programming Paradigms Structural (Procedural) PROGRAM Object-Oriented PROGRAM OBJECT FUNCTION Operations FUNCTION Data OBJECT Operations Data 3
Object-Oriented Programming Language Features 1. Data abstraction 2. Inheritance of properties 3. Dynamic binding of operations to objects 4
OOP Terms C++ Equivalents Object Class object or class instance Instance variable Private data member Method Public member function Message passing Function call ( to a public member function) 5
What is an object? OBJECT Operations Data set of methods (public member functions) internal state (values of private data members) 6
Inheritance Hierarchy Among Vehicles vehicle wheeled vehicle car two-door boat bicycle four-door Every car is a wheeled vehicle. 7
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 8
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 init. Hrs, int init. Mins, int init. Secs); // Constructor Time (); // Default constructor private: }; int hrs; int mins; int secs; 9 9
Class Interface Diagram Time class Set Increment Private data: hrs Write Time mins secs Time 10
Using Inheritance to Add Features // Specification file (“exttime. h”) #include “time. h” enum Zone. Type{EST, CST, MST, PST, EDT, CDT, MDT, PDT}; class Ext. Time : public Time // Time is the base class { public: void Set(int hours, int minutes, int seconds, Zone. Type time. Zone); void Write () const; Ext. Time ( int init. Hrs, int init. Mins, int init. Secs, Zone. Type init. Zone); Ext. Time (); private: Zone. Type zone; // Additional data member }; 11 11
class Ext. Time: public Time l Says class Time is a public base class of the derived class Ext. Time l As a result, all public members of Time (except constructors) are also public members of Ext. Time l In this example, new constructors are provided, new data member zone is added, and member functions Set and Write are overridden 12
Class Interface Diagram Ext. Time class Set Increment Write Private data: hrs mins Ext. Time secs Private data: zone 13
Accessibility Inheritance does not imply accessibility. l The derived class inherits all the members of its base class, both public and private l The derived class (its member functions) cannot access the private members of the base class l 14
Client Code Using Ext. Time #include “exttime. h”. . . Ext. Time this. Time ( 8, 35, 0, PST); Ext. Time that. Time; // Default constructor called that. Time. Write(); // Outputs 00: 00 EST cout << endl; that. Time. Set (16, 49, 23, CDT); that. Time. Write(); // Outputs 16: 49: 23 CDT cout << endl; this. Time. Increment (); this. Time. Write (); // Outputs 08: 35: 02 PST cout << endl; 15 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 Ext. Time Default Constructor Ext. Time: : Ext. Time ( ) // Default Constructor // Postcondition: // hrs == 0 && mins == 0 && secs == 0 // // (via an implicit call to base class default constructor) // { && zone == EST zone = EST; } 17
Implementation of Another Ext. Time Class Constructor Ext. Time: : Ext. Time ( /* in */ int init. Hrs, /* in */ int init. Mins, /* in */ int init. Secs, /* in */ Zone. Type init. Zone) : Time (init. Hrs, init. Mins, init. Secs) // Constructor initializer // Pre: 0 <= init. Hrs <= 23 && 0 <= init. Mins <= 59 // 0 <= init. Secs <= 59 && init. Zone is assigned // Post: zone == init. Zone && Time set by base // class constructor { zone = init. Zone; } 18 18
Implementation of Ext. Time: : Set function void Ext. Time: : Set ( /* in */ int init. Hrs, /* in */ int init. Mins, /* in */ int init. Secs, /* in */ Zone. Type init. Zone) // Pre: 0 <= init. Hrs <= 23 && 0 <= init. Mins <= 59 // 0 <= init. Secs <= 59 && init. Zone is assigned // Post: zone == time. Zone && Time set by base // class function { Time: : Set (init. Hrs, init. Mins, init. Secs); zone = init. Zone; } 19 19
Implementation of Ext. Time: : Write Function void Ext. Time: : Write ( ) const // Postcondition: // Time has been output in form HH: MM: SS ZZZ // where ZZZ is the time zone abbreviation { static string zone. String[8] = { “EST”, CST”, MST”, “PST”, “EDT”, “CDT”, “MDT”, “PDT” }; Time: : Write (); cout << ‘ ‘ << zone. String[zone]; } 20 20
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 l 21
What responsibilities are Missing? The Time class needs int Hours() int Minutes() int Seconds() The Ext. Time class needs Zone. Type zone() 22
The End of Chapter 14 Part 1 23
- Slides: 23