-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperatorOverloading.cpp
More file actions
65 lines (52 loc) · 2.49 KB
/
operatorOverloading.cpp
File metadata and controls
65 lines (52 loc) · 2.49 KB
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
#include <iostream>
// Structure to represent a 2D vector
struct Vector2 {
float x, y; // Member variables to store coordinates
// Constructor to initialize a Vector2 object
Vector2(float x, float y)
: x(x), y(y) {}
// Function to add two Vector2 objects
Vector2 Add(const Vector2& other) const {
return Vector2(x + other.x, y + other.y); // Returns the sum of two vectors
// Alternative implementation: return *this + other;
}
// Overloaded '+' operator to enable vector addition using "+"
Vector2 operator+(const Vector2& other) const {
return Add(other); // Calls the Add function
// Alternative implementation: return Vector2(x + other.x, y + other.y);
}
// Function to multiply two Vector2 objects
Vector2 Multiply(const Vector2& other) const {
return Vector2(x * other.x, y * other.y); // Returns the product of two vectors
// Alternative implementation: return *this * other;
}
// Overloaded '*' operator to enable vector multiplication using "*"
Vector2 operator*(const Vector2& other) const {
return Multiply(other); // Calls the Multiply function
// Alternative implementation: return Vector2(x * other.x, y * other.y);
}
// Overloaded '==' operator to compare two Vector2 objects for equality
bool operator==(const Vector2& other) const {
return (x == other.x && y == other.y); // Returns true if both coordinates match
}
};
// Overloaded '<<' operator to print Vector2 objects using std::cout
std::ostream& operator<<(std::ostream& stream, const Vector2& other) {
stream << other.x << ", " << other.y; // Prints the vector components
return stream;
}
int main() {
// Initializing three Vector2 objects
Vector2 position(4.0f, 4.0f); // Position vector
Vector2 speed(0.5f, 1.5f); // Speed vector
Vector2 powerup(1.1f, 1.1f); // Power-up modifier vector
// Using the defined methods and operators to perform calculations
Vector2 result1 = position.Add(speed.Multiply(powerup)); // Addition using function calls
Vector2 result2 = position + (speed * powerup); // Addition using overloaded operators
// Displaying the results
std::cout << result1 << std::endl; // Prints first calculated vector
std::cout << result2 << std::endl; // Prints second calculated vector
// Comparing two results for equality
std::cout << std::boolalpha << (result1 == result2) << std::endl; // Prints "true" or "false"
return 0;
}