Sunday, May 8, 2022

friend function overloading over normal operator overloading

 /*program to show use of friend function operator overloading over normal

operator overloading using methods
a pro of using friend operator overloading instead of normal is that
we can use it to overload for simple datatypes like class obj and int datatype
which aint possible with normal operator overloading
*/
#include <iostream>
using namespace std;
class A
{
    int num1;

public:
    A() {}
    A(int num1)
    {
        this->num1 = num1;
    }
    friend int operator+(A &, int);
};
int operator+(A &a, int num2)
{
    return a.num1 + num2;
}

int main()
{
    A a(2);
    int num2 = 4;

    cout << a + num2;

    return 0;
}

No comments:

HackCache ’26: A 48-Hour Online Hackathon Open to Every Student

  Hackathons are often associated with computer science students, experienced developers, and teams that already know exactly what they are...

Popular Posts