Create a class ComplexNumber containing members as: - real - imaginary Write a C++ program to calculate and display the sum of two complex numbers.

//If you are not using Borland or Turbo C then Add the following line after header files :
//using namespace std;

#include<conio.h>
#include<iostream.h>
class complex
{
    int real,imag;
    public:
   void accept()
   {
      cout<<"\nEnter real no: ";
      cin>>real;
      cout<<"\nEnter imaginary: ";
      cin>>imag;

   }
   void dis()
   {
    cout<<"complex no is : "<<real<<" + "<<imag<<" i"<<endl;
   }
  friend void cal(complex &a,complex &b); //decalaration of friend function

   void display()
{
    cout<<"Addition is   : ";
  cout<<real<<" + "<<imag<<" i";
}
};
void cal(complex &a,complex &b)
   {
        //addtion of real and imaginary no
    a.real=a.real+b.real;
        a.imag=a.imag+b.imag;
   }
int main()
{
   complex c1,c2;// creating 2 instance of class complex
   cout<<"Enter details of 1st no";
   c1.accept();
   cout<<"Enter details of 2nd no";
   c2.accept();
   c1.dis(); //display 1st no
   c2.dis(); //display 2nd no
   cal(c1,c2); //passing values to cal function 
   c1.display(); 
   getch();
   return(0);
}

0 Comments