First Principles of C++: Binary Operator Overloading

First Principles of C++: Binary Operator Overloading

Reason for C++ to have operator overloading feature is make programming simple, intuitive and let programmer focus on algorithm not the logic behind it.

C++ compiler handles different operators in different styles like + and ++ is handled differently. In this operator overloading lets take dive deeply to understand the concept in a intuitive way. Think like a compiler.

For a simple addition of two integer numbers c = a + b is enough.

But for the addition of two complex number a class or struct will help o program and maintain here is the example,

#include <iostream>

class Complex {
private:
    double real;
    double imaginary;
public:
    Complex(double r=0, double i=0): real(r), imaginary(i) {
    }
    
    void display(){
        std::cout <<real << "+" <<imaginary<< "i"<<std::endl;
    }
    /***** Operator overloading *****/
    Complex operator+(const Complex& rhs) const{
        Complex temp;
        temp.real = this->real + rhs.real;
        temp.imaginary = this->imaginary + rhs.imaginary;
        return temp;
    }
};

int main() {
    Complex c1(3,5);
    Complex c2(10,15);
    c1.display();

    Complex c3;
    c3 = c1 + c2 ;
    c3.display();

    return 0;
}        

Simple Explanation: Created variable c1 of type Complex with value 3+5i, and c2 with value 10+15i.

Adding then simply as "c1+c2" results in 13+20i stored in c3, and the same is displayed.

So c1 + c2 is more intuitive than calling the method to add 2 complex number like "add(c1, c2)".

Lets see the simple explanation first,

c3 = c1 + c2 // This line is perceived as c1.operator+(c2) by the compiler.

At this instant, operator+() method is called, data member of c1 that is accessed using "this" operator, and data member of c2 is passed as argument reference.

Result of addition returned back whose datatype is again Complex.

 Complex operator+(const Complex& rhs) const{
        Complex temp;
        temp.real = this->real + rhs.real;
        temp.imaginary = this->imaginary + rhs.imaginary;
        return temp;
    }        

First Principle Explanation: How, compiler preserves addition of two objects and call its corresponding overloaded function.

Code of operator+ method is stored in code segment, when compiler see the line

"c3 = c1 + c2". It has 4 parts. return type | operator + | LHS | RHS

so compiler look for operator+ overloaded method in the code segment, with matching the prototype.

return type is Complex | operator + | LHS is c1 | RHS is c2

Once the match method is found, and same is called function is called. since LHS is c1, operator+ method is called with implicit c1 as first argument and c2 as second argument. Where c1 values are accessed using 'this' operator.

Shows how Compiler generates assembly instruction of operator overloaded method

The result of operator+ () is stored in local variable 'temp' is returned, means copied to c3 in main().


Bonus:

Now a days compilers are very intelligent and optimized, it can perform some implicit operation at the same time logically convincible not burdening the programmer.

Lets see one example,

 int main() {
    Complex c1(3,5);
    Complex c2(10,15);
    c1.display();

    Complex c3;
    c3 = c1 + 10 ;   // passing a integer number as RHS
    c3.display();

    return 0;
}        

here c3 = c1 + 10. Initially looks like compiler Error. But compiles fine.

The integer 10 is implicitly converted to a Complex object. Creating a arbitrary object Complex(10, 0). This is possible because the Complex class has a constructor that can take a single double (or int, because int can be promoted to double) and default imaginary to 0.

    Complex(double r=0, double i=0): real(r), imaginary(i) {
    }        


If there is no default initializer values then "c3 = c1 + 10 ; " will be a Error.

  Complex(double r, double i): real(r), imaginary(i) {    }        

要查看或添加评论,请登录

M Vijaynath的更多文章

社区洞察

其他会员也浏览了