Modeling Our Simulator Components

Read time: 8 minutes (2231 words)

Each of the actual components we will need to model in our system shares the basic structure we will use in a basic Component class design. What makes each one unique is the behavior of that component. We need to provide a way to create each needed component part, while retaining a common component interconnection strategy.

The answer is to use C++ inheritance to create each part.

C++ Inheritance

If we need all of the features of one class, but we need to change some aspect of how things work by adding new attributes, or by providing methods that operate differently, we can “derive” a new class from an existing one. We call the existing class the “parent” class, or the “base” class, and the new class the “child” or “derived” class.

The child class can add attributes and methods, but it cannot remove any attributes or methods from the base class. The child class can choose to alter how those methods operate by “overriding” them with new implementations. It is even possible to create a new method that uses the old one to do some of the needed work, then completes actions needed.

Our needs will be pretty simple. We will not add any new attributes, but we will provide a new implementation of the existing tick method. That is where all the action occurs!

Here is the basic scheme:

class Component {
    ...
};

class PC : public Component {
    ...
};

Actually, we have control over which attributes methods in the base class the derived class can access. Here is a guide that tries to explain how this works:

Class Access Control

C++ has three kinds of access specifiers:

  • public
  • private
  • protected.

Here is a simple diagram showing how derived classes can use data from the base class:

Access public protected private
members of the same class yes yes yes
members of derived class yes yes no
not members yes no no

This table says that you can create methods in the base class that are protected, and derived class methods can call those methods to get at the private data in the base class. Clear! (Yeah, right!)

Since we will want our derived components to access all of the connection attributes and methods, we will need to provide access to them. So, we need to change the protection we are using a bit.

We will do that as we build the individual parts, and we will introduce the specialized component as we work through the four steps of processing.