What Is Inheritance?


In object-oriented programming languages, inheritance is a way of enhancing existing classes or altering their functionality without rewriting them. Imagine you're writing a racing game. It would be sensible in that situation to create a class called car. The car class would contain such information as the car's current speed, weight, horsepower, and so forth.

The car class is generic. Ideally, you would like to use it for drag racers, stock cars, and so on. Inheritance lets you do that. You can use the car class to create such classes as drag_racer and stock_car. The drag_racer and stock_car classes have all of the functionality of the car class. They also add functionality that is specific to their own types of racecars. For example, drag racers have parachutes, but generic and stock cars don't. Therefore, the drag_racer class can add a data member that specifies the drag produced by the parachute. It should also have a data member that is true if the parachute is deployed and false if not.

Inheritance enables you to write a generic car class containing the basic functionality of all cars. You can use it to implement the drag_racer class, and the drag_racer class adds more features. In addition, you can use the same car class as the basis for implementing the stock_car class. This saves you effort. In other words, you get to be successfully lazy.

Game programmers often draw inheritance diagrams to help them figure out the inheritance of all the objects in their games. Figure 6.1 shows an example of an inheritance diagram.

Figure 6.1. Inheritance in the racing game.


As this diagram shows, both the drag_racer and stock_car classes inherit from the car class. You can tell this by the fact that the arrows point from drag_racer and stock_car to car.

When classes inherit from another class, the class they inherit from is called the base class or parent class. The classes that inherit from the base class are called the derived classes or child classes. In Figure 6.1, the car class is the base or parent class and the drag_racer and stock_car classes are the derived or child classes.

It's important to note that when child classes derive from parent classes, the children inherit all of the member data and functions of the parent. For instance, Figure 6.1 shows that the drag_racer class contains two items of member data that both relate to the drag racer's parachute. However, because drag_racer inherits from car, it essentially has a car object built into it automatically. As a result, drag_racer inherits all of the member data you see in car. Figure 6.2 illustrates this.

Figure 6.2. Child classes actually contain a parent class object.


As the figure shows, the derived drag_racer and stock_car objects contain a copy of a car object. The embedded car object is invisible and unnamed. Its functions are part of the child object. In this way, the child objects inherit all of the member data and member functions of the parent.



Creating Games in C++(c) A Step-by-Step Guide
Creating Games in C++: A Step-by-Step Guide
ISBN: 0735714347
EAN: 2147483647
Year: N/A
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net