C++ programmer language 6

46 0 0
                                    

----------------------- Page 1-----------------------

Module 10

Inheritance, Virtual Functions,  

and Polymorphism

Table of Contents

CRITICAL SKILL 10.1: Inheritance Fundamentals...........................................................................................2

CRITICAL SKILL 10.2: Base Class Access Control............................................................................................7

CRITICAL SKILL 10.3: Using protected Members...........................................................................................9

CRITICAL SKILL 10.4: Calling Base Class Constructors ................................................................................. 14

CRITICAL SKILL 10.5: Creating a Multilevel Hierarchy .................................................................................22

CRITICAL SKILL 10.6: Inheriting Multiple Base Classes................................................................................25

CRITICAL SKILL 10.7: When Constructor and Destructor Functions Are Executed.....................................26

CRITICAL SKILL 10.8: Pointers to Derived Types .........................................................................................27

CRITICAL SKILL 10.9: Virtual Functions and Polymorphism ........................................................................28

CRITICAL SKILL 10.10: Pure Virtual Functions and Abstract Classes ...........................................................37

This module discusses three features of C++ that directly relate to object-oriented programming:  

inheritance, virtual functions, and polymorphism. Inheritance is the feature that allows one class to  

inherit the characteristics of another. Using inheritance, you can create a general class that defines traits  

common to a set of related items. This class can then be inherited by other, more specific classes, each  

adding those things that are unique to it. Built on the foundation of inheritance is the virtual function.  

The virtual function supports polymorphism, the “one interface, multiple methods†philosophy of  

object-oriented programming.

1 C++ A Beginner’s Guide by Herbert Schildt

----------------------- Page 2-----------------------

CRITICAL SKILL 10.1: Inheritance Fundamentals

In the language of C++, a class that is inherited is called a base class. The class that does the inheriting is  

called a derived class. Therefore, a derived class is a specialized version of a base class. A derived class  

inherits all of the members defined by the base class and adds its own, unique elements.

C++ implements inheritance by allowing one class to incorporate another class into its declaration. This  

is done by specifying a base class when a derived class is declared. Let’s begin with a short example that  

illustrates several of the key features of inheritance. The following program creates a base class called  

TwoDShape that stores the width and height of a two-dimensional object, and a derived class called  

You've reached the end of published parts.

⏰ Last updated: Dec 22, 2010 ⏰

Add this story to your Library to get notified about new parts!

C++ programmer language 6Where stories live. Discover now