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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:18:29+00:00 2026-06-11T07:18:29+00:00

Below is the code The Code: #include <iostream> using namespace std; class Rational {

  • 0

Below is the code

The Code:

#include <iostream>
using namespace std;

class Rational {
  int num;  // numerator
  int den;  // denominator
  friend istream& operator>> (istream & , Rational&);
  friend ostream& operator<< (ostream &  , const Rational&);
 public:
  Rational (int num = 0, int den = 1)
    :     num(num), den(den) {}
  void getUserInput() {
    cout << "num = ";
    cin >> num;
    cout << "den = ";
    cin >> den;
  }
  Rational operator+(const Rational &);
};

Rational Rational::operator+ (const Rational& r) { //HERE 
  int n = num * r.den + den * r.num;
  int d = den * r.den;
  return Rational (n, d);
}

istream& operator>> (istream & is , Rational& r)
{
    is >> r.num >> r.den;
}

ostream& operator<< (ostream & os , const Rational& r)
{
    os << r.num << " / " <<  r.den << endl;;
}
int main() {
  Rational r1, r2, r3;
  cout << "Input r1:\n";
  cin >> r1;
  cout << "Input r2:\n";
  cin >> r2;
  r3 = r1 + r2;
  cout << "r1 = " << r1;
  cout << "r2 = " << r2;
  cout << "r1 + r2 = " << r3;
  return 0;
}

The Question

The above code has a operator+ overloading , in the operator+ definition we can see the parameter r accessing the private data (r.num and r.den) . Why C++ allow the parameter to access private data outside of the class ? Is it some kind of a special case?

Thank you.

  • 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-11T07:18:31+00:00Added an answer on June 11, 2026 at 7:18 am

    Access specifiers apply at the level of classes, not instances, so the Rational class can see private data members of any other Rational instance. Since your Rational operator+ is a member function, it has access to private data of it’s Rational argument.

    Note: the canonical approach is to define a member operator +=, and then use that to implement a non-member operator+

    struct Foo
    {
      int i;
    
      Foo& operator+=(const Foo& rhs) 
      { 
        i += rhs.i;
        return *this;
      }
    
    };
    
    Foo operator+(Foo lhs, const Foo& rhs)
    {
      return lhs += rhs;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please see the code snippet below : #include <iostream> using namespace std; int main()
Consider the following sample code below: #include <iostream> using namespace std; class base {
Conside the following sample code below: #include <iostream> using namespace std; class base {
Consider the sample code below: #include <iostream> using namespace std; class A { private:
Consider the sample code below: #include <iostream> using namespace std; class core { public:
Please see the code below: #include <iostream> #include <stdlib.h> #include <time.h> using namespace std;
Consider the example below: #include <iostream> using namespace std; class base { public: virtual
I have the code below: // IBase.h #include <iostream> class IBase{ public: virtual string
I have some doubt on below code #include<stdio.h> int i=6; int main() { int
my test code is below: main1.c: #include <stdio.h> extern struct tt ; int main()

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.