UMBC CMSC202, Computer Science II, Spring, 1999 Sections 0201 and 0202

Introduction

[Next Lecture] [Previous Lecture]

Topics Covered:

Files used in this set of lectures public and private derivations
pointers to objects

Derivations

The main idea of deriving a class from a base class is that the base class' functionality is given to the derived class, with very little work on the part of the programmer. From the perspective of object orientation, a derived class either extends the functionality of the base class or it specializes the base class.

C++ distinguishes between 3 kinds of derivations: public, private, and protected The differences between them are summarizd in this table:(courtesy Richard Chang)

  In the base class, the inherited member is a:
Access From Public Member Protected Member Private Member
Members & friends of
a derived class
Yes Yes No
Members & friends of
a subsequently derived class
if previous derivations
are public or protected
if previous derivations
are public or protected
No
Other functions,
using a derived object
if derivation
is public
No No
Another way to describe this is:
In a publicly derived class, public members in the base class are public in the derived class; private members of the base class are inaccessable in teh derived class, and protected members of the base class are protected in teh derived class.
In a private derivation, public and protected members of the base class are private i the derived class, and private members of the base class are inaccessable in teh derived class.
In a protected derivation, public and protected members of the base class are protected in the derived class, and private members of the base class are inaccessable to members of the derived class.

Friend functions of a base class are not friends of the derived class

Consider the package class, package.h (links to all files are given above). It is a private derivation from box, which means that the function grow, which was public in box, cannot be used by nonmember functions accessing packages. This prevents us from having a box that is inside another one change its dimensions, and violating the idea that a package can hold only other packages which are smaller than it.

Pointer to derived object

These create their own intersting issues. Consider the base class Person and the derived classes Student and Faculty: because each of the derived classes has appendant members, they are of different sizes than the base class. This can create some problems if we are not careful.

The first program main1.C has a function foo which takes a person& as an argument and a function bar, taking a Person*. (since student and faculty are both also of type person, we can pass them to foo and to bar with no typecasts - this is another way of saying that a derived class is compatible with the base class) . (a href="lec19/derive2/main1.txt"> output

the second program shows that pointer assignment also works in that we can cause a pointer to a base class to point to an object of the derived class; but the call ptr->id() gets the base class's id always, because the pointer dereferences as a person typeoutput

We can show that a pointer to a pointer to a person is not assignment compatible with a pointer to pointer to student - because if it were, I could do some weird stuff:

   Person **p1, **p2;
   Student S1("Joe", 19, 2, 3.79);
   Faculty F1("Dr. Who", 49, 15);

   Student * sp1 = &S1;  //points to student
   Faculty * fp1 = &F1;  // points to faculty;

   p1 = &sp1;   //p1 points to student pointer
   p2 = &fp1;   //p2 points to faculty pointer

   *p1 = *p2;  //replace what p1 points to by what p2 points  to
now, sp1 points to a faculty object, but this is not good. student and faculty objects occupy differnet amounts of storage, and this assignment can get us into some serious trouble.

Program 4 has a function SortByAge, which uses selection sort to sort an array of persons.
output

Suppose we use SortByAge to sort an array of students, The programand its output show the effect of trying to use SortbyAge to sort an array of students. Even though student pointers are assignment compatible with person pointers, the size of the objects being pointed to are different, and when we try to move these objects about in memory, we get into trouble. The address of the second object in an array of persons is A+sizeof(Person), which is not equal to A+sizeof(student). If we make SortbyAge sort an array of person pointers, as in program 6, and then typecast an array of pointer to students as person pointers. Even though the output shows that we can do it, it is considered bad programming practice ot do this kind of finagling.

If we try a slightly different apporach, making an array of person pointers point to students (as in thsi example, we can sort the array of students, but not id them as students. (a href="lec19/derive2/main7.txt">Output

The way around all these difficulties is in creating a virtual base clss. person2.h shows how this is done. Notice that the id function is declared as virtual in the base class, and when the compiler dereferences a pointer to a person, it figures out what kind of person it is pointing to - person, student, faculty, and then invokes the id function appropriate to that type. code and Output


Last Modified: 25 Apr 1999 21:26:16 EDT by Mitch Edelman edelman@cs.umbc.edu

Back up to Spring 1999 CMSC 202 Section Homepage