You must Sign In to post a response.
  • Category: Programming

    C++ program to add two complex numbers using binary operator overloading


    Having difficulty in preparing for Object Oriented Programming using C++ BCA Exam? Interested in getting problem solution for usage of operator overloading for operations on complex numbers? Find responses from ISC experts on this page and decide how to prepare further.

    Following is a question that was asked in the question paper of BCA(December 2016)conducted by IGNOU, for subject Object Oriented Programming using C++(CS-72).

    This question is asked for 5 marks and hence requires some explanation along with the program code. Please let me know the solution.

    Q) Write a program in C++ to add two complex numbers using binary operator overloading.
    [5 Marks]
  • Answers

    1 Answers found.
  • Please add iostream.h and conio.h header files before declaration of class complex.

    //PROGRAM TO PERFORM ADDITION OF TWO COMPLEX NUMBERS USING OPERATOR OVERLOADING
    #include
    #include
    class complex
    {
    private:
    float real,imag;
    public:
    complex(){}
    complex(float x, float y)
    {
    real=x;
    imag=y;
    }
    void display(void)
    {
    cout<<"\n"< }
    complex operator +(complex m)
    {
    class complex temp;
    temp.real=real+m.real;
    temp.imag=imag+m.imag;
    return(temp);
    }
    };
    void main(void)
    {
    class complex t1(3.3,4.4);
    class complex t2(5.2,2.3);
    class complex t3;
    cout<<"\nFirst complex number is ";
    t1.display();
    cout<<"\nSecond complex number is ";
    t2.display();
    t3=t1+t2;
    cout<<"\naddition of above two comples numbers is ";
    t3.display();
    getch();
    }


  • Sign In to post your comments