Chapter 15 What is Inheritance? - PowerPoint PPT Presentation

chapter 15 what is inheritance n.
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 15 What is Inheritance? PowerPoint Presentation
Download Presentation
Chapter 15 What is Inheritance?

play fullscreen
1 / 23
Download Presentation
Download Presentation

Chapter 15 What is Inheritance?

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. 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.

  2. Inherited Class

  3. Inheriting from Base Class

  4. 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

  5. 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.

  6. 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.

  7. Base Class Access

  8. 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.

  9. 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

  10. Cube Constructor Cube::Cube( float wide, float long, float high ) : Rect(wide, long) { height = high; volume = area * high; } // Cube::Cube

  11. Overriding Base Class Functions • A member function of a derived class may have the same name as a member function of a base class.

  12. 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

  13. Need for Virtual Member Functions

  14. 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.

  15. 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;

  16. 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

  17. 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.

  18. Chain of Inheritance

  19. Multiple Inheritance • Multiple inheritance is when a derived class has two or more base classes.

  20. Multiple Inheritance (cont)

  21. 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.

  22. 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

  23. 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.