operator overloading in C++ using friend function

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

  • Kaustubh Katdare
    Kaustubh Katdare
    namivrm
    operator overloading in C++ using friend function
    Yep, what about it?
  • itchap
    itchap
    // Using friend functions to
    // overload addition and subtarction
    // operators
    #include

    class myclass
    {
    int a;
    int b;

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

    // 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
    thanks... cn u hlp me in opereator overloading usng member function...
  • itchap
    itchap
    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
    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
    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
    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
    hey... i'll nt use dis code in my assignments.... i had doubt... thanks for ur help....
  • Kaustubh Katdare
    Kaustubh Katdare
    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.

You are reading an archived discussion.

Related Posts

Do you use your Core 2 Duo, for surfing the internet, making presentations or replying to e-mails? You can use the idle time on your computer (Windows, Mac, Linux) to...
hi frnz...!!!i m stdnt of b.tech1 7th sem..i hv 2 submit a mini prjct so i wnt 2 knw 4rm u wht tpc shud i hv 2 choose..
1. The 1st male singer who won d NATIONAL AWARD....???? a)Mahendra kapoor b)Mohd Rafi c)Mukesh d)Manna De 2. 1st male tourist who went 2 space just 2 visit dere by...
Im final year student of civil engineering desperate to find a hot topic for project in structural engineering... anybody help please 😕😕😕...
A fullerene is a formation of little parts of carbon that is neither diamond nor graphite. During the past it was used for protective covering for soldier at the same...