Capítulo 5 Propuesta de solución
5.2 Procedimientos del modelo de gestión de proyectos en el DIM
5.2.12 Grupo de procesos para la fase de ejecución
To enable you to relate some of the concepts described previously, we will develop an object hierarchy without using C++ language syntax or its keywords. The class definitions shown below cannot be compiled in an actual C++ program, however, they demonstrate the principles associated with an object hierarchy. We start with the abstract class Vehicle discussed earlier:
Abstract Class Vehicle Member data: Speed Power Member Functions: Stop Go
This class is the most fundamental of all classes of this example. It has encapsulated the bare minimum that is essential for a vehicle. It must possess Power for it to be able to move, and also will have a Speed characteristic to describe its motion. The member function Go will start the vehicle moving, and the other member function Stop will bring it to a stand-still.
New classes with more specific details added to them can then be derived from the Vehicle class. We have done this by forming two new class definitions named Passenger Transport, and Goods Transport. This class hierarchy is shown in Figure 4-7 where there are two branches coming from its root (base class). The class definitions are shown below:
Derived Class Passenger Transport: derived from Vehicle class Additional member data:
Number of Passengers
Derived Class Goods Transport: derived from Vehicle class Additional member data:
Load carrying capacity in Kg.
Note that these two classes inherit all the member data and member functions of the base class Vehicle. For example, if we list everything in the Passenger Transport class we will form the class definition show below:
Derived Class Passenger Transport Member data: Speed Power Number of Passengers Member Functions: Stop Go
Figure 4-7 Deriving classes from a base class.
Passenger Transport Goods Transport Vehicle (Base Class)
Although we did not specifically mention Speed,Power,StopandGo, they are present in the new derived class as a result of inheritance. Furthermore, the Goods Transport class is a direct descendant of just the abstract class Vehicle and therefore does not inherit anything from the Passenger Transport class. Therefore, there are two branches, right at the root of this object hierarchy as shown in Figure 4-7.Also, the Passenger Transport class and the Goods Transport class may or may not be abstract classes. Any of these classes can be used to further derive new classes with additional refinements.
In the next class definition, a new class named Passenger Train is derived from the Passenger Transport base class:
Derived Class Passenger Train: derived from Passenger Transport class
Additional member data: Number of passenger cars Additional member functions:
Doors Open
Doors Close
Air Conditioning
In this class definition, the members of the Passenger Transport class are in effect added to the already existing members of the Passenger Transport class that have been inherited (and are invisible). If we managed to see the equivalent complete class definition for the Passenger Train class, it would appear as follows - the inherited members are shown in bold italic typeface:
Derived Class Passenger Train Member data:
Speed Power
Number of Passengers
Number of passenger cars Member Functions: Stop Go Doors Open Doors Close Air Conditioning
Just as we used Passenger Transport as a base class, the Goods Transport class can be used to derive further classes. An example of a new Goods Train class derived from the Goods Transport class is now given:
Derived Class Goods Train: derived from Goods Transport class Additional member data:
Number of boxcars Number of tank cars Number of open cars
Motorcars are primarily meant for transporting passengers. Therefore, if we wish to form a new class to represent motorcars, the best place to start is the Passenger Transport class. An example class definition for the new Motorcar class is now given:
Derived Class Motorcar: derived from Passenger Transport class
Additional member data:
Engine Capacity Body Colour Trim Colour Number of Cylinders Wheel Size Number of Doors
Additional member functions: Steer
Brake
If we then need a class definition to represent luxury cars, the best starting point is theMotorcar class. The Motorcar class is chosen instead of the Passenger Transport class because Motorcar objects form a more complete sub-object of a Luxury Car class. If we use the Passenger Transport class as the base class of the Luxury Car class, we will have to re-introduce members such as Engine Capacity, Body Colour,Trim Colour, etc. This involves a lot of unnecessary work, is error prone, and does not take advantage of code reuse.
Derived Class Luxury Car: derived from Motor Car class Additional member data:
Inside Air temperature
Global Position
Additional member functions: Air Conditioning Control Power Mirror Control Power Window control
Cruise Control
Antenna Control
In a class hierarchy, future changes that need to be carried out can be done with minimum reprogramming. If the necessary changes are very specific, then the changes are more likely to be made in the most recently derived classes. If the required changes are more general in nature, it is most likely that the changes will be carried out closer to the root of the hierarchy. For example, if all luxury cars are to have automatic navigation in the future, we will add a new member function namedNavigate to the Luxury Car class. On the other hand, if all vehicles are to be fitted with automatic navigation facilities in the future, we will add the Navigate member function to the abstract class Vehicle.
Figure 4-8 The example class hierarchy.
A set of object classes that fit into a class hierarchy always has an expansive nature rather than a multiplicity nature. In an expansive situation, additional member data and member functions will be added to the new derived class. In a multiplicity situation more members of the same object type are added to the new class.
For example, an object class representing a house and another object class representing a better house fit nicely into a class hierarchy. On the contrary, the class representing a house and another class representing many houses of the same type as the other class do not fit into a class hierarchy. New, completely different member objects need to exist in the new class.
Although you may have understood how classes are formed, their use may still be unclear. In the coming chapters, we will develop classes and begin to use them. For now, it is important to gain an understanding of what a class hierarchy is and how it is formed. Vehicle (Base Class) Passenger Transport Goods Transport Passenger Train Motor Car Luxury Car Goods Train