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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:04:59+00:00 2026-06-11T15:04:59+00:00

So I have class A and class B, where class B extends class A.

  • 0

So I have class A and class B, where class B extends class A. I must overload the << and >> in both classes. I was hoping that in the function definition of the operators for class B, I could call the overloaded operators from class A, but I’m having trouble doing so.

#include <iostream>
#include <string>
using namespace std;

class A {
friend ostream& operator<<(ostream& out, A a);
protected:
    int i;
    string st;
public:
    A(){
        i=50;
        st = "boop1";
    }
};

ostream& operator<<(ostream &out, A a) {
out << a.i << a.st;
return out;
}

class B : public A {
friend ostream& operator<<(ostream& out, B b);
private:
    int r;
public:
    B() : A() {
        r=12;
    }
};

ostream& operator<<(ostream &out, B b) {
out = A::operator<<(out, b);    //operator<< is not a member of A
out << "boop2" << b.r;
return out;
}

int main () {
B b;
cout << b;
}

I attempt to call A’s version of operator<< in B’s version of operator<<, but of course it doesn’t actually belong to A, so it cannot compile. How should I be achieving this?

also, note that in reality A and B have their own header and body files.

  • 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-11T15:05:00+00:00Added an answer on June 11, 2026 at 3:05 pm

    You can make your B object look like an A object:

    std::ostream& operator<< (std::ostream& out, B const& b) {
        out << static_cast<A const&>(b);
        out << "boop2" << b.r;
        return out;
    }
    

    Note that you almost certainly don’t want to pass the object to be printed by value. I have changed the signature to use a const& instead: This indicates that the object won’t get changed and it won’t get copied.

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

Sidebar

Related Questions

This is what I have: class Calendar extends CI_Controller { public $extension; function __construct()
I have a class that extends AsyncTask , which fetches the gps cordinates from
I have a class that extends Application. The class is fairly extensive. When the
I have parent class c1 with function hi(). class c2 extends c1 and contains
I have a class PanelFormes that extends JPanel, which I use as a container.
I have a nested inner class that extends AsyncTask to run a db query
I have a public class FriendMaps extends MapActivity that gets called from a menu
I have an Android application that uses a class that extends the Activity class,
For example, I have a class that has two strings, one of which must
i have class Plugin_Magazine extends Zend_Controller_Action in some views i need to call static

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.