I'm a bit new to object oriented programming in c++ and I've been trying to overload subtraction(-) operator in c++ for a Complex class I created. It is working fine except my program is terminating abnormally.
Below is what I've been trying to do:
#include<iostream>
#include<cstdlib>
class Complex{
//Data-members
private:
int re, im;
//methods
public:
//Constructor
Complex(){ /*default Constructor*/ }
Complex(const int& re_, const int& im_):re(re_), im(im_){}
//Subtraction(-) operator overloading
Complex operator-(const Complex& op)
{
Complex res(this->re - op.re, this->im - op.im);
return res;
}
//get-set methods for re
int getReal(){ return re; }
void setReal(const int& re){ this->re = re; }
//get-set methods for im
int getImaginary(){ return im; }
void setImaginary(const int& im){ this->im = im; }
//Destructor
~Complex(){ free(this); }
};
int main()
{
Complex a(2, 3), b(3, 5);
Complex d = a - b;
std::cout<<"d.re = "<<d.getReal()<<" d.im = "<<d.getImaginary()<<"\n";
return 0;
}
Can anyone please explain the cause of error.