FY-3-a Best C++ program to add two complex number

OOP program to add two complex number

Design a class Complex for adding the two complex numbers and also show the
use of constructor.

Click here to get other OOP Practical

FYIT practical 3-a simple constructor program

#include<iostream>
using namespace std;
class complex
{
int a,b;
public:
complex()
{
a=0;
b=0;
}
complex(int m,int n)
{
a=m;
b=n;
}
void show()
{
cout<<endl<<a;
cout<<endl<<b;
}
complex sum(complex c1)
{
complex c3;
c3.a=a+c1.a;
c3.b=b+c1.b;
return c3;
}
};
int main()
{
complex x(2,3);
complex y(4,7);
complex z;
z=x.sum(y);
cout<<"Values of first objects";
x.show();
cout<<endl<<"Values of second objects";
y.show();
cout<<endl<<"Result";
z.show();
return 0;
}

Output

oop-program-to-add-two-complex-number-itvoyagers

Check out more FYIT OOP Practical Program and other logic building programs in C++

We are aiming to explain all concepts of C++ and OOP in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

Leave a Comment