class Track |
Declare Track class. For a basic discussion of a class see the OO topic
Objects & Classes.
|
: public TObject |
This section is optional. The colon is followed by a comma separated list
of classes from which the Track class
inherits.
The access specifier, public in this case, sets a lower level of security on
the embedded TObject members. See OO topic
Inheritance
|
{ |
The opening curly brace of the
compound statement
that defines the class. |
|
public: |
This access specifier means that the following are visible to users of the
class. Its normal to start with the public section - its the part that user's
of an object need to know about.
See the OO topic
Private & Public.
|
|
// Constructors and Destructors |
These
member functions
are used to create and destroy objects. See the OO topic
Constructors & Destructors.
|
|
Track::Track(); |
This is the default
constructor
i.e. the one to be used if no arguments are available. Every class has to have
a default constructor.
The body of this function is held in Track.cxx.
|
|
Track(Float_t mass, Float_t energy =0.); |
This constructor
will be called when a two argument constructor is required. It can also be
used when only a one argument constructor is required and will then have energy
set to zero.
The body of this function is held in Track.cxx.
Track has two constructors, a situation that is discussed in the
OO topic
Overloading.
|
|
~Track(); |
This is the Track
destructor.
Destructors have an important role to play when an object creates further
objects during its lifetime.
The body of this function is held in Track.cxx.
See the OO topic
Constructors & Destructors.
|
|
// Getters and Setters |
Getters and setters allow a class designer to allow controlled
access to parts of the object.
|
|
Float_t GetEnergy() {
return fEnergy;
}
|
The complete function is supplied in the header. This is often the case with
simple functions and allows the compiler to replace function calls with in-
line code.
See OO topic
Private & Public.
|
|
void Track::SetEnergy( Float_t energy ) {
fEnergy = energy;
fMomentum
= sqrt( fEnergy*fEnergy - fMass*fMass );
}
|
Again the compiler can in-line the code. This also demonstrates how Setters
can be used to ensure the object remains internally self-consistent.
See the OO topic:
Objects & Classes.
|
|
// Operations |
Here is where the more substantial member functions are defined.
|
|
virtual |
virtual means that calls to this function go via a virtual function address
table so that, when embedded in another object, that object can intercept
calls to this function. See OO topic
Virtual Functions &
Polymorphism.
|
void Propagate() |
|
= 0; |
This means that the function has no body. It must be supplied by an
inheriting class before objects can be created. See OO topic
Interfaces & Abstract Classes.
|
|
private: |
From now on, all members are private and are not visible by users of the
object or classes that inherit from this class. The alternative:
protected
would allow inheriting classes access but still deny it to users.
See OO topic
Private & Public.
|
|
// Data
Float_t fEnergy;
Float_t fMass;
Float_t fMomentum;
|
The object's
data members.
Object
encapsulation
dictates that all data members should be private.
|
|
}; |
The end of the compound statement that defines the class. It is followed by a
semi-colon which is needed because it is possible, if unusual, to define a
class and a object of that class in a single statement:-
class MyClass {...} MyObject;
|