operator overloading in C++ using friend function

namivrm

namivrm

@namivrm-w4iQGm Oct 26, 2024
Hello, this is Nami. I am a student of computer science. While learning C++, I faced a query about operator overloading using friend function. I am pretty clueless about how to code that. Please help me out!

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Kaustubh Katdare

    Kaustubh Katdare

    @thebigk Aug 25, 2009

    namivrm
    operator overloading in C++ using friend function
    Yep, what about it?
  • itchap

    itchap

    @itchap-RKJQDB Aug 25, 2009

    // Using friend functions to
    // overload addition and subtarction
    // operators
    #include <iostream.h>

    class myclass
    {
    int a;
    int b;

    public:
    myclass(){}
    myclass(int x,int y){a=x;b=y;}
    void show()
    {
    cout<<a<<endl<<b<<endl;
    }

    // these are friend operator functions
    // NOTE: Both the operans will be be
    // passed explicitely.
    // operand to the left of the operator
    // will be passed as the first argument
    // and operand to the right as the second
    // argument
    friend myclass operator+(myclass,myclass);
    friend myclass operator-(myclass,myclass);

    };

    myclass operator+(myclass ob1,myclass ob2)
    {
    myclass temp;

    temp.a = ob1.a + ob2.a;
    temp.b = ob1.b + ob2.b;

    return temp;
    }

    myclass operator-(myclass ob1,myclass ob2)
    {
    myclass temp;

    temp.a = ob1.a - ob2.a;
    temp.b = ob1.b - ob2.b;

    return temp;
    }

    void main()
    {
    myclass a(10,20);
    myclass b(100,200);

    a=a+b;

    a.show();
    }
    I think you are asking about operator overloading program in C++ using friend function.
    From now onwards please give the exact details about your query.
  • namivrm

    namivrm

    @namivrm-w4iQGm Aug 26, 2009

    thanks... cn u hlp me in opereator overloading usng member function...
  • itchap

    itchap

    @itchap-RKJQDB Aug 26, 2009

    namivrm
    thanks... cn u hlp me in opereator overloading usng member function...
    Yeah I can help you but first of all stop using this sms text in your posts anywhere on CE.
    Now tell me what do you want to know about operator overloading using member function..
  • itchap

    itchap

    @itchap-RKJQDB Aug 26, 2009

    Overloading operators using a member function is very similar to overloading operators using a friend function. When overloading an operator using a member function:
    The leftmost operand of the overloaded operator must be an object of the class type.
    The leftmost operand becomes the implicit *this parameter. All other operands become function parameters.

    Most operators can actually be overloaded either way, however there are a few exception cases:
    If the leftmost operand is not a member of the class type, such as when overloading operator+(int, YourClass), or operator<<(ostream&, YourClass), the operator must be overloaded as a friend.
    The assignment (=), subscript ([]), call (()), and member selection (->) operators must be overloaded as member functions.

    Overloading the unary negative (-) operator

    The negative operator is a unary operator that can be implemented using either method. Before i show you how to overload the operator using a member function, here’s a reminder of how to overloaded it using a friend function:

    class Cents
    {
    private:
    int m_nCents;

    public:
    Cents(int nCents) { m_nCents = nCents; }

    // Overload -cCents
    friend Cents operator-(const Cents &cCents);
    };

    // note: this function is not a member function!
    Cents operator-(const Cents &cCents)
    {
    return Cents(-cCents.m_nCents);
    }
    Now let’s overload the same operator using a member function instead:

    class Cents
    {
    private:
    int m_nCents;

    public:
    Cents(int nCents) { m_nCents = nCents; }

    // Overload -cCents
    Cents operator-();
    };

    // note: this function is a member function!
    Cents Cents:: operator-()
    {
    return Cents(-m_nCents);
    }
    Note that this method is very similar. However, the member function version of operator- doesn’t take any parameters.A member function has an implicit *this pointer which always points to the class object the member function is working on. The parameter we had to list explicitly in the friend function version (which doesn’t have a *this pointer) becomes the implicit *this parameter in the member function version.

    Remember that when C++ sees the function prototype Cents Cents:😲perator-();, the compiler internally converts this to Cents operator-(const Cents *this), which you will note is almost identical to our friend version Cents operator-(const Cents &cCents)!
  • Saandeep Sreerambatla

    Saandeep Sreerambatla

    @saandeep-sreerambatla-hWHU1M Aug 26, 2009

    Itchap good programs 😀

    A small suggestion dont give all the codes let people try themselves.

    Mr.namivrm I hope you are not using these codes for your assignments!!!
  • itchap

    itchap

    @itchap-RKJQDB Aug 26, 2009

    English-Scared
    Itchap good programs 😀

    A small suggestion dont give all the codes let people try themselves.

    Mr.namivrm I hope you are not using these codes for your assignments!!!
    Ok i will take care of that.😎
  • namivrm

    namivrm

    @namivrm-w4iQGm Aug 27, 2009

    hey... i'll nt use dis code in my assignments.... i had doubt... thanks for ur help....
  • Kaustubh Katdare

    Kaustubh Katdare

    @thebigk Aug 27, 2009

    namivrm
    hey... i'll nt use dis code in my assignments.... i had doubt... thanks for ur help....
    Please refrain from using SMS text in your posts on CrazyEngineers.