Search This Blog

Read Your Language

Write a C++ program to find the sum of two complex numbers.

Write a C++ program to find the sum of two complex numbers. Define proper
class, constructor and method(s) in your program. Give comments to make your
code more understandable.
#include<iostream>
using namespace std;
class complex_no       // create a class  of complex number 
{
private:              /*create a  private member variable or function cannot be accessed from outside the class only friend functions can access private members.*/

    float real_no;       // create a flaot data type  real integer number 
    float imag_no;       // create a flaot data type  imagenary number 
public:           // create a  public  member variable or function can be any accessed from outside the class.

 complex_no() // call the function complex_no()
{
    
}
    complex_no(float r, float i)

    {

real_no= r;

imag_no= i;

    }
    complex_no sum(complex_no c)
    {

complex_no t;

t.real_no = real_no + c.real_no;

t.imag_no = imag_no+ c.imag_no;

return t;

    }
 void disp()
 {
cout<<real_no<<" + "<<imag_no<<endl;
 }

};
int main()
{
complex_no C1(3.5,6.1),C2(3.5,6.5),C3;
C3=C1.sum(C2);
cout<<"\n complex Number 1 = ";
C1.disp();
cout<<"\n complex Number 2 = ";
C2.disp();
cout<<"\n complex Number 3 = ";
C3.disp();
return 0;
}


Output: 
complex Number 1 = 3.5  +  6.1
complex Number 2 = 3.5  +  6.5
complex Number 3 =  7    + 12.6


No comments:

Contact Form

Name

Email *

Message *

Followers