Click Here!
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


Steps

1.  Create a new subdirectory named PCAR and change to that subdirectory. Copy the files named VEHICLE.H and VEHICLE.CPP from the VEHICLE subdirectory (used in How-To 3.2).
2.  Open up the header file VEHICLE.H in your editor and add the virtual keyword as shown in the following (the changes are bolded):
// vehicle.h - this file contains a class
// declaration for the vehicle type.

class Vehicle
{
public:
    enum Switch { SWITCH_ON, SWITCH_OFF } ;
    Vehicle( ) ;
    virtual ~Vehicle( ) ;

    virtual bool powerSwitch( Switch onOrOff ) ;
    virtual unsigned int accelerate( unsigned int amount ) ;
    virtual unsigned int decelerate( unsigned int amount ) ;
    virtual bool isMoving( void ) const ;
    virtual unsigned int getSpeed( void ) const ;
    virtual unsigned int setSpeed( unsigned int speedIn ) ;
protected:
    bool moving ;
private:
    unsigned int speed ;
} ;

Please note that if your compiler does not support the bool data type, you can add the following line of code at the top of this header file:
enum bool { false = 0, true } ;
3.  Create a new file called CAR.H and type the following class declaration:
// car.h - this file contains a class declaration
// for the Car type, derived from Vehicle
#include “vehicle.h”

class Car : public Vehicle
{
public:
    enum { MAX_SPEED = 80 } ;
    Car( ) ;
    ~Car( ) ;

    unsigned int setSpeed( unsigned int speedIn ) ;
    virtual void steer( int degrees ) ;
    virtual void headLights( Switch onOrOff ) ;
private:
    int degrees ;
    Switch drivingLights ;
} ;
4.  Create a file named CAR.CPP and type the following code:
// car.cpp implementation file
// for a Car; derived from Vehicle
#include “car.h”

Car::Car( ) : Vehicle(),
    degrees( 0 ), drivingLights( SWITCH_OFF )
{
}

Car::~Car( )
{
    degrees = 0 ;
    drivingLights = SWITCH_OFF ;
}

unsigned int Car::setSpeed( unsigned int speedIn )
{
    if( speedIn > MAX_SPEED )
        speedIn = MAX_SPEED ;
    Vehicle::setSpeed( speedIn ) ;
    moving = true ;

    return( Vehicle::getSpeed() ) ;
}

void Car::steer( int degrees )
{
    if( moving )
    {
        if( degrees == 0 )
            this->degrees = 0 ;
        else
            this->degrees += degrees ;
    }
}

void Car::headLights( Switch onOrOff )
{
    drivingLights = onOrOff ;
}
5.  Save CAR.CPP and create a file named MAIN.CPP. This file should contain the following:
#include <iostream>
using namespace std ;
#include “car.h”

int main( )
{
    Vehicle *sporty = new Car() ;
    Car *luxury = new Car() ;

    sporty->setSpeed( 120 ) ;
    sporty->decelerate( 20 ) ;
    luxury->setSpeed( 35 ) ;
    luxury->steer( 20 ) ;
    luxury->headLights( Vehicle::SWITCH_ON ) ;
    luxury->accelerate( 15 ) ;

    cout << “sporty’s speed : ” << sporty->getSpeed() << endl ;
    cout << “pLuxury’s speed: ” << luxury->getSpeed() << endl ;

    if( sporty->isMoving( ) )        cout << “sporty is moving” << endl ;    else        cout << “sporty isn’t moving” << endl ;

    delete luxury ;
    delete sporty ;

    return 0 ;
}
6.  Save MAIN.CPP and return to the command line. Next, compile and link MAIN.CPP, VEHICLE.CPP, and CAR.CPP as shown in the following:
gcc -c main.cpp vehicle.cpp car.cpp

If your compiler complains about the statement
#include <iostream>

comment out the using declaration following it and change the #include line to
#include <iostream.h>
7.  Run the program; the output should be as follows:
sporty’s speed : 60
pLuxury’s speed: 50
sporty is moving

If you are using the DJGPP compiler, you might have to rename the executable from A.OUT to MAIN.EXE to execute the program.

How It Works

In the file VEHICLE.CPP, you prefixed the virtual keyword to Vehicle’s destructor and six member functions. Only member functions and class destructor can be virtual; class data members can not be virtual. The C++ language uses the virtual keyword to specify a member function that is dynamically bound at runtime. Non-virtual member functions are statically bound; this means that the compiler knows the proper function to call at compile time. The call is made depending on the type of the reference. Conversely, a virtual member function call is determined at runtime. The member function that is called depends on the actual type of the object. Even if a pointer to the object is a base pointer of the object, the proper function will be called.

The virtual behavior of a member function is inherited by derived classes. If a member function is redeclared in a derived class, and that member function is declared virtual in the base class, the derived class member function is also virtual. Some class designers insist that the virtual keyword be applied to virtual member functions in derived classes. This is a visual reminder to developers that the member functions are indeed virtual.

Notice that in VEHICLE.H the virtual keyword is prefixed to each of the public member functions and the destructor. It is especially important that you apply the virtual keyword to the destructor of a class that will be inherited from. This guarantees the proper chain of destruction for the derived class object and for each base class object.

Now turn your attention to the declaration of the Car class found in CAR.H. The Car class is inherited from the Vehicle class. In the public section, you will find declarations for the constructor and destructor of the class.

The next declaration in the class

unsigned int setSpeed( unsigned int speedIn ) ;

redeclares the member function setSpeed found in the base class Vehicle. Remember that setSpeed is declared with the virtual keyword in Vehicle, so Car’s setSpeed function is also virtual.

The next two member function declarations

virtual void steer( int degrees ) ;
virtual void headLights( Switch onOrOff ) ;


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.