Download Our Beta Android App And Help Us Build Awesome Stuff!  Download Now.

Complex Class - Constructor overloading and Addition

Category: Notes and Practical Files

Course: B.Tech.

Semester: 4

#include <iostream>

using namespace std ;

class complx
{
      int alpha, beta ;

      public:
            complx( )
            {
                  alpha = beta = 10 ;
            }
            complx( int value )
            {
                  alpha = beta = value ;
            }
            complx( int val1, int val2 )
            {
                  alpha = val1 ;
                  beta = val2 ;
            }
            void show( ) ;
            complx add( complx C1 ) ;
};

void complx::show( )
{
      cout<<"\nComplex number is : "<<alpha<<" + i"<<beta<<endl ;
}

complx complx::add( complx C1 )
{
      return complx( alpha+C1.alpha , beta+C1.beta ) ;
}


int main( )
{
      int value, val1, val2 ;

      cout<<"\nCreating Object w/o taking I/p " ;
      complx C1 ;
      C1.show( ) ;

      cout<<"\nEnter one Value : " ;
      cin>>value ;
      complx C2( value ) ;
      C2.show( ) ;

      cout<<"\nEnter Two Values : " ;
      cin>>val1>>val2 ;
      complx C3( val1, val2 ) ;
      C3.show( ) ;

      cout<<"\n\nNow Doing Addition ------> " ;
      cout<<"\nFirst Complex Number : " ;
      C2.show( ) ;
      cout<<"\nSecond Complex Number : " ;
      C3.show( ) ;

      complx C4 = C3.add( C2 ) ;
      cout<<"\n\nFinal result :" ;
      C4.show( ) ;
      
      return 0 ;
}

Added by StAn

Comments