Implement a class Complex which represents the Complex Number data type. Implement the following operations: 1. Constructor (including a default constructor which creates the complex number 0+0i). 2. Overloaded operator+ to add two complex numbers. 3. Overloaded operator* to multiply two complex numbers. 4. Overloaded << and >> to print and read Complex Numbers.

Implement a class Complex which represents the Complex Number data type. Implement the following operations:
1. Constructor (including a default constructor which creates the complex number 0+0i).
2. Overloaded operator+ to add two complex numbers.
3. Overloaded operator* to multiply two complex numbers.
4. Overloaded << and >> to print and read Complex Numbers.

#include
OUTPUT FOR THE PROGRAM
using namespace std;
class complex
{
   public:
         float real,img;
         complex()
         {
           real = 0;
           img = 0;
         }
     
     
        friend ostream & operator <<(ostream& ,complex& );
        friend istream & operator >>(istream& ,complex& );
        friend complex operator +(complex &c1,complex&a1);
        friend complex operator -(complex &c2,complex&a2);
        friend complex operator *(complex &c3,complex&a3);
        friend complex operator /(complex &c4,complex&a4);
};

  istream & operator >>(istream&in ,complex&obj )
  {
    cout<<"Enter the real part: ";
    in>>obj.real;
    cout<<"Enter the imaginary part: ";
    in>>obj.img;
    return in;
  }

  ostream & operator <<(ostream& out ,complex& obj )
  {
    cout<<"Complex No is :";
    out<    out<<"+"<<"("<    return out;
  }

  complex operator +(complex &c1,complex&a1)
  {
    complex r1;
    r1.real=c1.real+a1.real;
    r1.img=c1.img+a1.img;
    return r1;
  }

  complex operator -(complex &c2,complex&a2)
  {
    complex r2;
    r2.real=c2.real-a2.real;
    r2.img=c2.img-a2.img;
    return r2;
  }

  complex operator *(complex &c3,complex&a3)
  {
    complex r3;
    r3.real=c3.real*a3.real-c3.img*a3.img;
    r3.img=c3.real*a3.img+c3.img*a3.real;
    return r3;
  }

  complex operator /(complex &c4,complex&a4)
  {
    complex r4;
    r4.real=(c4.real*a4.real+c4.img*a4.img) / (a4.real*a4.real+c4.img*c4.img);
    r4.img=(-c4.real*a4.img+c4.img*a4.real) / (a4.real*a4.real+c4.img*c4.img);
    return r4;
  }
int main()
{
    complex s,a,b,add,sub,mul,div;
    cout<    cout<<"Enter First Complex No:\n";
    cin>>a;
    cout<<"Enter Second Complex No:\n";
    cin>>b;
  int ch;
  char ans ,y,Y;
   do
   {
        cout<<"\nMENU:"<        cout<<"1.Addition"<        cout<<"2.Substraction"<        cout<<"3.Multiplication"<        cout<<"4.Division"<        cout<<"Enter your Choice: ";
        cin>>ch;

     switch(ch)
     {
        case 1:
            add=a+b;
            cout<<"Addition of ";
            cout<            break;
        case 2:
            sub=a-b;
            cout<<"Substraction of ";
            cout<            break;
        case 3:
           mul=a*b;
           cout<<"Multiplication of ";
           cout<           break;
        case 4:
            div=a/b;
            cout<<"Division of ";
            cout<
            break;
       default:
            cout<<"Please Enter valid choice";
            break;
     }
     cout<<"\n Do you want to continue (y/n): ";
     cin>>ans;
  }
  while(ans=='y'||ans=='Y');

  return 0;
}


For more such posts click the link:-http://svencrai.com/G8W


Comments

Popular posts from this blog

A book shop maintains the inventory of books that are being sold at the shop. The list includes details such as author, title, price, publisher and stock position. Whenever a customer wants a book, the sales person inputs the title and author and the system searches the list and displays whether it is available or not. If it is not, an appropriate message is displayed. If it is, then the system displays the book details and requests for the number of copies required. If the requested copies book details and requests for the number of copies required. If the requested copies are available, the total cost of the requested copies is displayed; otherwise the message ?Required copies not in stock? is displayed. Design a system using a class called books with suitable member functions and Constructors. Use new operator in constructors to allocate memory space required. Implement C++ program for the system

Imagine a publishing company which does marketing for book and audiocassette versions. Create a class publication that stores the title (a string) and price (type float) of a publication. From this class derive two classes: book, which adds a page count (type int), and tape, which adds a playing time in minutes (type float). Write a program that instantiates the book and tape classes, allows user to enter data and displays the data members.

Write C++ program for storing appointment schedule for day. Appointments are booked randomly using linked list. Set start and end time and min and max duration for visit slot. Write functions for- a)Display free slots b)Book appointment c)Cancel appointment ( check validity, time bounds, availability etc) d)Sort list based on time e)Sort list based on time using pointer manipulation