Advertisement
Eternoseeker

OOP 5th ass

Nov 11th, 2022
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | Source Code | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class complex{
  5. public:
  6.     float real,imag;
  7.     complex(){
  8.         real=0;
  9.         imag=0;
  10.     }
  11.     complex operator +(complex);
  12.     complex operator -(complex);
  13.     complex operator *(complex);
  14.     complex operator /(complex);
  15.     friend ostream &operator <<(ostream &output,complex &t)
  16.     {
  17.         output<<t.real<<"+"<<t.imag<<"i\n";
  18.         return output;
  19.     }
  20.     friend istream &operator >>(istream &input,complex &nn)
  21.     {
  22.     cout<<"Enter the real part:";
  23.     input>>nn.real;
  24.     cout<<"Enter the imaginary part";
  25.     input>>nn.imag;
  26.     return input;
  27.     }
  28. };
  29.  
  30. complex complex::operator +(complex n2){
  31.     complex n3;
  32.     n3.real=real+n2.real;
  33.     n3.imag=imag+n2.imag;
  34.     return(n3);
  35. }
  36. complex complex::operator -(complex n2){
  37.     complex n3;
  38.     n3.real=real-n2.real;
  39.     n3.imag=imag-n2.imag;
  40.     return n3;
  41. }
  42. complex complex::operator *(complex n2){
  43.     complex n4;
  44.     n4.real=(real*n2.real)-(imag*n2.imag);
  45.     n4.imag=(imag*n2.real)+(real*n2.imag);
  46.     return (n4);
  47. }
  48. complex complex::operator /(complex n2){
  49.     complex n3;
  50.     int temp=n2.real*n2.real+n2.imag*n2.imag;
  51.     n3.real=(real*n2.real+n2.imag*imag)/temp;
  52.     n3.imag=(imag*n2.real-n2.imag*real)/temp;
  53.     return n3;
  54. }
  55. int main(){
  56.     complex c1,c2,c3,c4,c5,c6;
  57.      cout<<"Default constructor value=\n";
  58.      int ch=10;
  59.      cout<<c1;
  60.     do{
  61.         cout<<"Enter 1 to calculate sum of complex number\n";
  62.         cout<<"Enter 2 to calculate difference of complex numbers\n";
  63.         cout<<"Enter 3 to calculate multiplication of two complex numbers\n";
  64.         cout<<"Enter 4 to calculate division of two complex numbers\n";
  65.         cout<<"Enter 0 to exit\n";
  66.         cout<<"Enter your choice:\n";
  67.         cin>>ch;
  68.         switch(ch){
  69.         case 1:
  70.             cout<<"Enter the details for first number\n";
  71.             cin>>c1;
  72.             cout<<"Enter the details for second number\n";
  73.             cin>>c2;
  74.             c3=c1+c2;
  75.             cout<<c3;
  76.             break;
  77.         case 2:
  78.             cout<<"Enter the details for first number\n";
  79.             cin>>c1;
  80.             cout<<"Enter the details for second number\n";
  81.             cin>>c2;
  82.             c3=c1-c2;
  83.             cout<<"Subtraction of "<<c1<<" and "<<c2<<" is "<<c3;
  84.             break;
  85.         case 3:
  86.             cout<<"Enter the details for first number\n";
  87.             cin>>c1;
  88.             cout<<"Enter the details for second number\n";
  89.             cin>>c2;
  90.             c3=c1*c2;
  91.             cout<<c3;
  92.             break;
  93.         case 4:
  94.             cout<<"Enter the details for first number\n";
  95.             cin>>c1;
  96.             cout<<"Enter the details for second number\n";
  97.             cin>>c2;
  98.             c3=c1/c2;
  99.             cout<<c3;
  100.             break;
  101.         default:
  102.             cout<<"Enter proper choice!!"<<endl;
  103.             break;
  104.         }
  105.     }while(ch!=0);
  106.     return 0;
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement