Sams Teach Yourself C in 21 Days

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

BD3


Inheriting from a Base Class ........................................................................


Now that you have a base class, you are ready to inherit from it. Listing B3.8 presents an
employeeclass that inherits from the personclass you just created. The employeeis a
subclass of person.

To help you out, the bold lines in Listing B3.8 point out what has been
Note added to the code from Listing B3.7.

LISTINGB3.8 employee.cpp. Inheriting from the person class
1: // Inheritance illustrated
2: #include <iostream.h>
3: #include <string.h>
4:
5: #define MAX_LEN 81
6:
7: class person {
8: protected:
9: char fname[MAX_LEN];
10: char lname[MAX_LEN];
11: int age;
12: public:
13: void set_fname(char fn[] ) { strcpy(fname, fn); };
14: void set_lname(char ln[] ) { strcpy(lname, ln); };
15: void set_age( int a ) { age = a ; };
16: char *get_name(char *fullname);
17: int get_age( void ) { return age; };
18: person(char fn[] = “blank”, char ln[] = “blank”);
19: };
20:
21: class employee : public person {
22: protected:
23: long salary;
24: public:
25: void set_salary(long sal) { salary = sal; };
26: long get_salary() { return salary; };
27: employee(char fn[] = “eblank”, char ln[] = “eblank”);
28: };
29:
30: person::person( char fn[], char ln[] )
31: {
32: strcpy(fname, fn);
33: strcpy(lname, ln);
34: age = -1;

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

Free download pdf