Adding two complex number using class program in C++


In this program, we will read and add two complex numbers using class and object.

Adding two complex number using a class program in C++. , and perofrming atjotj,etic operation in complex number.

                                    Real_part+ImagainaryPart*i
Step
  • Use int variable to represent the private data of class.
  • provide constrictor function to enable object.Use two constrictor one with parameter and one is default constrictor.
  • Inside public define methods to get Complex number and set value into attribute ,method for printing number and method for sum two complex number.
 //this code read and display two complax number and summ those complx number  
 #include<iostream >  
 using namespace std;  
 class Compx   
 {  
   private:  
     int real; //real number is int type  
     int imag;// imagainary number is also int type  
 public:  
   Compx(){};//Constructor taking no parmeter  
   Compx(int r, int i){real=r;imag=i;} //Constructor initinalized value  
   void GetValue(int r,int i)  
   {  
     real=r;imag=i;  
   }  
   void print()//Print function print values of complex number  
   {  
     cout<<real<<"+"<<imag<<"i"<<endl;  
   }  
   Compx add(Compx c2)  
   {  
     Compx temp;  
     temp.real=real+c2.real;  
     temp.imag=imag+c2.imag;  
     return temp;  
   }  
 };  
 int main()  
 {  
   Compx c1,a;   
   Compx c2(4,5);  
   // c1.print();  
   c1.GetValue(11,22);  
   c1.print();  
   c2.print();  
    a= c1.add(c2);  
    cout<<"After adding two complex number "<<endl;  
   a.print();  
   return 0;  
 }  
Output
11+22i
4+5i
After adding two complex number
15+27i

0 Comments