Write a c++ program to create complex class and use array of object to print and writing value

 

This c++ program will explain how to read and print multiple complex numbers using Array.


 //this code read and display two complax number and sum those complx number,Use array object in class  
 #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,i;  
     cout<<"Enter real value :"<<endl;cin>>r;  
     cout<<"Enter imaginry value "<<endl;  
     cin>>i;  
     real=r;imag=i;  
   }  
   void print()//Print function print values of complex number  
   {  
     cout<<real<<"+"<<imag<<"i"<<endl;  
   }  
 };  
 int main()  
 {  
  Compx c1[10];  // array contain 10 values   
  int i,n;  
  cout<<"How many complex number you want to enter "<<endl;  
  cin>>n; //Complex number  
  for (i=0;i<n;i++)// this for loop for getting value  
  {  
    cout<<"Enter complex number :"<<i+1<<endl;  
    c1[i].GetValue();  
  }  
  for(i=0;i<n;i++)//this for loop is for printing values  
  {  
    c1[i].print();  
  }  
  return 0;  
 }  
Output
How many complex number you want to enter
5
Enter complex number :1
Enter real value :     
5
Enter imaginry value 
6
Enter complex number :2
Enter real value :     
4
Enter imaginry value 
9
Enter complex number :3
Enter real value :     
7
Enter imaginry value 
5
Enter complex number :4
Enter real value :     
2
Enter imaginry value
6
Enter complex number :5
Enter real value :
7
Enter imaginry value 
9
5+6i
4+9i
7+5i
2+6i
7+9i

0 Comments