1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115 | #include <iostream>
#include <vector>
#include <cmath>
// Vector class to represent both 2D and 3D vectors
class Vector {
public:
Vector(std::vector<double> components) : components_(components) {}
Vector operator+(const Vector& other) const {
if (components_.size() != other.components_.size()) {
throw std::runtime_error("Vector dimensions do not match");
}
std::vector<double> result(components_.size());
for (size_t i = 0; i < components_.size(); ++i) {
result[i] = components_[i] + other.components_[i];
}
return Vector(result);
}
Vector operator*(const double& other) const {
std::vector<double> result(components_.size());
for (size_t i = 0; i < components_.size(); ++i) {
result[i] = components_[i] * other;
}
return Vector(result);
}
friend std::ostream& operator<<(std::ostream& os, const Vector& v) {
os << "(";
for (size_t i = 0; i < v.components_.size(); ++i) {
os << v.components_[i];
if (i < v.components_.size() - 1) os << ", ";
}
os << ")";
return os;
}
std::vector<double> components_;
};
// Function overloading for vector norm
double norm(const Vector& v) {
if (v.components_.size() == 2) {
return std::sqrt(v.components_[0] * v.components_[0] + v.components_[1] * v.components_[1]);
} else if (v.components_.size() == 3) {
return std::sqrt(v.components_[0] * v.components_[0] + v.components_[1] * v.components_[1] + v.components_[2] * v.components_[2]);
} else {
throw std::runtime_error("Norm function only supports 2D and 3D vectors");
}
}
class Particle {
public:
Particle(double mass, const Vector& position, const Vector& velocity)
: mass_(mass), position_(position), velocity_(velocity) {
std::cout << "Particle created at position " << position_ << std::endl;
}
~Particle() {
std::cout << "Particle destroyed at position " << position_ << std::endl;
}
void updatePosition(double time) {
position_ = position_ + velocity_ * time;
}
void printState() const {
std::cout << "Particle - Mass: " << mass_ << ", Position: " << position_
<< ", Velocity: " << velocity_ << ", Speed: " << norm(velocity_) << std::endl;
}
private:
double mass_;
Vector position_;
Vector velocity_;
};
/* Given a particle, what can one do?
- Create a collection of particles
- Create multiple collection of particles
- Mix 2D and 3D particles
- Make the particles move
- Benchmark the motion
*/
int main() {
// Create a 2D particle
Particle particle2D(5.0, Vector({0.0, 0.0}), Vector({1.0, 2.0}));
std::cout << "Initial state of 2D particle:" << std::endl;
particle2D.printState();
particle2D.updatePosition(2.0);
std::cout << "2D particle after 2 seconds:" << std::endl;
particle2D.printState();
// Create a 3D particle
Particle particle3D(10.0, Vector({0.0, 0.0, 0.0}), Vector({1.0, 2.0, 3.0}));
std::cout << "\nInitial state of 3D particle:" << std::endl;
particle3D.printState();
particle3D.updatePosition(2.0); // ERROR
return 0;
std::cout << "3D particle after 2 seconds:" << std::endl;
particle3D.printState();
// Demonstrate vector addition
Vector v1({1.0, 2.0});
Vector v2({3.0, 4.0});
Vector v3 = v1 + v2;
std::cout << "\nVector addition: " << v1 << " + " << v2 << " = " << v3 << std::endl;
return 0;
}
|