Inheritance - Inheritance In this chapter, you will: Learn...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Inheritance In this chapter, you will: Learn about inheritance Learn about derived and base classes Explore how to redefine the member functions of a base class Examine how the constructors of base and derived classes work Learn how to construct the header file of a derived class Explore three types of inheritance: public, protected, and private Learn about composition Become familiar with the three basic principles of object-oriented design INHERITANCE AND COMPOSITION The two common ways to relate two classes in a meaningful way are: Inheritance ("is-a" relationship), and Composition ("has-a" relationship). INHERITANCE Design a class, partTimeEmployee, to implement and process the characteristics of a part-time employee. Every part-time employee has a name, pay rate, and number of hours worked. Every part-time employee is a person. Rather than design the class partTimeEmployee from scratch, we want to be able to extend the definition of the class personType by adding additional members (data and/or function). We do not want to make the necessary changes directly to the class personType-- that is, edit the class personType, and add and/or delete members. We want to create the class partTimeEmployee without making any physical changes to the class personType, by adding only the members that are necessary. The new class that we create from existing classes is called the derived class and the existing classes are called the base classes. >Single inheritance- the derived class is derived from a single base class. Multiple inheritance- the derived class is derived from more than one base class. Inheritance can be viewed as a tree-like, or hierarchical, structure wherein a base class is shown with its derived classes.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
class circle: public shape { . . . }; The word public in the heading, called the member access specifier, specifies that all public members of the class shape are inherited as public members by the class circle. class circle: private shape { . . . }; In this definition, the public members of shape become private members of the class circle. The previous definition of circle is equivalent to class circle: shape { . . . }; The general syntax of a derived class is class className: memberAccessSpecifier baseClassName { member list }; where memberAccessSpecifier is public, protected, or private. When no memberAccessSpecifier is specified, it is assumed to be a private inheritance. The following facts about the base and the derived classes should be kept in mind.
Background image of page 2
1. The private members of the base class are private to the base class; hence, the members of the derived class cannot directly access them. 2.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/18/2008 for the course CS 201 taught by Professor Markhieber during the Spring '08 term at University of Missouri-Kansas City .

Page1 / 12

Inheritance - Inheritance In this chapter, you will: Learn...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online