230 likes | 318 Views
5-Jul-2006. Today’s Objectives. 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
E N D
5-Jul-2006 Today’s Objectives • 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)
Object-Oriented Programming:Inheritance Chapter 12
Object-Oriented Programming: Inheritance Three Principle Features ofObject-Oriented Programming • Encapsulation • Inheritance • Polymorphism
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
Object-Oriented Programming: Inheritance (Deitel, 634–680) Inheritance • 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)
Object-Oriented Programming: Inheritance (Deitel, 634–680) UML Notation Base class Inheritance relationship Derived classes
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
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
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(){ Student alice("Alice"); 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
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: //... };
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{ //... };
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
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
Object-Oriented Programming: Inheritance (Deitel, 634–680) Calling a Base Class Functionin 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
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
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
Library Demo (Continued)
Object-Oriented Programming: Inheritance Library Demo • 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 { //... };
Object-Oriented Programming: Inheritance Library Demo • 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
Object-Oriented Programming: Inheritance Library Demo • 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"
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.