Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Chapter 14-1 Object- Oriented Software Development Dale/Weems."— Presentation transcript:

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

2 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 3 Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT Operations Data

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

5 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 6 What is an object? OBJECT Operations Data set of methods (public member functions) internal state (values of private data members)

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

8 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 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 10 Class Interface Diagram Private data: hrs mins secs Set Increment Write Time Time class

11 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 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 13 Class Interface Diagram Private data: hrs mins secs ExtTime class Set Increment Write Time Set Increment Write ExtTime Private data: zone

14 14 Accessibility l 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

15 15 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; 15

16 16 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

17 17 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; }

18 18 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 18

19 19 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 (initHrs, initMins, initSecs); zone = initZone; } 19

20 20 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]; } 20

21 21 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

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

23 23 The End of Chapter 14 Part 1


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

Similar presentations


Ads by Google