My point in a code is that I want to overload the + operator, so I could add two class instances. For example, consider a class that has 2 double members, Real and Complex. Class is called Complex and I have created 2 instances, c1 and c2. Then I want to create a c3 instance which is the addition of values of c1 and c2. I have a copy constructor and + operator overloader. (Copy, so I can assign the new instance made by the + overloader to c3): Here is my class:
class Complex {
double real;
double imaginary;
public:
Complex();
Complex(double real, double imaginary);
~Complex();
Complex(const Complex &other);
Complex operator+(const Complex &other, const Complex &other2);
};
And the CPP file:
Complex Complex::operator+(const Complex &other1, const Complex &other2){
return(Complex(other1.real+other2.real, other1.imaginary+other2.imaginary));
}
Complex::Complex(){};
Complex::Complex(double real, double imaginary): real(real), imaginary(imaginary){};
Complex::~Complex(){};
Complex::Complex(const Complex &other){
this->real = other.real;
this->imaginary = other.imaginary;
}
I know that I can create a friend operator but I want to keep everything in a class. No reason for that, just I like it that way.