vbcnsm,a.s

C++ Program to Add to Complex Numbers

The program has initialized complex value 2.3,4.2 and1.2,3.5 to take input use cin add below code after complex c1,c2,c3,


float num1,num2,num3,num4;
    std::cin>>num1>>num2>>num3>>num4;
    c1=complex(num1,num2);
    c2=complex(num3,num4);
and comment out the initialized value



Sample Input 1
2.3
4.2
1.2
3.5
Sample Output 1
2.3+i4.2
1.2+i3.5
3.5+i5.4
Sample Input 2
2.4
6.5
8.9
3.5
Sample Output 2
2.4+i6.5
8.9+i3.5
11.3+i15.4

Solution

#include<iostream>
#include<conio.h>
class complex
{
    float x,y;
    public:
    complex()
    {

    }
    complex(float real,float imag)
    {
        x=real;
        y=imag;
    }
    complex operator+(complex c);
    void display();
};
complex complex::operator+(complex c)
{
    complex temp;
    temp.x=x+c.x;
    temp.y=y+c.x;
    return(temp);
}
void complex::display(void)
{
    std::cout<<x<<"+i"<<y<<"\n";
}
int main()
{
    complex c1,c2,c3;
    c1=complex(2.3,4.2);
    c2=complex(1.2,3.5);
    c3=c1+c2;
    c1.display();
    c2.display();
    c3.display();
    return 0;
}

Post a Comment

© CodeBro. All rights reserved. Distributed by Pixabin