Skip to content

Instantly share code, notes, and snippets.

@dibakarsutradhar
Created September 8, 2018 18:52
Show Gist options
  • Save dibakarsutradhar/64ce9d9d67a4290e1320ce2829a4a702 to your computer and use it in GitHub Desktop.
Save dibakarsutradhar/64ce9d9d67a4290e1320ce2829a4a702 to your computer and use it in GitHub Desktop.
#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