Download presentation
Presentation is loading. Please wait.
1
Inheritance CMSC 202, Version 4/02
2
Code Reuse Essential to the software development process
Do not have to “reinvent the wheel” Are using software components that have been debugged through previous use So far, we have seen the following examples of code reuse: Functions (function libraries) Classes (class libraries) Composition (aggregation) Inheritance is another tool that promotes code reuse. CMSC 202, Version 4/02
3
Object Relationships There are three basic relationships that objects can have: 1. “uses a” 2. “has a” 3. “is a” When an object uses another object by calling a public method of that object CMSC 202, Version 4/02
4
Object Relationships (con’t)
2. “has a” Examples: A course section has a student list A person has a birth date A movie has a start time Implemented using composition (aggregation) Graphically represented by an aggregation hierarchy CMSC 202, Version 4/02
5
Object Relationships (con’t)
3. “is a” One object is a more specialized version of another: A car is a vehicle A sorted list is a list A student is a person A professor is a faculty member A lecturer is a faculty member Implemented using inheritance Graphically represented by an inheritance hierarchy CMSC 202, Version 4/02
6
An Inheritance Hierarchy
UMBC COMMUNITY MEMBER STUDENT FACULTY MEMBER GRADUATE STUDENT UNDERGRAD STUDENT PROFESSOR LECTURER An Inheritance Hierarchy CMSC 202, Version 4/02
7
Inheritance Hierarchy
Objects at the top levels are more general in nature than the ones on the lower levels. The lower you go in the hierarchy, the more specialized the objects become. Inheritance is transitive across levels A “graduate student” is a “student.” A “student” is a “UMBC community member.” Therefore, a “graduate student” is also a “UMBC community member.” CMSC 202, Version 4/02
8
Inheritance and Classes
The more general class is referred to as a base class (or superclass). The more specialized class is referred to as a derived class (or subclass). A base class contains all that is common among its derived classes. The attributes (data) and behaviors (methods) of a base class are inherited by all its derived classes. Derived classes use, extend, modify, or replace the base class’ behaviors. CMSC 202, Version 4/02
9
Inheritance and OOP Inheritance is an abstraction for sharing similarities among classes while preserving their differences. Inheritance allows us to group classes into families of related types, allowing for the sharing of common operations and data. Multiple inheritance is possible, but not adviseable. (We will not address it.) Again, inheritance promotes code reuse. CMSC 202, Version 4/02
10
Inheritance in C++ C++ provides three types of inheritance
Public inheritance is used to implement the “is a” relationship. Private inheritance is use to implement the “is-implemented-in-terms-of” relationship, similar to composition. No one knows what protected inheritance is. Our discussions are limited to public inheritance CMSC 202, Version 4/02
11
Public Inheritance Assume that class D (“Derived”) publicly inherits from class B (“Base”). Every object of type D is a B, but not vice versa. D is a more specialized version of B. Anywhere an object of type B can be used, an object of type D can be used just as well, but not vice versa. (Adapted from: “Effective C++”, 2nd edition, page 155) CMSC 202, Version 4/02
12
// a military time class class Time { public:
A Simple Class Example // a military time class class Time { public: Time(int h = 0, int m = 0, int s = 0); void setTime (int h, int m, int s); void increment( ); void printTime ( ) const; private: int hrs; // int mins; int secs; }; CMSC 202, Version 4/02
13
Time::Time (int initHrs, int initMins, int initSecs)
Time Methods Time::Time (int initHrs, int initMins, int initSecs) : hrs (initHrs), mins (initMins), secs (initSecs) { // no code – uses member initialization list // should validate } void Time::setTime (int hours, int minutes, int seconds) { // should validate hrs = hours; mins = minutes; secs = seconds; CMSC 202, Version 4/02
14
void Time::increment ( ) // increments by one second { secs++;
Time Methods (con’t) void Time::increment ( ) // increments by one second { secs++; if (secs > 59) { secs = 0; mins++; if (mins > 59) { mins = 0; hrs++; if (hrs > 23) hrs = 0; } CMSC 202, Version 4/02
15
// print time as hh:mm:ss with leading zeroes
Time Methods (con’t) // print time as hh:mm:ss with leading zeroes void Time::printTime ( ) const { if (hrs < 10) cout << '0'; cout << hrs << ':'; if (mins < 10) cout << '0'; cout << mins << ':'; if (secs < 10) cout << secs; } CMSC 202, Version 4/02
16
A Derived Class: Extended Time
class ExtTime: public Time { public: enum ZoneType = {EST, CST, MST, PST, EDT, CDT, MDT, PDT}; ExtTime(int h = 0, int m = 0, int s = 0, ZoneType z = EST); void setExtTime (int h, int m, int s, ZoneType z); void printExtTime( ) const; private: ZoneType zone; }; CMSC 202, Version 4/02
17
What is Inherited? ExtTime publicly inherits from Time.
Time is the base class, ExtTime is the derived class. Inherited from base class Time: setTime( ), increment( ), printTime( ), hrs, mins, secs Not inherited: Time constructor (constructors cannot be inherited) Added by derived class ExtTime: setExtTime( ), printExtTime( ), zone CMSC 202, Version 4/02
18
protected: // potential encapsulation violation!
Protected Access A base class can give a derived class direct access to some members by placing them in the protected section. Only derived classes (and their friends) can access protected members. class Time { public: // same code as before protected: // potential encapsulation violation! int hrs, mins, secs; }; CMSC 202, Version 4/02
19
Access Summary Public in the base class Protected in the base class
Is public in the derived class Accessible by derived member functions, friends, and non-member functions Protected in the base class Is protected in the derived class Accessible by derived member functions and friends Private in the base class Is not accessible by the derived class Must use base class public or protected functions to access CMSC 202, Version 4/02
20
The ExtTime constructor calls the Time (base class)
constructor using a member initialization list. Without this, Time’s default constructor would be called. Note that theTime constructor is called first, then the ExtTime constructor body is executed. (Destruction would take place in reverse order.) ExtTime::ExtTime (int Hrs, int Mins, int Secs, ZoneType Zone) : Time (Hrs, Mins, Secs) { zone = Zone; } CMSC 202, Version 4/02
21
ExtTime::setExtTime setExtTime calls Time::SetTime to set (private) hrs, mins, and secs inherited from Time. void ExtTime:: setExtTime(int h, int m, int s, ZoneType z) { SetTime (h, m, s); zone = z; } CMSC 202, Version 4/02
22
ExtTime::printExtTime
void ExtTime::printExtTime ( ) const { static char* zoneString[8] = “EST", "CST", "MST", "PST", "EDT", "CDT", "MDT", "PDT" }; printTime ( ); // a base class method cout << ' ' << zoneString[zone]; } CMSC 202, Version 4/02
23
Using ExtTime int main ( ) { ExtTime eTime (12, 0, 0); // noon, EST eTime.printExtTime ( ); // ExtTime method eTime.increment ( ); // an inherited method eTime.printTime ( ); // just prints hh:mm:ss eTime.setExtTime (13, 12, 7, ExtTime::PST); eTime.printExtTime ( ); // ExtTime method return 0; } CMSC 202, Version 4/02
24
Using Time and ExtTime int main( ) { Time t(5, 14, 10), *tPtr = &t;
ExtTime et1(3, 7, 12), et2(14, 9, 20), *etPtr = &et1; t = et1; // t is now 05:14:10 et2 = t; // syntax error! tPtr = &et1; // base class pointer points to derived object tPtr->printTime( ); // prints 03:07:12 etPtr = &t; // syntax error! return 0; } CMSC 202, Version 4/02
25
Mixing Base and Derived Class Objects and Pointers
A derived class object may be assigned to a base class object because a derived object “is a” base object. Member slicing will occur. However, a base class object may not be assigned to a derived class object. A base class pointer may point to a derived class object. However, a derived class pointer may not (without casting) point to a base class object. CMSC 202, Version 4/02
26
Overloaded Assignment Operator
Note that an overloaded base class assignment operator (operator=) is not inherited and not called automatically from the derived class overloaded assignment operator. CMSC 202, Version 4/02
27
Time::operator= Time& Time::operator= (const Time& rhs)
{ if (this != &rhs) // self-assignment check { hrs = rhs.hrs; mins = rhs.mins; secs = rhs.secs; } return *this; // enables x = y = z CMSC 202, Version 4/02
28
Erroneous ExtTime::operator=
ExtTime& ExtTime:: operator=(const ExtTime &rhs) { if (this != &rhs) zone = rhs.zone; return *this; } CMSC 202, Version 4/02
29
What Happens? ExtTime eTime1 (12, 30, 0, ExtTime::EST);
ExtTime eTime2 (13, 45, 30, ExtTime::PDT); // assignment only changes the zone eTime2 = eTime1; // now eTime2 is 13:45:30 EST CMSC 202, Version 4/02
30
Correct ExtTime::operator=
ExtTime& ExtTime:: operator=(const ExtTime &rhs) { if (this != &rhs) { // explicitly call base class op= Time::operator= (rhs); zone = rhs.zone; } return *this; } CMSC 202, Version 4/02
31
Notes on Inheritance Base class constructors and destructors are not inherited. The base class constructor (destructor) will automatically be invoked when the derived class is constructed (destroyed). The base class constructor will be called first. The derived class destructor will be called first (objects are destroyed in the reverse order they are constructed). Base class operator= is not inherited and not automatically called by derived class operator= Friendship is not inherited. That is, a friend of the base class is not automatically a friend of the derived class. (Friendship must be granted.) CMSC 202, Version 4/02
32
Specialization by Overriding
An inheritance hierarchy shows how more specialized classes are derived from more general classes. We have already seen specialization through the addition of new methods and data members in a derived class (ExtTime). Another mechanism for specialization is overriding base class member functions. This is achieved by defining a derived class method with the exact same signature as a base class method. The derived class method “hides” the base class method from the user. CMSC 202, Version 4/02
33
Overriding is Not Overloading
Recall that overloading a function means using the same function name, but with different parameters. Overriding a function means redefining a function using the same name and parameters (i.e., signature). (This is possible only with inheritance.) CMSC 202, Version 4/02
34
Overloading Example In our Time/ExtTime example, ExtTime extended Time by adding new methods and data members. But suppose ExtTime was defined a little differently. Suppose instead of having printExtTime( ), the ExtTime class redefined printTime( ). CMSC 202, Version 4/02
35
Revised ExtTime Class With Function Overriding
class ExtTime: public Time { public: enum ZoneType = {EST, CST, MST, PST, EDT, CDT, MDT, PDT}; ExtTime(int h = 0, int m = 0, int s = 0, ZoneType z = EST); void setExtTime (int h, int m, int s, ZoneType z); void printTime( ) const; // function overriding private: ZoneType zone; }; CMSC 202, Version 4/02
36
ExtTime::printTime( )
void ExtTime::printTime ( ) const { static char* zoneString[8] = {“EST”, “CST”, “MST”, “PST”, “EDT”, “CDT”, “MDT”, “PDT”}; Time::printTime( ); // explicit call cout << ‘ ‘ << zoneString[zone]; } CMSC 202, Version 4/02
37
eTime.Time::printTime( ); // using Time’s printTime() return 0; }
main( ) must call Time::printTime( ) explicitly because it is lexically “hidden” by ExtTime::printTime( ). int main ( ) { ExtTime eTime (12, 0, 0); // noon, EST eTime.increment( ); // an inherited method eTime.printTime( ); // overriding eTime.Time::printTime( ); // using Time’s printTime() return 0; } CMSC 202, Version 4/02
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.