// the implementation file (source code) for the Particle class // //--------------------------------------------------------------- // for the source file for any class, we must always include the header file for that class #include "Particle.h" #include // Need the sqrt() function for the energy calculation //--------------------------------------------------------------- // this is the default constructor Particle::Particle() { // if we dont give any arguments, the mass and momentum will be zero fMass = 0; fP = 0; } // This is the normal constructor for an object of the class Particle // we create the object and assign the appropriate values to its members Particle::Particle(double mass, double p) { fMass = mass; fP = p; } double Particle::GetMass() { return fMass; } void Particle::SetMass(double new_mass) { fMass = new_mass; } double Particle::GetMomentum() { return fP; } void Particle::SetMomentum(double new_momentum) { fP=new_momentum; } double Particle::GetEnergy() { // Calculates the energy of the particle from its mass and its momentum // By doing this inside the class itself, it greatly simplifies your code // now in any program with the particle class // we can just get the energy using: particle.GetEnergy() return sqrt(fMass*fMass+fP*fP); } // Note that there is NO main() function in this file // This code cannot be executed on its own (but can be used by another program)