Wednesday 26 June 2013

Complex number arithmetic operation-using c++(inheritance)

         Program for add and multiply complex numbers implemented using hierarchical inheritance and also using        

                                   constructor


#include<iostream.h>
#include<conio.h>
 
class A     //Base class
{
    public:
    double a,b;
    void getnumber()     // Member function
    {
           cout<<"\n\n\tEnter Number for complex number:::\n";
           cin>>a>>b;
           cout<<"\n\tComplex number is :::\t";
           if(b>0)
           cout<<a<<"+"<<b<<"i";
           else
           cout<<a<<b<<"i";
    }
};
 
class B : public A    // Derived class 1
{
    public:
    B(double a,double b,double c,double d) //parameterized constructor
    {
 
        if((b+d)>0)
        cout<<"\n\nAddition of complex number :::\t"<<(a+c)<<"+"<<(b+d)<<"i";
        else
        cout<<"\n\nAddition od complex number :::\t"<<(a+c)<<(b+d)<<"i";
 
    }
 
};
 
class C : public A   // Derived class 2
{
    public:
    C(double a,double b,double c,double d) //parameterized constructor
    {
 
        if(((a*d)*(b*c))>0)
    cout<<"\n\nMultiplication of complex number :::\t"<<((a*c)+((b*d)*(-1)))<<"+"<<(a*d)+(b*c)<<"i";
        else
    cout<<"\n\nMultiplication of complex number :::\t"<<((a*c)+((b*d)*(-1)))<<(a*d)+(b*c)<<"i";
 
    }
};
 
int main()
{
    clrscr();
    A a1;     // a1 is object of class A
    a1.getnumber();  // It call member function ( getnumber() ) of class A
    A a2;     // a2 is another object of class A
    cout<<"\n\n---------------------------------------------------";
    a2.getnumber(); // It call member function ( getnumber() ) of class A
    cout<<"\n\n---------------------------------------------------";
    B(a1.a,a1.b,a2.a,a2.b);  // It call class B constructor
    cout<<"\n\n---------------------------------------------------";
    C(a1.a,a1.b,a2.a,a2.b);  // It call class C constructor
    cout<<"\n\n---------------------------------------------------";
 
    getch();
}
/*
If 10+20i is complex number then
a1.a=10  and  a1.b=20
and another complex number for add and multiply is 30+40i then
a2.a=30  and  a2.b=40
*/






No comments:

Post a Comment