1 / 39

Inheritance

Inheritance. Briana B. Morrison Adapted from Alan Eugenio, William J. Collins, & Nell Dale. Inheritance. is a mechanism by which one class acquires (inherits) the properties (both data and operations) of another class the class being inherited from is the Base Class (Superclass)

thor
Download Presentation

Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance Briana B. Morrison Adapted from Alan Eugenio, William J. Collins, & Nell Dale

  2. Inheritance

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

  4. SUPERCLASS SUBCLASS Inheritance

  5. Inheritance

  6. vehicle wheeled vehicle boat car bicycle two-door four-door Inheritance Hierarchy Among Vehicles Every car is a wheeled vehicle. Inheritance

  7. Inheritance Relations Example • Inheritance involves sharing of attributes and operations among classes. A base class defines a set of common attributes and operations which it shares with derived classes. A derived class extends the resources provided by the base class by adding its own data members and member functions. Inheritance

  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 initHrs, int initMins, int initSecs ) ; //constructor Time () ; // default constructor private : int hrs ; int mins ; int secs ; } ; Inheritance

  9. Class Interface Diagram Timeclass Set Private data: hrs mins secs Increment Write Time Time Inheritance

  10. THE OPEN-CLOSED PRINCIPLE EVERY CLASS SHOULD BE OPEN: EXTENDIBLE THROUGH INHERITANCE CLOSED: STABLE FOR EXISTING APPLICATIONS Inheritance

  11. Using Inheritance to Add Features // 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 ) ; // constructor ExtTime ( ) ;// default constructor private : ZoneType zone ; // added data member } ; Inheritance

  12. class ExtTime: public Time • says class Time is a public base class of the derived class ExtTime • as a result, all public members of Time (except constructors) are also public members of ExtTime • in this example, new constructors are provided, new data member zone is added, and member functions Set and Write are overridden Inheritance

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

  14. Client Code UsingExtTime #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.Increment ( ) ; thisTime.Write ( ) ; // outputs 08:35:02 PST cout << endl ; Inheritance

  15. Access using Public Inheritance Inheritance

  16. Types of Inheritance Public: • public in base class means public in derived class • protected in base class means protected in derived class • hidden in derived class Protected: • public in base class means protected in derived class • protected in base class means protected in derived class • hidden in derived class Inheritance

  17. Types of Inheritance Private: • public in base class means private in derived class • protected in base class means private in derived class • hidden in derived class Inheritance

  18. Constructor Rules for Derived Classes • at run time, the base class constructor is implicitly called first, before the body of the derived class’s constructor executes • if the base class constructor requires parameters, they must be passed by the derived class’s constructor Inheritance

  19. 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 ; } Inheritance

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

  21. Implementation of ExtTime::Set function void ExtTime :: Set ( /* in */ int hours, /* in */ int minutes, /* in */ int seconds, /* in */ ZoneType time Zone ) // Precondition: 0 <= hours <= 23 && 0 <= minutes <= 59 // 0 <= seconds <= 59 && timeZone is assigned // Postcondition: // zone == timeZone && Time set by base class function { Time :: Set (hours, minutes, seconds); zone = timeZone ; } Inheritance

  22. 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] ; } Inheritance

  23. Composition (or Containment) • is a mechanism by which the internal data (the state) of one class includes an object of another class Inheritance

  24. ATimeCard object has aTime object #include “time.h” class TimeCard { public: void Punch ( /* in */ int hours, /* in */ int minutes, /* in */ int seconds ) ; void Print ( ) const ; TimeCard ( /* in */ long idNum, /* in */ int initHrs, /* in */ int initMins, /* in */ int initSecs ) ; TimeCard ( ) ; private: long id ; Time timeStamp ; } ; Inheritance

  25. Punch TimeCard Class TimeCardhas aTime object Private data: id timeStamp Print . . . Private data: hrs mins secs Set Increment Write . . . TimeCard TimeCard Inheritance

  26. Implementation of TimeCard Class Constructor TimeCard :: TimeCard ( /* in */ long idNum, /* in */ int initHrs, /* in */ int initMins, /* in */ int initSecs ) : timeStamp (initHrs, initMins, initSecs)// constructor initializer // Precondition: 0 <= initHrs <= 23 && 0 <= initMins <= 59 // 0 <= initSecs <= 59 && initNum is assigned // Postcondition: // id == idNum && timeStamp set by its constructor { id = idNum ; } Inheritance

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

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

  29. Inheritance

  30. Static Binding • is the compile-time determination of which function to call for a particular object based on the type of the formal parameter • when pass-by-value is used, static binding occurs Inheritance

  31. 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 ) ; Time is 08:30:00 ExtTime endTime (10, 45, 0, CST) ; Time is 10:45:00 Print ( startTime ) ; Print ( endTime ) ; Inheritance

  32. 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 • declaring a member function to be virtual instructs the compiler to generate code that guarantees dynamic binding Inheritance

  33. Virtual Member Function // SPECIFICATION FILE ( time.h ) class TimeType { public : . . . virtual void Write ( ) const ;// for dynamic binding . . . private : int hrs ; int mins ; int secs ; } ; Inheritance

  34. 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 ) ; Time is 08:30:00 ExtTime endTime (10, 45, 0, CST) ; Time is 10:45:00 CST Print ( startTime ) ; Print ( endTime ) ; Inheritance

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

  36. Polymorphism • Polymorphism allows two or more objects in an inheritance hierarchy to have identical member functions that perform distinct tasks. • C++ implements polymorphism by using dynamic binding of virtual member functions. This contrasts with static binding, which is the usual way of linking an object with a member function. Inheritance

  37. Pure Virtual Functions & Abstract Classes - A pure virtual function declared in a base class forces the definition of the function in a derived class. - The base class is known as an abstract class; it is a template or a mold for the construction of derived classes. - An abstract base class is used to specify the design of a class that is likely to have different implementations. Inheritance 37

  38. Summary Slide 1 §- Inheritance - combines with object composition to create the primary techniques for reusing software. - involves a class hierarchy whose elements are base and derived classes. - A base class provides member functions and data to a derived class. - The derived class might choose to add its own member functions or redefine existing ones in the base class. - expresses the "is a" relationship Inheritance

  39. Summary Slide 2 §- Polymorphism - object-oriented programming is refer to as "inheritance with runtime polymorphism." - allows two or more objects in an inheritance hierarchy to have operations with the same prototype that perform distinct tasks. - when calls by using a pointer or reference to an object, the runtime system determines the required version. This is known as dynamic binding and contrasts with static binding, in which the compiler determines which function should be called. - implemented by declaring functions using virtual. Inheritance

More Related