Virtual Functions and Extended Type Compatibility
If Derived is a derived class of the base class Base, then you can assign an object of type Derived to a variable (or parameter) of type Base, but not the other way around. If you consider a concrete example, this becomes sensible. For example, DiscountSale is a derived class of Sale. (Refer to Displays 1 and 3.) You can assign an object of the class DiscountSale to a variable of type Sale, since a DiscountSale is a Sale. However, you cannot do the reverse assignment, since a Sale is not necessarily a DiscountSale. The fact that you can assign an object of a derived class to a variable (or parameter) of its base class is critically important for reusing of code via inheritance. However, it does have its problems.
For example, suppose a program or unit contains the following class definitions:
class Pet
{
public:
string name;
virtual void print( ) const;
};
class Dog : public Pet
{
public:
string breed;
virtual void print( ) const; //keyword virtual not needed,
//but put here for clarity.
};
Dog vdog;
Pet vpet;
Now concentrate on the data members, name and breed. (To keep this example simple, we have made the member variables public. In a real application, they should be private and have functions to manipulate them.)
Anything that is a Dog is also a Pet. It would seem to make sense to allow programs to consider values of type Dog to also be values of type Pet and hence the following should be allowed:
vdog.name = "Tiny";
vdog.breed = "Great Dane";
vpet = vdog;
C++ does allow this sort of assignment. You may assign a value, such as the value of vdog to a variable of a parent type, such as vpet (but you are not allowed to perform the reverse assignment). Although the above assignment is allowed, the value that is assigned to the variable vpet loses its breed field. This is called the slicing problem. The following attempted access will produce an error message:
cout << vpet.breed;
// Illegal: class Pet has no member named breed
You can argue that this makes sense, since once a Dog is moved to a variable of type Pet it should be treated like any other Pet and not have properties peculiar to Dogs. This makes for a lively philosophical debate, but it usually just makes for a nuisance when programming. The dog named Tiny is still a Great Dane and we would like to refer to its breed, even if we treated it as a Pet someplace along the way.
Fortunately, C++ does offer us a way to treat a Dog as a Pet without throwing away the name of the breed. To do this, we use pointers to dynamic variables.
Suppose we add the following declarations:
If we use pointers and dynamic variables we can treat Tiny as a Pet without losing his breed. The following is allowed.
pdog = new Dog;
pdog->name = "Tiny";
pdog->breed = "Great Dane";
ppet = pdog;
Moreover, we can still access the breed field of the node pointed to by ppet. Suppose that
has been defined as follows:
void Dog::print( ) const
{
cout << "name: " << name << endl;
cout << "breed: " << breed << endl;
}
The statement
will cause the following to be printed on the screen:
name: Tiny
breed: Great Dane
This nice output happens by virtue of the fact that print( ) is a virtual member function. (No pun intended.) We have included test code in Display 7.
Display 7—Defeating the Slicing Problem
//Program to illustrate use of a virtual function to defeat the slicing problem.
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
class Pet
{
public:
//We have made the member variables public to keep the example simple. In a
//real application they should be private and accessed via member functions.
string name;
virtual void print( ) const;
};
class Dog : public Pet
{
public:
string breed;
virtual void print( ) const;
};
int main( )
{
Dog vdog;
Pet vpet;
vdog.name = "Tiny";
vdog.breed = "Great Dane";
vpet = vdog;
cout << "The slicing problem:
";
//vpet.breed; is illegal since class Pet has no member named breed.
vpet.print( );
cout << "Note that it was print from Pet that was invoked.
";
cout << "The slicing problem defeated:
";
Pet *ppet;
ppet = new Pet;
Dog *pdog;
pdog = new Dog;
pdog->name = "Tiny";
pdog->breed = "Great Dane";
ppet = pdog;
//These two print the same output:
//name: Tiny
//breed: Great Dane
ppet->print( );
pdog->print( );
//The following, which accesses member variables directly
//rather than via virtual functions would produce an error:
//cout << "name: " << ppet->name << " breed: "
// << ppet->breed << endl;
return 0;
}
void Dog::print( ) const
{
cout << "name: " << name << endl;
cout << "breed: " << breed << endl;
}
void Pet::print( ) const
{
cout << "name: " << name << endl;
}
Sample Dialogue
The slicing problem:
name: Tiny
Note that it was print from Pet that was invoked.
The slicing problem defeated:
name: Tiny
breed: Great Dane
name: Tiny
breed: Great Dane
Object-oriented programming with dynamic variables is a very different way of viewing programming. This can all be bewildering at first. It will help if you keep two simple rules in mind:
-
If the domain type of the pointer pAncestor is an ancestor class for the domain type of the pointer pDescendent, then the following assignment of pointers is allowed:
pAncestor = pDescendent;
Moreover, none of the data members or member functions of the dynamic variable being pointed to by pDescendent will be lost.
-
Although all the extra fields of the dynamic variable are there, you will need virtual member functions to access them.
Pitfall: The Slicing Problem
Although it is legal to assign a derived class object into a base class variable, assigning a derived class object to a base class object slices off data. Any data members in the derived class object that are not also in the base class will be lost in the assignment, and any member functions that are not defined in the base class are similarly unavailable to the resulting base class object.
For example, if Dog is a derived class of Pet, then the following is legal:
Dog vdog;
Pet vpet;
vpet = vdog;
However, vpet cannot be a calling object for a member function from Dog unless the function is also a member function of Pet, and all the member variables of vdog that are not inherited from the class Pet are lost. This is the slicing problem.
Note that simply making a member function virtual does not defeat the slicing problem. Note the following code from Display 7:
Dog vdog;
Pet vpet;
vdog.name = "Tiny";
vdog.breed = "Great Dane";
vpet = vdog;
. . .
vpet.print( );
Although the object in vdog is of type Dog, when vdog is assigned to the variable vpet (of type Pet) it becomes an object of type Pet. So, vpet.print( ) invokes the version of print( ) defined in Pet, not the version defined in Dog. This happens despite the fact that print( ) is virtual. In order to defeat the slicing problem, the function must be virtual and you must use pointers and dynamic variables.