Sams Teach Yourself C in 21 Days

(singke) #1
Working with C++ Classes and Objects 695

BD3


Person brad: Bradley Jones
age: 21
Person blank: blank blank
age: -1
Employee kyle: Kyle Rinne
age: 32
salary: 50000
Much of this listing is the same as the previous listing. You can see that the per-
sonclass has been declared just as it was in Listing B3.7. The new code has been
bolded to make it easier for you to see where the changes are.
In line 21 you see the first real changes. One of these is the declaration for the employee
class. This declaration is presented as
class employee : public person {
Just as in a regular class, the employee definition starts off with the keyword class fol-
lowed by the name of the new class—employee, in this case. What is new is the colon
and the text which follows. The colon indicates that employeeis a subclass. The name of
the base class to which employeeis a subclass is then presented—person, in this case.
Thepublickeyword provides access to the components of the personclass to the
employeeclass. If you were creating a subclass called student, you would start the defi-
nition off with
class student : public person {
Additionally, if you were going to use employeeas a base class to create a new class
calledtemp_employee, you would start the definition off with
class temp_employee : public employee {
Thetemp_employeeinherits from employee. You should note that because employee
inherits from person,temp_employeealso has access to the personmembers and fea-
tures. Figure B3.1 illustrates these inheritance structures.
Looking back at Listing B3.8, you can see that the declaration for the employeeclass is
relatively short. The employeeclass is short in this case because it also contains every-
thing in the personclass. Only those things that are new or different are added to this
class. In this case, that means a single data member called salary, two access functions
forsalary, and a constructor.

Implementing the Subclass Constructor
In lines 46–50 you see the employeeclass’s constructor. Again, you should notice that
this constructor is different from the personclass’s constructor. The first part of the con-
structor’s header is the same:

OUTPUT

ANALYSIS

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

Free download pdf