// PL4/class2a.cpp // An example using a class with a mistake // Niels Walet, last updated 04/12/2019 #include<iostream> #include<string> #include<cmath> class particle { private: std::string type; double mass; double momentum; double energy; public: // Function to set type of particle void set_type(const string &ptype) {type=ptype;} // Function to print type of particle void print_type() {cout<<"Particle is of type "<<type<<endl;} }; void print_data(const struct particle &p) { std::cout.precision(3); // 2 significant figures std::cout<<"Particle: [type,m,p,E] = ["<<p.type<<","<< p.mass <<","<<p.momentum<<","<<p.energy<<"]"<<std::endl; return; } double gamma(const struct particle &p) { return p.energy/p.mass; } int main() { particle p1; p1.type="electron"; p1.mass=5.11e5; p1.momentum=1.e6; p1.energy=sqrt(p1.mass*p1.mass+p1.momentum*p1.momentum); particle p2; p2.type="proton"; p2.mass=0.938e9; p2.momentum=3.e9; p2.energy=sqrt(p2.mass*p2.mass+p2.momentum*p2.momentum); // Print out details print_data(p1); print_data(p2); // Calculate Lorentz factors std::cout.precision(2); std::cout<<"Particle 1 has Lorentz factor gamma=" <<gamma(p1)<<std::endl; std::cout<<"Particle 2 has Lorentz factor gamma=" <<gamma(p2)<<std::endl; return 0; } |