TU-Programmieren_2/lab2/taskB.cpp
2025-04-09 10:22:44 +02:00

76 lines
1.9 KiB
C++

/// @file
/// @brief task B
/// g++ -std=c++20 taskB.cpp -o taskB && ./taskB
#include <iostream>
namespace one {
struct Particle {
double vx; // velocity in x-direction
double vy; // velocity in y-direction
double m; // mass
/// @todo implement the following member functions:
/// - energy(): returns the kinetic energy of the particle (formula, see main.ipynb)
/// - print(): prints the values of the member variables to the console
double energy() {
return 0.5 * m * (vx * vx + vy * vy);
}
void print() {
std::cout << "vx: " << vx << ", vy: " << vy << ", m: " << m << std::endl;
}
};
} // end namespace one
namespace two {
/// @todo implement a class with the following properties:
/// - private member variables vx, vy, m
/// - constructor with three parameters (for the three member variables)
/// - set(): function to change the values of all three member variables
/// - get(): returns the values of the member variables in a tuple
/// - energy(): returns the kinetic energy of the particle (formula, see main.ipynb)
/// - print(): prints the values of the member variables to the console
class Particle {
double vx; // velocity in x-direction
double vy; // velocity in y-direction
double m; // mass
void set(double vx, double vy, double m) {
this->vx = vx;
this->vy = vy;
this->m = m;
}
std::tuple<double, double, double> get() {
return std::make_tuple(vx, vy, m);
}
double energy() {
return 0.5 * m * (vx * vx + vy * vy);
}
void print() {
std::cout << "vx: " << vx << ", vy: " << vy << ", m: " << m << std::endl;
}
};
} // end namespace two
int main() {
/// @todo create objects from both classes and use their member functions
one::Particle p1;
p1.vx = 1.0;
p1.vy = 2.0;
p1.m = 3.0;
p1.print();
std::cout << "energy: " << p1.energy() << std::endl;
return 0;
}