We conclude this chapter by describing shortly the typical structure of the OOP program.
As we have seen in previous sections, an OOP program consists of objects that collaborate by messages. In this structure, one object must play the role of a starting object. This means that one of the methods of this object will be called as the program start. The starting object typically creates other objects in the program and manages the their lifetime.
All the other objects represent various parts of the problem solved and are responsible for the associated resource management, computation, etc.
As we are in C++, our program must have the main() function where the program starts. It will create the starting object and let it run:
int main() { // Create the starting object Experiment().Run(); // and run it }The Experiment class could be defined as follows:
class Experiment{ public: Experiment(); // Constructor void Run(); // Run the experiment private: Detector *det; // Detector in this experiment Source *sce; // Particle source ResultFile *rfile; // Result file };
The Experiment::Experiment() constructor reads the input data (e.g., cross sections describing the probabilities of the interactions) and creates and initializes the attributes (particle source, result file etc.).
The Experiment::run() methods does the work - it starts particle emission in the source using the appropriate method of the Source class, determines, whether the given particle hits the detector using the appropriate Detector method, records the results of the detection into the ResultFile instance and in the end processes the results using the appropriate ResultFile methods.
Class diagram of the program at the level we have given here is presented in Fig. 13.8.
Note that this is top-level design only. The extent of this chapter allows us to demonstrate only the beginning of the object oriented approach to the example.