Website Summary Files Commits Branches @commit @diff @stats
path: root / Operator_overloading
diff options
context:
space:
mode:
Commit112a0d344da9e66d20ee9ab5d488e59c5d52e6ad (patch)
Author
 Angeld55 <angel_dimitriev@abv.bg>
2020-04-09 18:09:42 +0300
Committer
 Angeld55 <angel_dimitriev@abv.bg>
2020-04-09 18:09:42 +0300
Filese3bb9cf17c24a3b53c746d9142906d550d549465 /Operator_overloading
Object-oriented_programming_FMI-112a0d344da9e66d20ee9ab5d488e59c5d52e6ad.tar
Object-oriented_programming_FMI-112a0d344da9e66d20ee9ab5d488e59c5d52e6ad.tar.gz
Object-oriented_programming_FMI-112a0d344da9e66d20ee9ab5d488e59c5d52e6ad.zip
Parentec26fab32d144b436d08f9da6049bd053156d783 (diff)
added new examples
diff --git a/Operator_overloading/Complex/Complex.hpp b/Operator_overloading/Complex/Complex.hpp
new file mode 100644
index 0000000..8a6edd5
--- /dev/null
+++ b/Operator_overloading/Complex/Complex.hpp
@@ -0,0 +1,136 @@
+#include <iostream>
+class Complex
+{
+private:
+ double real;
+ double im;
+
+public:
+ Complex();
+ Complex(double real, double im);
+
+ Complex getConjugated() const; //връща комплексно спрегнатото число на текущия обект.
+
+ //assignment operators
+ // a+=b Към а се добавя стойността на b. а се променя , b не се. Операцията ще върне a за да може да се правят верижни изрази (a+=b+=c)
+ Complex& operator+=(const Complex&);
+ Complex& operator-=(const Complex&);
+ Complex& operator*=(const Complex&);
+ Complex& operator/=(const Complex&);
+
+
+
+ //stream opeartors ( std::cout>> c1) (std::cin<<c1 )
+ friend std::ostream& operator<<(std::ostream&, const Complex&);
+ friend std::istream& operator>>(std::istream&, Complex&);
+
+};
+// a+b. Операторът връща НОВА променлива(Complex), която е резултатът от операцията.
+Complex operator+(const Complex&, const Complex&);
+Complex operator-(const Complex&, const Complex&);
+Complex operator*(const Complex&, const Complex&);
+Complex operator/(const Complex&, const Complex&);
+
+
+
+Complex::Complex()
+{
+ real = 0;
+ im = 0;
+}
+Complex::Complex(double real, double im)
+{
+ this->real = real;
+ this->im = im;
+}
+Complex& Complex::operator+=(const Complex& other)
+{
+ real += other.real;
+ im += other.im;
+ return *this;
+}
+Complex& Complex::operator-=(const Complex& other)
+{
+ real -= other.real;
+ im -= other.im;
+ return *this;
+}
+Complex& Complex::operator*=(const Complex& other)
+{
+ int oldReal = real;
+ real = real*other.real - im*other.im;
+ im = oldReal*other.im + im * other.real;
+ return *this;
+}
+Complex& Complex::operator/=(const Complex& other) // a/=b
+{
+ Complex Conjugated = other.getConjugated(); //взимаме комплексно спрегнатата на другата дроб (b)
+
+ Complex otherCopy(other);// копираме другата (b), за да не я промяняме.
+
+ //Умножаваме двете по комплексно спрегнатата.
+ *this *= Conjugated;
+ otherCopy *= Conjugated; //Тук трябва да остане само реална част.
+
+ if (otherCopy.real != 0)
+ {
+ real /= otherCopy.real;
+ im /= otherCopy.real;
+ }
+
+ return *this;
+}
+
+//След дефинирането на +=, -=, *= и /=, можем лесно да дефинираме +, -, * и /, преизползвайки вече написаните опратори.
+
+
+Complex operator+(const Complex& lhs, const Complex& rhs) { //a+b
+
+ Complex result(lhs); // Създаваме нов обект, който е копие на a (използваме копи-конструктора)
+ result += rhs; //към копието на a, добавяме b (Използваме вече дефинирания оператор +=)
+
+ return result;
+}
+//Операторите -, *, / са аналогични с +.
+Complex operator-(const Complex& lhs, const Complex& rhs) {
+ Complex result(lhs);
+ result -= rhs;
+
+ return result;
+}
+
+Complex operator*(const Complex& lhs, const Complex& rhs) {
+
+ Complex result(lhs);
+ result *= rhs;
+
+ return result;
+}
+
+Complex operator/(const Complex& lhs, const Complex& rhs) {
+
+ Complex result(lhs);
+ result /= rhs;
+
+ return result;
+}
+
+Complex Complex::getConjugated() const
+{
+ Complex result(*this);
+ result.im *= -1;
+
+ return result;
+}
+
+// за да можем да правим cout<<c1 или да запазваме във файл и др.
+std::ostream& operator<<(std::ostream& ofs, const Complex& r) {
+
+ return ofs << r.real << ' ' << r.im << 'i';
+}
+
+// за да можем да правим cin>>c1 или да четем от файл и др.
+std::istream& operator>>(std::istream& ifs, Complex& r) {
+
+ return ifs >> r.real >> r.im;
+}
diff --git a/Operator_overloading/Vector/Source.cpp b/Operator_overloading/Vector/Source.cpp
new file mode 100644
index 0000000..c2e3c70
--- /dev/null
+++ b/Operator_overloading/Vector/Source.cpp
@@ -0,0 +1,14 @@
+#include <iostream>
+#include "Vector.h"
+int main()
+{
+
+
+ Vector v1(2);
+
+ std::cin >> v1;
+
+ std::cout << (v1);
+
+
+}
diff --git a/Operator_overloading/Vector/Vector.cpp b/Operator_overloading/Vector/Vector.cpp
new file mode 100644
index 0000000..5059a66
--- /dev/null
+++ b/Operator_overloading/Vector/Vector.cpp
@@ -0,0 +1,145 @@
+#include "Vector.h"
+
+Vector::Vector() :coordinates(nullptr), length(0)
+{}
+Vector::Vector(int len)
+{
+ coordinates = new int[len];
+ for (int i = 0; i < len; i++)
+ coordinates[i] = 0;
+ length = len;
+}
+void Vector::CopyFrom(const Vector& other)
+{
+ coordinates = new int[other.length];
+ for (int i = 0; i < other.GetLen(); i++)
+ coordinates[i] = other.coordinates[i];
+ length = other.length;
+}
+void Vector::Free()
+{
+ delete[] coordinates;
+ length = 0;
+}
+Vector::Vector(const Vector& other)
+{
+ CopyFrom(other);
+}
+Vector& Vector::operator=(const Vector& other)
+{
+ if (this != &other)
+ {
+ Free();
+ CopyFrom(other);
+ }
+ return *this;
+}
+Vector::~Vector()
+{
+ Free();
+}
+
+int& Vector::operator[](int index)
+{
+ if (index < length)
+ return coordinates[index];
+ else
+ throw 0;
+}
+void Vector::print()
+{
+ std::cout << '(';
+ for (int i = 0; i < length; i++)
+ std::cout << coordinates[i]<<" ";
+ std::cout <<')' <<std::endl;
+}
+
+int Vector::GetLen() const
+{
+ return length;
+}
+int Vector::GetAtIndex(int index) const
+{
+ if (index<length)
+ return coordinates[index];
+ else
+ return 0;
+}
+Vector& Vector::operator+=(const Vector& other)
+{
+ if (GetLen() != other.GetLen())
+ throw "Invalid operation";
+ for (int i = 0; i < other.length; i++)
+ coordinates[i] += other.coordinates[i];
+ return *this;
+}
+
+Vector& Vector::operator-=(const Vector& other)
+{
+ if (GetLen() != other.GetLen())
+ throw "Invalid operation";
+ for (int i = 0; i < other.length; ++i)
+ coordinates[i] -= other.coordinates[i];
+ return *this;
+}
+
+Vector& Vector::operator*=(int scalar)
+{
+ for (int i = 0; i < length; i++)
+ coordinates[i] *= scalar;
+ return *this;
+}
+Vector operator+(const Vector& lhs,const Vector& rhs)
+{
+ Vector temp(lhs);
+ temp += rhs;
+ return temp;
+}
+
+Vector operator-(const Vector& lhs, const Vector& rhs)
+{
+ Vector temp(lhs);
+ temp -= rhs;
+ return temp;
+}
+
+int operator%(const Vector& lhs, const Vector& rhs)
+{
+ int min = lhs.GetLen() < rhs.GetLen() ? lhs.GetLen() : rhs.GetLen();
+ int result = 0;
+ for (int i = 0; i < min; i++)
+ result += lhs.GetAtIndex(i) * rhs.GetAtIndex(i);
+ return result;
+}
+bool operator||(const Vector& lhs, const Vector& rhs)// a и b са перпендикулярни <-> a*K=b (K е константа).
+{
+ double coeff = lhs.GetAtIndex(0) / rhs.GetAtIndex(0);
+ for (int i = 1; i < lhs.GetLen(); i++)
+ {
+ if (lhs.GetAtIndex(i) == 0 || rhs.GetAtIndex(i) == 0)
+ continue;
+ double Newfirst = lhs.GetAtIndex(i);
+ double Newcoeff = Newfirst / rhs.GetAtIndex(i);
+ if (abs(Newcoeff-coeff)>0.01)
+ return false;
+ }
+ return true;
+}
+bool operator|=(const Vector& lhs, const Vector& rhs)
+{
+ return lhs%rhs == 0; //скаларното произведение на два успоредни вектора е 0.
+}
+
+std::ostream& operator<<(std::ostream& os, const Vector& v)
+{
+ for (int i = 0; i < v.GetLen(); ++i)
+ os << v.GetAtIndex(i) << " ";
+ return os;
+}
+
+std::istream& operator>>(std::istream& ifs, Vector& v)
+{
+ for (int i = 0; i < v.GetLen(); ++i)
+ ifs >> v[i];
+ return ifs;
+}
diff --git a/Operator_overloading/Vector/Vector.h b/Operator_overloading/Vector/Vector.h
new file mode 100644
index 0000000..b8dbd20
--- /dev/null
+++ b/Operator_overloading/Vector/Vector.h
@@ -0,0 +1,37 @@
+#include <iostream>
+class Vector
+{
+private:
+ int* coordinates;
+ int length;
+ void CopyFrom(const Vector& other);
+ void Free();
+public:
+ Vector();
+ Vector(int len);
+
+ Vector(const Vector& other);
+ Vector& operator=(const Vector& other);
+ ~Vector();
+
+ int GetLen() const;
+
+ Vector& operator+=(const Vector& other);
+ Vector& operator-=(const Vector& other);
+ Vector& operator*=(int);
+
+ friend std::ostream& operator<<(std::ostream&, const Vector& v);
+ friend std::istream& operator>>(std::istream&, Vector& v);
+
+ int GetAtIndex(int index) const;
+ int& operator[](int index);// оператор за индексиране(като в масивите) vector a; a[4] -взима четвъртия кординат на вектора
+ void print();
+};
+
+Vector operator+(const Vector& lhs, const Vector& rhs);// Събиране на два вектора
+Vector operator-(const Vector& lhs, const Vector& rhs);// Изваждане на два вектора
+
+int operator%(const Vector& lhs, const Vector& rhs); // Скаларно произведение на два вектора
+
+bool operator||(const Vector& lhs, const Vector& rhs); // Дали два вектора са успоредни
+bool operator|=(const Vector& lhs, const Vector& rhs); // Дали два вектора са перпендикулярни