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 8973427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:26:38+00:00 2026-06-15T18:26:38+00:00

I want to know if the following code which is meant to do operations

  • 0

I want to know if the following code which is meant to do operations on rational numbers actually already overloads assignment operations and stream insertion operator << to print objects. I’m not very good with C++ so this is new to me but from what I can tell I think it already does.

    /*
 *
 *  C++ version
 *
 */

/* rational.h */

#ifndef RATIONAL_H
#define RATIONAL_H

#include <iostream>

using std::ostream;

struct rational {

    rational(int = 0, int = 1);

    rational operator+(const rational &) const;
    rational operator-(const rational &) const;
    rational operator*(const rational &) const;
    rational operator/(const rational &) const;

    rational operator+(int) const;
    rational operator-(int) const;
    rational operator*(int) const;
    rational operator/(int) const;

    friend rational operator+(int, const rational &);
    friend rational operator-(int, const rational &);
    friend rational operator*(int, const rational &);
    friend rational operator/(int, const rational &);

    friend ostream &operator<<(ostream &, const rational &);

private:

    int den;
    int num;
};

#endif /* RATIONAL_H */

/* rational.cc */

#include <iostream>
#include "rational.h"

rational::rational(int num, int den) : num(num), den(den) {}

rational rational::operator+(const rational &o) const {

    return rational(num * o.den + o.num * den, den * o.den);
}

rational rational::operator+(int n) const {

    return rational(num + n * den, den);
}

rational rational::operator-(const rational &o) const {

    return rational(num * o.den - o.num * den, den * o.den);
}

rational rational::operator-(int n) const {

    return rational(num - n * den, den);
}

rational rational::operator*(const rational &o) const {

    return rational(num * o.num, den * o.den);
}

rational rational::operator*(int n) const {

    return rational(num * n, den);
}

rational rational::operator/(const rational &o) const {

    return rational(num * o.den, den * o.num);
}

rational rational::operator/(int n) const {

    return rational(num, den * n);
}

rational operator+(int n, const rational &o) {

    return o + n;
}

rational operator-(int n, const rational &o) {

    return rational(n) - o;
}

rational operator*(int n, const rational &o) {

    return o * n;
}

rational operator/(int n, const rational &o) {

    return rational(n) / o;
}

ostream &operator<<(ostream &out, const rational &o) {

    out << '(' << o.num << " / " << o.den << ')';
    return out;
}

/* main.cc */

#include <iostream>
#include "rational.h"

using std::cout;
using std::endl;

int main(void) {

    rational a(1, 2);
    rational b(2, 3);

    int i = 5;

    cout << a << " + " << b << " = " << a + b << endl;
    cout << a << " - " << b << " = " << a - b << endl;
    cout << a << " * " << b << " = " << a * b << endl;
    cout << a << " / " << b << " = " << a / b << endl;

    cout << a << " + " << i << " = " << a + i << endl;
    cout << a << " - " << i << " = " << a - i << endl;
    cout << a << " * " << i << " = " << a * i << endl;
    cout << a << " / " << i << " = " << a / i << endl;

    cout << i << " + " << a << " = " << i + a << endl;
    cout << i << " - " << a << " = " << i - a << endl;
    cout << i << " * " << a << " = " << i * a << endl;
    cout << i << " / " << a << " = " << i / a << endl;

    return 0;
}
  • 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-15T18:26:40+00:00Added an answer on June 15, 2026 at 6:26 pm

    The operator<< is ok (you can write it in one line though, but it doesn’t matter here):

    ostream &operator<<(ostream &out, const rational &o) {
        return out << '(' << o.num << " / " << o.den << ')';
    }
    

    However, you haven’t defined assignment operators yet!

    rational & rational::operator=(rational const &rhs) {
        den = rhs.den;
        num = rhs.num;
        return *this;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know is below code correct ? I have following code which
I am new to C++ and I want to know whether following code is
So what I want to know is whether the following is possible. I have
I want to pass many arguments to a shell script which I don't know
I have the following code which popualtes a ListView with photos from Flickr private
I have the following code to trigger an alarm, which when it fires is
I have a class which does operations on the database, but I want to
I have the following code which gives the warning in the title. I am
i want know how i can manage multiple twitter account on iOS in my
i want know if is possible, to get a specific element value of a

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.