sidrs

OPP Ass 1 - Complex Nos

Jul 29th, 2024 (edited)
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Complex {
  6. private:
  7.     float x; float y;
  8. public:
  9.     Complex(){ x = 0; y = 0; }
  10.     Complex operator +(Complex PARAM){
  11.         Complex tempa;
  12.         tempa.x = x+PARAM.x;
  13.         tempa.y = y+PARAM.y;
  14.         return (tempa);
  15.     }
  16.     Complex operator *(Complex PARAM) {
  17.         Complex temp;
  18.         temp.x = (x*(PARAM.x)-y*(PARAM.y));
  19.         temp.y = (x*(PARAM.y)+y*(PARAM.x));
  20.         return (temp);
  21.     }
  22.     friend istream &operator >> (istream &INPUT, Complex &OBJ){
  23.         cout << "Enter Real Part: "; INPUT >> OBJ.x;
  24.         cout << "Enter Imaginary Part: "; INPUT >> OBJ.y;
  25.     }
  26.     friend ostream &operator << (ostream &OUTPUT, Complex &OBJ){
  27.         OUTPUT << OBJ.x << "+" << OBJ.y << "i" << endl;
  28.     }
  29.  
  30. };
  31.  
  32. int main(){
  33.     Complex c1, c2, c3, c4, c5;
  34.     cout << "\nDefault Initialized Complex Number: "; cout << c1 << endl;
  35.    
  36.     cout << "For the First Complex Number: " << endl; cin >> c2;
  37.     cout << "\nFor the Second Complex Number: " << endl; cin >> c3;
  38.  
  39.     cout << "\nThe First Complex Number is: "; cout << c2;
  40.     cout << "The Second Complex Number is: "; cout << c3;
  41.  
  42.     c4 = c2 + c3;
  43.     cout << "\nSum of these Complex Numbers is: "; cout << c4;
  44.  
  45.     c5 = c2 * c3;
  46.     cout << "Product of these Complex Numbers is: "; cout << c5;
  47.  
  48.  
  49.     cout << endl;
  50.     return 0;
  51. }
  52.  
  53.  
  54. /*
  55.  
  56. student@student:~/Documents/SB57_LAB/OOP/complex$ ./main.exe
  57.  
  58. Default Initialized Complex Number: 0+0i
  59.  
  60. For the First Complex Number:
  61. Enter Real Part: 8
  62. Enter Imaginary Part: 5
  63.  
  64. For the Second Complex Number:
  65. Enter Real Part: 4
  66. Enter Imaginary Part: 3
  67.  
  68. The First Complex Number is: 8+5i
  69. The Second Complex Number is: 4+3i
  70.  
  71. Sum of these Complex Numbers is: 12+8i
  72. Product of these Complex Numbers is: 17+44i
  73.  
  74.  
  75.  */
  76.  
Add Comment
Please, Sign In to add comment