[an error occurred while processing this directive]
CHAPTER 3 OBJECT ORIENTATIONC++ SPECIFICS
How do I...
- 3.1 Create a simple class in C++?
- 3.2 Implement the use of inheritance in C++?
- 3.3 Apply the use of encapsulation in a C++ program?
- 3.4 Implement polymorphism in C++?
- 3.5 Implement static members of a class?
In this chapter, you will explore the world of object-oriented programming using the C++ language. In Chapter 1,A Quick Introduction to the Language, the fundamentals of the language are introduced. In Chapter 2, Object OrientationTheory and Practice, the three basic concepts of object-oriented programming are introduced: inheritance, encapsulation, and polymorphism.
The first four How-Tos in this chapter will mirror the first four How-Tos in Chapter 2. This will provide you with a basic understanding of implementing inheritance, encapsulation, and polymorphism. The last How-To demonstrates the use of static members of a class.
3.1 Create a Simple Class in C++
This How-To shows how to create a user-defined type, known specifically as a class. You will also learn how to separate the declaration and definition of a class.
3.2 Implement the Use of Inheritance in C++
This How-To explores the concept of inheritance by providing a concrete example. Inheritance is a powerful concept and is easily implemented in the C++ language. The example given in the How-To will guide you through the process of creating an inheritance hierarchy.
3.3 Apply the Use of Encapsulation in a C++ Program
Encapsulation is one of three basic concepts of object-oriented programming. Encapsulation is the grouping of a class interface and its implementation, and providing a clean separation of the two. This How-To will introduce the concept, providing an example to illustrate the concept.
3.4 Implement Polymorphism in C++
Inheritance and encapsulation are two (of three) important features of object-oriented programming. The third important feature, polymorphism, is demonstrated in this How-To.
3.5 Implement Static Members of a Class
The use of static data members and static member functions can be useful in certain programming situations. The decision to use this feature should be dealt with during the design of your classes. In this How-To, you will see how to exploit the use of static members of a class.
3.1 Create a simple class in C++?
Problem
I have a good understanding of the C++ language in general, and am ready to explore the use its object-oriented features. I understand that the class is the basic building block for implementing all the concepts of object-oriented programming in C++. Is there some class I can define that will demonstrate the basic organization of a class?
Technique
Every class in C++ implies the use of object-oriented functionality. You should follow some basic rules when defining your classes. This How-To demonstrates those rules by creating a class and showing how to create an object of that class at runtime. This technique will demonstrate the principles of class declaration and definition.
This How-To will also show how to separate a class declaration from its implementation by using separate files. You will use a header file to contain the class declaration and define the functionality of the class in an implementation file.
Steps
- 1. Change to your base source directory and create a new subdirectory named VEHICLE.
- 2. Fire up your source code editor and type the following code into a file named VEHICLE.H:
// vehicle.h - this file contains a class
// declaration for the vehicle type.
class Vehicle
{
public:
enum Switch { SWITCH_ON, SWITCH_OFF } ;
Vehicle( ) ;
~Vehicle( ) ;
bool powerSwitch( Switch onOrOff ) ;
unsigned int accelerate( unsigned int amount ) ;
unsigned int decelerate( unsigned int amount ) ;
bool isMoving( void ) const ;
unsigned int getSpeed( void ) const ;
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 named VEHICLE.CPP and type the following code into that file:
// declaration of Vehicle
#include vehicle.h
Vehicle::Vehicle( ) :
speed( 0 ), moving( false )
{
}
Vehicle::~Vehicle( )
{
speed = 0;
moving = false ;
}
bool Vehicle::powerSwitch( Switch onOrOff )
{
bool on = false ;
if( onOrOff == SWITCH_OFF )
{
speed = 0 ;
moving = false ;
}
return( on ) ;}unsigned int Vehicle::accelerate( unsigned int amount )
{
speed += amount ;
moving = true ;
return( speed ) ;
}
unsigned int Vehicle::decelerate( unsigned int amount )
{
if( amount > speed )
speed = 0 ;
else
speed -= amount ;
moving = (speed == 0) ? false : true ;
return( speed ) ;
}
bool Vehicle::isMoving( ) const
{
return( moving ) ;
}
unsigned int Vehicle::getSpeed( ) const
{
return( speed ) ;
}
unsigned int Vehicle::setSpeed( unsigned int speedIn )
{
speed = speedIn ;
moving = true ;
return( speed ) ;
}
- 4. Save this file and create a new file named MAIN.CPP. This file should contain the following:
#include <iostream>
#include vehicle.h
int main( )
{
Vehicle vehicle ;
Vehicle *pv = new Vehicle() ;
vehicle.setSpeed( 10 ) ;
pv->setSpeed( 20 ) ;
cout << vehicles speed: << vehicle.getSpeed() << endl ;
cout << pvs speed : << pv->getSpeed() << endl ;
delete pv ;
return 0 ;
}
- 5. Save MAIN.CPP and return to the command line. Next, compile and link MAIN.CPP and VEHICLE.CPP as in the following example:
gcc -c main.cpp vehicle.cpp
If your compiler complains about the statement
#include <iostream>
comment out (or remove) the using directive following it and change the #include line to
#include <iostream.h>
- 6. Run the program; the output should be as follows:
vehicles speed: 10
pvs speed : 20
If you are using the DJGPP compiler, you might have to rename the executable from A.OUT to MAIN.EXE to execute the program.
|