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


The member initializer list follows the colon operator and precedes the constructor’s opening brace. The initializer list can only be used with constructors and is the preferred method of initializing class data members. Notice that you do not use the assignment operator to assign a value to a data member; only constructor notation is allowed within the initializer list. Any number of program statements can appear within the constructor’s body; in this example, no further functionality is required.

The next function definition in the implementation file is that of the destructor. Remember that the destructor is called for any instance of a class that is about to be destroyed. Within the body of a destructor is any logic required to properly de-initialize an instance of a class. For example, if your class allocates memory within the constructor, you should deallocate the memory in the destructor. The rule of thumb is to release any resources used by the class within the destructor. If an instance of a class is dynamically allocated and subsequently deallocated with delete, the destructor of the object is first called, and then the memory for the object is reclaimed.

The definition of powerSwitch follows the definition of the destructor. Notice that, besides constructors and destructors, the name of a member function is preceded by the name of the class and the scope resolution operator. Be sure that the return type is specified along with any arguments to the member function.

After the definition for powerSwitch are definitions for the functions accelerate, decelerate, isMoving, getSpeed, and setSpeed. A brief description of each function follows.

The member functions accelerate and decelerate are used to increase and decrease the speed of a Vehicle instance, respectively. The isMoving member function merely returns the value contained in the instance variable moving. The getSpeed member function does the same for the variable speed. Users of the class utilize the setSpeed member function to change the value of the variable speed.

Let’s turn our attention to the third source file: MAIN.CPP. This file contains the function main. The first two statements within main’s body follow:

Vehicle  vehicle ;
Vehicle *pv = new Vehicle() ;

The first statement instantiates an object of type Vehicle on the stack. The second statement uses the new expression to create a Vehicle object from the free store. The result of the allocation is a pointer of type Vehicle; this pointer points to the newly created object. There is a fundamental difference between the two. The lifetime of the object Vehicle will expire when it leaves the block from which it is created. The object created with the new expression continues to exist until delete is called for the pointer to the object.

The next two statements

vehicle.setSpeed( 10 ) ;
pv->setSpeed( 20 ) ;

are a call to the setSpeed member function for each object. The first method uses the member dot operator and the second uses the arrow member access operator. The dot operator is used to access a member of a class instantiated on the stack. The arrow member access operator is used to access a member of a class (through a pointer) that is dynamically allocated.

The two source statements that follow:

cout << “vehicle’s speed: ” << vehicle.getSpeed() << endl ;
cout << “pv’s speed     : ” << pv->getSpeed() << endl ;

simply print out the value of the speed instance variable for each object. The value is returned by the member function getSpeed.

The next-to-last statement in main is

delete pv ;

and is called to delete the dynamically allocated object that is pointed to by pv. Remember that if delete is called for a dynamically created object, the destructor is called first, and then the memory for the object is reclaimed.

It is not explicitly shown, but the vehicle object will be destroyed at the end of the main function; the sequence of events will result in a call being made to the destructor. Following that, the object will be destroyed.

Comments

Although this class is not very interesting with respect to functionality, the class does serve to demonstrate the basics of a C++ user-defined type.

A user-defined declaration begins with the keyword class (or struct), followed by a name for the class and an opening and closing brace, and ends with a semicolon. Declarations within the class body default to private access for class declarations and public for struct declarations. Any members declared with protected access are directly accessible to members of derived classes, yet are inaccessible to users of the class.

Place your data members in the private section and provide accessor and mutator member functions so clients of the class can access those data members. Be cautious because some data members should be completely shielded from client access. Only provide accessors and mutators where absolutely required.

Remember that you should place your class declaration in a header file and the implementation in a separate file, normally with a .cpp extension. Common practice is to put each class declaration in its own header file and each class definition in its own .cpp file.

3.2 Implement the use of inheritance in C++?

Problem

I know how to declare and implement a class in C++ and am now ready to implement a class hierarchy using inheritance. Is there a simple example that will demonstrate inheritance in C++?

Technique

Declaring and implementing user-defined types (classes) is a powerful feature of C++. This power is extended using class inheritance. Many abstractions are best represented in an inheritance hierarchy. For example, circles, squares, and polygons are all shapes. This suggests the use of a base class (possibly) named Shape. This base class will declare functionality and attributes that is common to all shapes. Then each specific type, such as Square, will derive from Shape and modify or extend the functionality for its specific purpose.


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.