Â
In this program, we are going to display complex numbers using polymorphism in C++ Programming Language.Â
Â
The Code given below can be used in gcc/g++ Compilers: -
#include <iostream>
using namespace std;
class Complex
{
private:
int real, imag;
public:
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}
Complex operator+(Complex const &obj)
{
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print()
{
cout << real <<" + "<< imag << "i"<< endl;
}
};
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
c3.print();
return 0;
}
//.......Coded by RISHAB NAIR
The Code given below can be used in Turbo C++ Compilers: -
#include <iostream.h>
#include <conio.h>
class Complex
{
private:
int real, imag;
public:
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}
Complex operator+(Complex const &obj)
{
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print()
{
cout << real <<" + "<< imag << "i"<< endl;
}
};
void main()
{
clrscr();
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
c3.print();
getch();
}
//.......Coded by RISHAB NAIR
#ENJOY CODING
Â
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP