Download presentation
Presentation is loading. Please wait.
Published byMadeleine Brooks Modified over 6 years ago
1
Abstract Classes and Inheritence Operator over-loading
C++ / G4MICE Course Session 5 Abstract Classes and Inheritence Dynamic Polymorphism Operators Operator over-loading
2
Abstract Classes It can be very useful when designing software to consider the objects (classes) that you have and determine if there are any common features. These can be extracted into an abstract class from which the specific unique examples are derived.
3
Abstract Class Example
Suppose you have lots of different detectors that are being simulated. Each detector will produce a different response, but should have some common information from the simulated particle. So you may have a class that is the essence of a Monte Carlo hit that knows about position, time, momentum, etc, but not the specifics of detectors.
4
MCHit -> Detector Hits
Position Momentum Time Energy Station Plane Fibre PhotoElectrons SciFiHit Station Slab PE TofHit
5
“Is a Kind Of” SciFiHit and TofHit inherit from MCHit.
They do this, not because they want a cheap and easy way of getting the MCHit properties for themselves, but because they are a kind of MCHit. This is the way to think about inheritence in an OO design. We look for objects which we can categorise as “a kind of” something and then group them together through an abstract class.
6
Pointers Suppose we have an object of the type SciFiHit: SciFiHit x;
We can point to it with a SciFiHit pointer: SciFiHit* y = & x; But we can also point to it from a MCHit pointer: MCHit* z = & x;
7
Dynamic Polymorphism This refers to a powerful feature of C++ whereby the method that is called from a pointer to an object is determined at run-time on the basis of what concrete type the pointer is pointing at. This means that you can have pointers to MCHit objects which are actually SciFiHits, or TofHits, etc. When you call a method of the MCHit class, if it has been overloaded by the SciFiHit or TofHit class, that version will run instead.
8
protected Protected is the other option to public and private for declaring methods or variables. If something is protected it can be seen by the class itself and any classes that inherit from it, but by no other code. So if a MCHit has things like momentum and energy, if they are protected, the SciFiHit and TofHit, etc can use them.
9
MCHit example Compile the code in the tarball using the command “make inheritence” and run the application inheritence. Read through the code and check that you understand how it works. Modify the TofHit so that it has its own version of the name() method.
10
Operator Overloading You can write your own definition of any of the operators for your class. This was briefly introduced in the ThreeVector example in the last session. It is generally a good idea to define the assignment operator as well as a copy constructor so that you can be sure what will happen in situations like: TofHit A; TofHit B; B = A; TofHit C(A);
11
Assignment Operator For a class “MyClass” would look something like:
MyClass operator=( const MyClass& c ) It is passed a const reference to what is on the right hand side of the equal sign (i.e. B in the example below). MyClass A; MyClass B = A;
12
Copy Constructor You may not want to have a default constructor (i.e. one with no arguments). But you may want to be able to construct an object with the initial value of another (recall one of the options for making a vector...) For this, we can define a copy constructor: MyClass( const MyClass& rhs ); This can be used in a manner like: MyClass A( ... ); MyClass B( A );
13
Some other Operators MyClass operator+( const MyClass& ) const;
bool operator==( const MyClass& ) const; bool operator<( const MyClass& ) const; As usual, if you can make a method const, it is a good idea to do so. Of course, some operators (e.g. ++) are required to change the object!
14
Final Task Try adding the operator==() method to the McHit class with some code that tests to see if the momentum, energy and name are the same. If you have time available, try to overload this function in one or both of the derived classes (SciFiHit and TofHit).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.