230 likes | 385 Views
Chapter 15 What is Inheritance?. Inheritance allows a new class to be based on an existing class. The new class inherits all the member variables and functions of the class it is based on. Note difference between is-a and has-a. A station-wagon is-a car, but a car has-a wheel.
E N D
Chapter 15 What is Inheritance? • Inheritance allows a new class to be based on an existing class. • The new class inherits all the member variables and functions of the class it is based on. • Note difference between is-a and has-a. • A station-wagon is-a car, but a car has-a wheel.
test.h - Program #ifndef TEST_H #define TEST_H #include "grade.h" class Test : public Grade { private: int numQuestions; float pointsEach; int numMissed; public: Test(int, int); }; // Test #endif
Why would you want? • In our set example, we could derive sets with various differences • traits, prints differently • new methods, isKind • keep track of number of traits • Animals, add/replace features. Logical categorization • Cars, add common characteristics, options • Allows customization, reuse of base class. • Keeps design consistent. All cars have dealerRetail. All cars have taxAmount.
Protected Members and Class Access • Protected members of a base class are like private members, but they may be accessed by derived classes. • The base class access specification determines how private, protected, and public base class members may be accessed by derived classes.
Constructors and Destructors • The base class’s constructor is called before the derived class’s constructor. • Since constructors have arguments, we are often not content with an argument-less call (the default). • The destructors are called in reverse order, with the derived class’s destructor being called first.
Passing Arguments to Base Class Constructors • Assume a class called Cube is derived from a class called Rect. Here is how the constructor looks in Rect: Rect::Rect ( float w, float l ) { width = w; length = l; area = length * width; } // Rect::Rect
Cube Constructor Cube::Cube( float wide, float long, float high ) : Rect(wide, long) { height = high; volume = area * high; } // Cube::Cube
Overriding Base Class Functions • A member function of a derived class may have the same name as a member function of a base class.
Polymorphism and Virtual Member Functions • The term polymorphism means the ability to take many form. • A virtual member function in a base class expects to be overridden in a derived class
Abstract Base Classes and Pure Virtual Functions • An abstract base class is not instantiated, but other classes are derived from it. • A pure virtual function is a virtual member function of a base class that must be overridden.
Abstract Base Classes and Pure Virtual Functions (cont) • A class becomes an abstract base class when it contains one or more pure virtual functions. • A pure virtual function is a virtual member function declared in a manner similar to the following:virtual void showInfo(void) = 0;
Base Class Pointers • Pointers to a base class may be assigned the address of a derived class object. • The pointer, however, ignores any overrides the derived class performs
Classes Derived from Derived Classes • A base class can also be derived from another class. • This gives us a “chain” of inheritance where one class is derived from a second, which in turn is derived from a third.
Multiple Inheritance • Multiple inheritance is when a derived class has two or more base classes.
Examples • Could have a fruit which falls under two categories: both fruit and vegetable. A tomato is sometimes referred to as a fruit and sometimes as a vegetable. • fruit - the ripened reproductive body of a seed plant • vegetable - edible seeds or roots or stems or leaves or bulbs or tubers or nonsweet fruits of any of numerous herbaceous plant • Cross-categorization. An apple is a fruit and an apple is a breakfast food.
Given the following, organize them into inheritance or composition • A test for 1720, a student in 1720 • An electronic device, a computer, a calculating tool • A woman, an employee, a person • A course, a course evaluation • Clothing, scarf, winter clothes, boots • Sesame Street Character, Big Bird, Oscar the Grouch
Concerns on multiple inheritance • Multiple inheritance is generally thought to be a bad idea because a common base class (B and C inherit from A, and D inherits from both B and C) cause problems with both B and C using the common variables differently. • Java handles with interface – function use is specified, but no implementation.