0% found this document useful (0 votes)
15 views

Complex Addition Operator Overloading

Uploaded by

gurly101
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Complex Addition Operator Overloading

Uploaded by

gurly101
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

//EXP 17.

1 Complex Number addition operator overloading


//ComplexAdditionOperatorOverloading.cpp
#include<iostream.h>

class complex {
public:
int real;
int img;
complex() {
real=img=0;
}
complex(int x,int y) {
real=x;
img=y;
}
void show() {
cout<<"\n"<<real<<"+"<<img<<"i";
}
friend complex operator+(complex c, complex d);
}; //end of class complex

//operator+ is not part of complex class so have 2 args for + operator overload.
complex operator+(complex c, complex f) {
complex ans;
ans.real = c.real+f.real;
ans.img = c.img+f.img;
return(ans);
}

int main() {
complex x(1, 2), y(0, 7);
complex c = x + y; //overloaded + is called here
c.show();
} // end of main

You might also like