chapter 15 what is inheritance?
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
1
Starting Out with C++, 3rd Edition
Chapter 15 What is Chapter 15 What is Inheritance?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
Starting Out with C++, 3rd Edition
Inherited ClassInherited Class
3
Starting Out with C++, 3rd Edition
Inheriting from Base ClassInheriting from Base Class
4
Starting Out with C++, 3rd Edition
test.htest.h - - Program 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
Starting Out with C++, 3rd Edition
Why would you want?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
Starting Out with C++, 3rd Edition
Protected Members and Protected Members and Class AccessClass 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
Starting Out with C++, 3rd Edition
Base Class AccessBase Class Access
8
Starting Out with C++, 3rd Edition
Constructors and Constructors and DestructorsDestructors
• 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
Starting Out with C++, 3rd Edition
Passing Arguments to Passing Arguments to Base Class ConstructorsBase 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
Starting Out with C++, 3rd Edition
Cube ConstructorCube Constructor
Cube::Cube( float wide, float long, float high ) : Rect(wide, long)
{ height = high;volume = area * high;
} // Cube::Cube
11
Starting Out with C++, 3rd Edition
Overriding Base Class Overriding Base Class FunctionsFunctions
• A member function of a derived class may have the same name as a member function of a base class.
12
Starting Out with C++, 3rd Edition
Polymorphism and Virtual Polymorphism and Virtual Member FunctionsMember 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
Starting Out with C++, 3rd Edition
Need for Virtual Member Need for Virtual Member FunctionsFunctions
14
Starting Out with C++, 3rd Edition
Abstract Base Classes and Abstract Base Classes and Pure Virtual FunctionsPure 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
Starting Out with C++, 3rd Edition
Abstract Base Classes and Abstract Base Classes and Pure Virtual Functions Pure Virtual Functions (cont)(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
Starting Out with C++, 3rd Edition
Base Class PointersBase 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
Starting Out with C++, 3rd Edition
Classes Derived from Classes Derived from Derived ClassesDerived 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
Starting Out with C++, 3rd Edition
Chain of InheritanceChain of Inheritance
19
Starting Out with C++, 3rd Edition
Multiple InheritanceMultiple Inheritance• Multiple inheritance is when a
derived class has two or more base classes.
20
Starting Out with C++, 3rd Edition
Multiple Inheritance Multiple Inheritance (cont)(cont)
21
Starting Out with C++, 3rd Edition
ExamplesExamples• 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
Starting Out with C++, 3rd Edition
Given the following, organize Given the following, organize them into inheritance or them into inheritance or
compositioncomposition• 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
Starting Out with C++, 3rd Edition
Concerns on multiple Concerns on multiple inheritanceinheritance
• 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.