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


Next, you find the declaration

void headLights( Switch onOrOff ) ;

This member function is required so that the headlights for a Car object can turn on and off. This also provides additional functionality not provided by the base class Vehicle.

Following the declaration for headlight, you come upon the access specifier private. Within the private section, you find two attributes declared: degrees and drivingLights. The attribute degrees is used to hold the amount of degrees that the steering wheel is moved and the drivingLights attribute is used to hold the state of an object’s headlights. Access to these attributes is controlled by the two member functions previously discussed.

Let’s now look at the implementation file for Car. The first definition, the constructor, is as follows:

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

After the colon operator, you find a constructor call to the base class Vehicle within the initializer list. Whenever a derived class object is instantiated at runtime, the base class object is first constructed. Here, you are explicitly calling the base class constructor. After the constructor call, a comma-separated list of class data members is initialized. No additional functionality of the class is required, so the constructor’s body is empty.

Following the definition of the constructor, you come upon the function definition for setSpeed. Remember that this definition overrides the definition found in Vehicle. The reason you override the member function is that you have to control the upper limit of the object’s speed. Take a look at the following two statements within this member function:

Vehicle::setSpeed( speedIn ) ;
moving = true ;

In the member function, you first include some logic to verify the object’s speed. Next comes the call to the member function setSpeed found in the base class; this is done by specifying the name of the base class followed by the scope resolution operator. This call will set the speed attribute found in Vehicle. How about the assignment to the moving variable, you ask? No attribute named moving is in the declaration of Car. It turns out that this data member is declared in the base class Vehicle. Because you have inherited this data member and it has protected visibility, the member functions of this derived class can access the variable directly. Also notice the return statement; a call is made to Vehicle’s getspeed member function. This call must happen because the speed data member is inaccessible to Car.

Next, you find the definitions for the member functions Car::steer and Car::headLights. These two functions provide an implementation to steer the object and turn the driving lights on and off.

Now examine the file named MAIN.CPP. This file contains the main function for the program. The first two statement within main follow:

Car *sporty = new Car() ;
Car *luxury = new Car() ;

Each of these statements dynamically creates an object of type Car using the new expression.

The next two lines of code

sporty->setSpeed( 120 ) ;
luxury->setSpeed( 35 ) ;

call the member function setSpeed for each object. Remember that you provided functionality to limit the speed of an object; in a moment, you will see if this functionality works properly.

The following three lines

luxury->steer( 20 ) ; // turn to right
luxury->headLights( Vehicle::SWITCH_ON ) ;
luxury->accelerate( 15 ) ;

alter the state of the object luxury. The first statement steers the object to the right 20 degrees. The next statement turns the headlights on for the object. Note that you cannot use the constant SWITCH_ON directly; the class name and scope resolution operator must be used to access the value.

The next two statements use cout to print the speed of each vehicle. The output from sporty confirms the logic of the setSpeed member function. The best you can do is 80, even though you really want to go 120.

Next, you find an if-else statement pair, checking to see if the sporty object is moving. A call is made to the member function isMoving, yet this member function is not declared or defined in Car. This member function is found in the base class; because this member function is not overridden in Car, a call is made to the base class function.

Finally, the two delete expressions in the main function destroy the two Car objects.

Comments

You have seen a concrete example of inheritance in this How-To. The amount of code required to implement a Car has been minimized because the base class provides some default functionality. You did not have to reapply the functionality found in Vehicle to the Car class. This is the power of inheritance.

Object-oriented programming dictates more forethought concerning program design. You must think of the problem domain and flush out the abstractions. The first task is always to identify the classes that must support the application. Do you find that some classes exhibit common functionality between them? If so, you should abstract that common functionality into a base class and then inherit for specialization.

3.3 Apply the use of encapsulation in a C++ program?

Problem

I have read the term encapsulation and understand its meaning to some degree. I would like to see how this concept is implemented in C++.

Technique

You will examine the practice of encapsulation in this How-To. There are no hard-and-fast rules for encapsulation. This is the responsibility of the class designer. Ask six different class architects to provide a class declaration for a radio and you will get six different class declarations!

Encapsulation involves the collection of a class’s interface, its data members, and its implementation. A class that is properly encapsulated is said to be cohesive, or self-sufficient. The term data hiding is sometimes used as a synonym for encapsulation. The two terms are not the same, yet data hiding is an aggregate of encapsulation. Data hiding is one of the goals of encapsulation; it is the shielding of data from users of a class.


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.