Advertisement
Guest User

overloading project

a guest
Nov 15th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Complex {
  5.     friend ostream &operator<<(ostream&, Complex&);
  6.     friend istream &operator>>(istream&, Complex&);
  7.     friend Complex &operator++(Complex&);      // Prefix increment  
  8.     friend Complex &operator++(Complex&, int); // Postfix increment  
  9. public:
  10.     Complex(int = 0, int = 0);
  11.     ~Complex();
  12.     void cube(Complex&);
  13.     bool operator==(const Complex &rhs) const;
  14.     bool operator!=(const Complex &rhs) const;
  15.     Complex operator+(const Complex &rhs)
  16.     {
  17.         float realSum = r + rhs.r;
  18.         float imagSum = i + rhs.i;
  19.         return Complex(realSum, imagSum);
  20.     }
  21.     Complex operator-(const Complex &rhs)
  22.     {
  23.         float realDif = r - rhs.r;
  24.         float imagDif = i - rhs.i;
  25.         return Complex(realDif, imagDif);
  26.     }
  27.     Complex operator*(const Complex &rhs)
  28.     {
  29.         float realProd = (r * rhs.r) + (-1 * i * rhs.i);
  30.         float imagProd = (r * rhs.i) + (i * rhs.r);
  31.         return Complex(realProd, imagProd);
  32.     }
  33. private:
  34.     float r, i;
  35.     bool f;
  36. };
  37.  
  38. Complex::Complex(int a, int b)
  39.     : r(a), i(b) {}
  40.  
  41. Complex::~Complex()
  42. {
  43.  
  44. }
  45.  
  46. void Complex::cube(Complex &a)
  47. {
  48.     float tmp = a.r;
  49.     float real1 = (a.r * a.r) + (-1 * a.i * a.i);   //real part after FOIL
  50.     float imag1 = (a.r * a.i) + (a.i * a.r);        //imaginary part after FOIL
  51.     a.r = (real1 * a.r) + (-1 * imag1 * a.i);   //real part of the cube
  52.     a.i = (real1 * a.i) + (imag1 * tmp);        //imaginary part of the cube
  53. }
  54.  
  55. Complex &operator++(Complex &a) //Pre
  56. {
  57.     a.cube(a);
  58.     return a;
  59. }
  60.  
  61. Complex &operator++(Complex &a, int) //Post
  62. {
  63.     Complex temp(a.r, a.i);
  64.     a.cube(a);
  65.     return temp;
  66. }
  67.  
  68. ostream &operator<<(ostream &out, Complex &a)
  69. {
  70.     if (a.i >= 0)
  71.     {
  72.         a.f = true;
  73.     }
  74.     else
  75.     {
  76.         a.f = false;
  77.     }
  78.  
  79.     if (a.f)
  80.     {
  81.         out << "(" << a.r << " + " << a.i << "i)" << endl;
  82.     }
  83.     else
  84.     {
  85.         a.i *= -1;
  86.         out << "(" << a.r << " - " << a.i << "i)" << endl;
  87.         a.i *= -1;
  88.     }
  89.     return out;
  90. }
  91.  
  92. istream &operator>>(istream &in, Complex &a)
  93. {
  94.     cout << "Enter the real value: ";
  95.     in >> a.r;
  96.     cout << "Enter the imgaginary value: ";
  97.     in >> a.i;
  98.     cout << "Original values are " << a.r << " and " << a.i << endl;
  99.  
  100.     if (a.i >= 0)
  101.     {
  102.         a.f = true;
  103.     }
  104.     else
  105.     {
  106.         a.f = false;
  107.     }
  108.  
  109.     if (a.f)
  110.     {
  111.         cout << "Equation is: (" << a.r << " + " << a.i << "i)" << endl << endl;
  112.     }
  113.     else
  114.     {
  115.         a.i *= -1;
  116.         cout << "Equation is: (" << a.r << " - " << a.i << "i)" << endl << endl;
  117.         a.i *= -1;
  118.     }
  119.     return in;
  120. }
  121.  
  122. bool Complex::operator==(const Complex &rhs) const
  123. {
  124.     return (r == rhs.r && i == rhs.i);
  125. }
  126.  
  127. bool Complex::operator!=(const Complex &rhs) const
  128. {
  129.     return (r != rhs.r || i != rhs.i);
  130. }
  131.  
  132. int main()
  133. {
  134.     Complex one, two, s, d, p;
  135.     cin >> one;
  136.     cin >> two;
  137.     s = one + two;
  138.     cout << "The sum is:" << endl << s;
  139.     d = one - two;
  140.     cout << "The difference is:" << endl << d;
  141.     p = one * two;
  142.     cout << "The product is:" << endl << p;
  143.     if (one == two)
  144.     {
  145.         cout << endl << "Expressions are equal to each other!" << endl;
  146.     }
  147.     if (one != two)
  148.     {
  149.         cout << endl << "Expressions are not equal to each other!" << endl;
  150.     }
  151.     cout << endl << "Preincrement expressions:" << endl << ++one << ++two;
  152.     cout << endl << "Postincrement expressions:" << endl << one++ << two++;
  153.     cout << endl << "After postincrement expressions:" << endl << one << two;
  154.  
  155.     char c;
  156.     cin >> c;
  157.  
  158.     return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement