adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C++ to add Two complex number using class

Program in C++ to add Two complex number using class


Source Code
#include<iostream>
#include<conio.h>
  using namespace std;
  class complex
   {
     int real;
     int imag;
   public:
      complex()
     {
      real=4;
      imag=6;
     }
     complex(int r,int i)
     { 
       real=r;
       imag=i;
  }
     complex(complex &x)
     {
      real=x.real;
      imag=x.imag;
     } 
    ~complex() {}
     void input()
     {
      cout<<"\nEnter real and imaginary part :-";
      cin>>real>>imag;
     }
    void show()
     {
       cout<<real<<" + "<<imag<<"i ";
     }
       
    void add(complex x)
     {
      real=real+x.real;
      imag=imag+x.imag;
     }
   };
   int main()
    {
    
    complex c1(4,6);
    cout<<"\n1st complex no. ";
    c1.show();
    complex c2(3,7);
    cout<<"\n2nd complex no. ";
    c2.show();
    c1.add(c2);
    cout<<"\nSum of two complex nos. ";
    c1.show();
     
    complex c3,c4;
    c3.input();
    c4.input();
    cout<<"\n1st complex no. ";
    c3.show();
    cout<<"\n2nd complex no. ";
    c4.show();
    c3.add(c4);
    cout<<"\nSum of two complex nos. ";
    c3.show();
    return 0;
    }
Output:-




Post a Comment

0 Comments