Download presentation
Presentation is loading. Please wait.
1
Today’s Objectives 5-Jul-2006 Announcements
Turn in HW #3 early for 10 bonus points on this HW Homework #4 is posted, it is due on July 17 Quiz #3 will be on Monday, July 10 – Class string, string stream processing, operator overloading, and inheritance Object-Oriented Programming: Inheritance (Ch. 12) Reusability Base classes and derived classes “is-a” relationship Using protected Constructors and destructors Other types of inheritance Using typedef Review of Homework #4 Library Demo (Continued)
2
Object-Oriented Programming: Inheritance
Chapter 12
3
Three Principle Features of Object-Oriented Programming
Object-Oriented Programming: Inheritance Three Principle Features of Object-Oriented Programming Encapsulation Inheritance Polymorphism
4
Reusability An important design goal of software engineering
Object-Oriented Programming: Inheritance (Deitel, 634–680, Goodrich) Reusability An important design goal of software engineering Advantages Saves time Better to use code that is already tested
5
Object-Oriented Programming: Inheritance (Deitel, 634–680)
Allows reuse of code that is common between related classes Create a base class that has the common code (also called a “parent” class) Create a derived class that inherits from the base class (also called a “child” class)
6
UML Notation Base class Inheritance relationship Derived classes
Object-Oriented Programming: Inheritance (Deitel, 634–680) UML Notation Base class Inheritance relationship Derived classes
7
“is-a” Relationship Represented by inheritance
Object-Oriented Programming: Inheritance (Deitel, 634–680) “is-a” Relationship Represented by inheritance We say that an object of the derived class “is-a” object of the base class Example: “A Student is-a Person” is always true However, an object of the base class is not an object of the derived class Example – We cannot say, “A Person is-a Student,” because that is not always true
8
Examples Base class Derived class Person Student Account
Object-Oriented Programming: Inheritance Examples Base class General Code used by all derived classes Derived class Specific Specialized code Person Student Account SavingsAccount GamePlayer Defender Shape Rectangle RentalItem Movie
9
Object-Oriented Programming: Inheritance (Deitel, 634–680)
Public Inheritance One of three types of inheritance – the others are “private” and “protected” Creates the “is-a” relationship Most commonly used type of inheritance class Student : public Person { //... }; Derived class Base class
10
Objects of the Derived Class
Object-Oriented Programming: Inheritance Objects of the Derived Class The derived class object automatically has all the data members and member functions that are in the base class //Derived class objects can use base class functions Student alice("Alice"); cout << alice.getName(); A derived-class object can be assigned to a base-class object Student bob("Bob"); Person personBob = bob; A derived-class object can be used as an argument when the function parameter is declared as a base-class object void notify( Person& p ){ /*...*/ } int main(){ notify(alice); } Instantiate an object of the derived class Use a base class member function Assigning an object of the derived class to a base class object A function with a base class parameter It can be called with a derived class object
11
Protected Members “protected” is a member-access specifier
Object-Oriented Programming: Inheritance (Deitel, 634–680) Protected Members “protected” is a member-access specifier Protected members can be accessed by members and friends of both the base class and the derived classes class Person{ protected: string name;//Accessible in derived classes public: //... };
12
Single and Multiple Inheritance
Object-Oriented Programming: Inheritance (Deitel, 634, 1213) Single and Multiple Inheritance Single inheritance When the derived class inherits from only one base class Multiple inheritance When the derived class inherits from more than one base class Derived class has characteristics of both bases Example: class TA : public Student, Faculty{ //... };
13
Derived Class Initializer
Object-Oriented Programming: Inheritance (Deitel, 634–680) Derived Class Initializer Should explicitly call the base class constructor Pass arguments to the base class constructor when appropriate Example: class Student : public Person{ public: Student(string nm=""):Person(nm){} //... }; Initializer list calls the constructor of the base class, and passes the argument to it Constructor of the derived class
14
Overriding a Base Class Function
Object-Oriented Programming: Inheritance (Deitel, 634–680) Overriding a Base Class Function A derived class can redefine a function that it inherited from the base class Example: class Game : public RentalItem { private: string platform; public: string toString(){ return title + "Platform: " + platform; } //... }; Redefinition of a member function of the base class
15
Calling a Base Class Function in the Derived Class
Object-Oriented Programming: Inheritance (Deitel, 634–680) Calling a Base Class Function in the Derived Class A base class function can be explicitly called from the derived class Use the binary scope operator class Game : public RentalItem { private: string platform; public: string toString(){ return RentalItem::toString()+"Platform: "+platform; } //... }; Explicit call to a member function of the base class
16
Constructors and Destructors
Object-Oriented Programming: Inheritance (Deitel, 634–680) Constructors and Destructors Sequence of calls when an object of a derived class is instantiated Base class constructor Derived class constructor Sequence of calls when an object of a derived class is destroyed Derived class destructor Base class destructor
17
Using typedef
18
Typedef Associates a name with a type
Using typedef (Deitel, 773) Typedef Associates a name with a type Can be used to create a shorter or more meaningful name Use the name like an alias typedef Book* BookPtr; BookPtr book1; //same as Book* book1 The alias
19
Library Demo (Continued)
20
Library Demo Object-Oriented Programming: Inheritance
Since one of the requirements states that the library shall have both books and journals, we can create a base class called LibraryItem and two derived classes, Book and Journal class Book : public LibraryItem { //... };
21
Library Demo Object-Oriented Programming: Inheritance
A derived class can redefine a function that it inherited from the base class class Journal : public LibraryItem { public: string toString(){ return LibraryItem::toString() + ", " + year; } //... Base class function Redefines the base class function
22
Library Demo Object-Oriented Programming: Inheritance
When a derived class object is assigned to a base class object, any members in the derived class that are not in the base class are lost Journal journal("Dr. Dobbs","2004"); cout << journal.toString(); //prints "Dr. Dobbs, 2004" LibraryItem item = journal; cout << item.toString(); //prints "Dr. Dobbs" prints "Dr. Dobbs" prints "Dr. Dobbs, 2004"
23
References Deitel, H. M., and P. J. Deitel, C++ How to Program, Fourth Edition. Upper Saddle River, NJ: Prentice Hall, 2003. Goodrich, M. T., R. Tamassia, and D. Mount, Data Structures and Algorithms in C++. Hoboken, NJ: John Wiley & Sons, Inc., 2004.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.