class Complex
{
private:
float real, imaginary;
public:
Complex(float real, float imaginary)
{
this->real = real;
this->imaginary = imaginary;
}
float getReal() const
{
return this->real;
}
float getImaginary() const
{
return this->imaginary;
}
Complex operator+(Complex other)
{
return Complex(this->real + other.real, this->imaginary + other.imaginary); // Here I can access other class' private member without using getter methods.
}
};
int main()
{
Complex first(5, 2);
Complex second(7, 12);
Complex result = first + second; // No error
std::cout << result.getReal() << " + " << result.getImaginary() << "i"; // But here I have to use getters in order to access those members.
return 0;
}
Why I am getting access to a class' private members from another class? I thought I have to use getters to access other class' private members. But it turns out, that if I call it from another class, it no longer requires those getters and I can access them directly. But this is not possible outside of that class.