Created
September 8, 2018 18:52
-
-
Save dibakarsutradhar/64ce9d9d67a4290e1320ce2829a4a702 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class Complex{ | |
private: | |
int real, imag; // imag for imagination | |
public: | |
Complex(int r = 0, int i = 0) {real = r; imag = i;} | |
// This is automatically called when '+' is used between two Complex Objects | |
Complex operator + (Complex const & obj) { | |
Complex res; | |
res.real = real + obj.real; | |
res.imag = imag + obj.imag; | |
return res; | |
} | |
void print() { | |
cout << real << " + i " << imag << endl; | |
} | |
}; | |
int main() { | |
Complex n1(1, 2), n2(4, 5); | |
Complex n3 = n1 + n2; | |
n3.print(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment