Featured Post

Trie implementation in C

Operator Overloading

A simple example of operator overloading

#include<iostream>

using namespace std;

class Dollar
{
private:
    int dlr;
public :
    Dollar(int dollar = 0)
    {
        dlr = dollar;
    }
    friend Dollar operator +(const Dollar &oper1, const Dollar &oper2);

    int getDollars()
    {
        return dlr ;
    }
};

Dollar operator +(const Dollar &oper1, const Dollar &oper2)
{
    return ( oper1.dlr + oper2.dlr );
}

int main()
{
    Dollar d1(4);
    Dollar d2(6);


    cout<< (d1 + d2).getDollars();
    return 0;
}

Comments