From 8d7815771cd7e036697521c47354cf807a955ef6 Mon Sep 17 00:00:00 2001 From: Butcher3Years Date: Mon, 2 Feb 2026 20:25:32 +0530 Subject: [PATCH] About operator overloading !!!! and when to use them!! --- Operators -- its overloading/Log.cpp | 64 ++++++++++++++++++++ Operators -- its overloading/dumb.cpp | 84 +++++++++++++++++++++++++++ Operators -- its overloading/main.cpp | 40 +++++++++++++ 3 files changed, 188 insertions(+) create mode 100644 Operators -- its overloading/Log.cpp create mode 100644 Operators -- its overloading/dumb.cpp create mode 100644 Operators -- its overloading/main.cpp diff --git a/Operators -- its overloading/Log.cpp b/Operators -- its overloading/Log.cpp new file mode 100644 index 0000000..f829f51 --- /dev/null +++ b/Operators -- its overloading/Log.cpp @@ -0,0 +1,64 @@ +#include +#include + + +struct Vector2 +{ + float x, y; + + Vector2(float x,float y) + : x(x), y(y) {} + + /*Vector2 Add(const Vector2& other) const + { + return *this + other; const ptr in this case so we need to dereference this this keyword!! + return operator+(other); + + } */ + + + + Vector2 Add(const Vector2& other) const + { + return Vector2(x + other.x, y + other.y); + } + + + Vector2 operator+(const Vector2& other) const + { + return Add(other); + } + + /* + + Vector2 operator+(const Vector2& other) const + { + return Vector2(x + other.x, y + other.y); we have done reverse of the overloading like creating Add from + + } + */ + + + + Vector2 Multiply(const Vector2& other) const + { + return Vector2(x * other.x, y * other.y ); + } + + Vector2 operator*(const Vector2& other) const + { + return Multiply(other); + } + +}; + +int main() +{ + Vector2 position(4.0f, 4.0f); + Vector2 speed(0.5f, 1.5f); + Vector2 powerup(1.1f, 1.1f); + + + Vector2 result1 = position.Add(speed.Multiply(powerup)); + Vector2 result2 = position + (speed * powerup);//BODMAS +} + diff --git a/Operators -- its overloading/dumb.cpp b/Operators -- its overloading/dumb.cpp new file mode 100644 index 0000000..da63c1e --- /dev/null +++ b/Operators -- its overloading/dumb.cpp @@ -0,0 +1,84 @@ +#include +#include + + +struct Vector2 +{ + float x, y; + + Vector2(float x,float y) + : x(x), y(y) {} + + + Vector2 Add(const Vector2& other) const + { + return Vector2(x + other.x, y + other.y); + } + + + Vector2 operator+(const Vector2& other) const + { + return Add(other); + } + + + + + Vector2 Multiply(const Vector2& other) const + { + return Vector2(x * other.x, y * other.y ); + } + + Vector2 operator*(const Vector2& other) const + { + return Multiply(other); + } + + + //equals(),Tostring in case of java + + bool operator==(const Vector2& other) const//EQUATING + { + return x == other.x && y == other.y; + } + + bool operator!=(const Vector2& other) const + { + + // return !operator==(other); not preferrable!!!! + return !(*this == other);//to reverse how it works!! + } + + +}; + + std::ostream& operator<<(std::ostream& stream, const Vector2& other) + { + stream << other.x << ", " << other.y; + return stream; + } + +int main() +{ + Vector2 position(4.0f, 4.0f); + Vector2 speed(0.5f, 1.5f); + Vector2 powerup(1.1f, 1.1f); + + + Vector2 result1 = position.Add(speed.Multiply(powerup)); + Vector2 result2 = position + (speed * powerup);//BODMAS + + //if (!result1.equals(result2))//this is how you equate in a java + if(result1 == result2) + { + + } + + + std::cout << result2 << std::endl;//no overload for << operator to take in an output string and also Vector2 + +} + + +//This is about the leftshift operator!!! + diff --git a/Operators -- its overloading/main.cpp b/Operators -- its overloading/main.cpp new file mode 100644 index 0000000..862cd4a --- /dev/null +++ b/Operators -- its overloading/main.cpp @@ -0,0 +1,40 @@ +#include +#include + + +struct Vector2 +{ + float x, y; + + Vector2(float x,float y) + : x(x), y(y) {} + + Vector2 Add(const Vector2& other) const + { + return Vector2(x + other.x, y + other.y); + } + + Vector2 Multiply(const Vector2& other) const + { + return Vector2(x * other.x, y * other.y ); + } + + +}; + +int main() +{ + Vector2 position(4.0f, 4.0f); + Vector2 speed(0.5f, 1.5f); + Vector2 powerup(1.1f, 1.1f);//Multiplies the result!! + + + Vector2 result = position.Add(speed.Multiply(powerup));//without operator overloading + + std::cin.get(); +} + + +//Operators are just funtions!! +//Operator overloading is just giving new funtionality to the operator +//This is not supported on java and partially on c# and use when only its needed! \ No newline at end of file