Sams Teach Yourself C in 21 Days

(singke) #1
In programming terms, it is said that an employee inherits the characteristics of a person.
Additionally, a student inherits the characteristics of a person. Figure B3.1 illustrates the
relationship between these three potential classes.
If you create classes to store a persons, employees, and students, you can make a couple
of additional statements. You can say that


  • Person is a base class.

  • Employee and student are subclasses.

  • There is no relationship between employee and student, although they use—
    descend from—the same base class.


690 Bonus Day 3

Abase classis simply a class that is inherited from by another class.
Note

Asubclassis simply a class that inheritsfrom another class.
Note

Building a Base Class for Inheritance ..........................................................


The best way to understand the power of inheritance is to see it in action. Before you can
inherit, you need a base class from which to inherit. You can build a base class just as
you built classes earlier today. The base class for today’s example is the person class.
Listing B3.7 presents a base class, as well as some code to see it in use.

LISTINGB3.7 base.cpp. The person class set up to be used as a base class
1: #include <iostream.h>
2: #include <string.h>
3:
4: #define MAX_LEN 81
5:
6: class person {

FIGUREB3.1
The relationship
between the person,
student, and employee
classes.

person

employee student

38 448201x-Bonus3 8/13/02 11:19 AM Page 690

Free download pdf