Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8440003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:09:50+00:00 2026-06-10T08:09:50+00:00

I have a Fraction class as following. class Fraction { int num, den ;

  • 0

I have a Fraction class as following.

class Fraction
{
  int num, den ;

  public:
   //member functions here
} ;

I read in some book, I think ‘effective c++’ that it is better to overload the addition operator as non member function. The reason given there was that it allows commutative addition.
This is the prototype of my overloaded function for addition operator.

Fraction operator + (const Fraction &obj, const int add_int) ;

Here, when i call it, I have to do it like this.

f1 + 2 ;

But it won’t work this way.

2 + f1 ;

For that i would have to write the function again and change the order of parameters in that.

I want to know that whether there is a method by which I can overload function a single time and perform commutative addition?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:09:52+00:00Added an answer on June 10, 2026 at 8:09 am

    You can, but only if your class has an implicit constructor from the type you want to add to, so this would work:

    class Fraction
    {
        int num, den;
    
    public:
        Fraction(int n) :num(n), den(1) {}
    
        // member functions here
    };
    
    Fraction operator+(Fraction lhs, Fraction rhs) { ... }
    
    int main()
    {
        Fraction f1(5);
        f1 = f1 + 5;
        f1 = 5 + f1;
    }
    

    Of course, this allows a usage that you didn’t actually mention you wanted, which is the ability to add two Fraction objects.

    Fraction f1(1), f2(2);
    Fraction f3 = f1 + f2
    

    I can’t imagine you would want to disallow that though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following structure: [StructLayout(LayoutKind.Sequential)] public struct TCurve { public int fNumItems; /*
I have a class called fraction, and I'm declaring some operators as friends. I
In a Silverlight 4 project I have a class that extends Canvas: public class
For example: Here's a simple class. class Hero { public string faction; public string
I have an Extension class for handling general functions that's designed to be used
Let's imagine I have Fraction class. So, the correct way to create instance of
I have a Fraction class, works fine within its own yard, like 1/2+1/3=5/6 or
I am new to obj-c programming, I read Kochan's book. here is code from
I've got an enum and a Foo class: public enum Type { Arithmetic, Fraction,
Let's say I have a Fraction class: class Fraction { ... /** Invert current

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.