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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:13:03+00:00 2026-05-27T23:13:03+00:00

I have the following problem: let’s say class Item holds the serial number of

  • 0

I have the following problem: let’s say class Item holds the serial number of a product,
and class Book is an Item which inherits class Item‘s serial number. I have to create and use operator>> for every class. I thought about creating operator>> to Item, and then just call it in the implementation of the istream of the book, but I don’t know how.

The code goes like this:

class Item
{
protected:
    int _sn;
public:
    Item();
    ~Item();
    ...
    const istream& operator>>(const istream& in,const Item& x)
        {
        int temp;
        in>>temp;
        x._sn=temp;
        return in;
        }
};

class Book
{
private:
    char _book_name[20];
public:
    Book();
    ~Book();
    ...
    const istream& operator>>(const istream& in,const Book& x)
        {
        char temp[20];
        ////**here i want to use the operator>> of Item**////
        in>>temp;
        strcpy(x._book_name,temp);
        return in;
        }
};

int main()
{
Book book;
in>>book; //here i want to get both _sn and _book_name
}

Is this even possible?

  • 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-05-27T23:13:03+00:00Added an answer on May 27, 2026 at 11:13 pm

    First, operator>> has to be a free function or a friend, because the left-hand side is not of your class type. Second, the lhs must also be a non-const reference (because the stream changes state on extraction), the same goes for the second parameter (you change state, it needs to be non-const). With that in mind, here’s how the operators should look:

    class Item
    {
    protected:
        int _sn;
    public:
        Item();
        ~Item();
        // ...
        friend std::istream& operator>>(std::istream& in, Item& item){
          return in >> item._sn;
        }
    };
    
    class Book
      : public Item
    {
    private:
        std::string name;
    public:
        Book();
        ~Book();
        // ...
        friend std::istream& operator>>(std::istream& in, Book& book){
          Item& item = book;
          return in >> item >> book.name;
        }
    };
    

    Note that I changed your C-style string handling (char name[20] + strncpy) to a std::string, which is what you should be doing in C++.

    It can be done even easier if you just implement a from_stream method:

    class Item
    {
    protected:
        int _sn;
    public:
        Item();
        ~Item();
        // ...
        virtual void from_stream(std::istream& in){
          in >> _sn;
        }
    };
    
    std::istream& operator>>(std::istream& in, Item& item){
      item.from_stream(in);
      return in;
    }
    
    class Book
      : public Item
    {
    private:
        std::string name;
    public:
        Book();
        ~Book();
        // ...
        void from_stream(std::istream& in){
          Item::from_stream(in);
          in >> name;
        }
    };
    

    Thanks to from_stream being virtual, you never need to reimplement operator>>, it will automatically dispatch to the correct derived class depending with which it was called:

    int main(){
      Item item;
      Book book;
    
      std::cin >> item; // calls operator>>(std::cin, item), them item.from_stream(std::cin)
    
      std::cin >> book; // calls operator>>(std::cin, book), converting book to its baseclass Item
                        // then calls Book::from_stream(std::cin), which in turn calls Item::from_stream(std::cin)
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Problem. Let's Say I have a JTable with the folowing
I have the following problem: We have a large product which is in master
I'm trying to solve the following problem: Say I have a Python script (let's
I have the following problem in Struts 2. Let's assume i have a class
I am stuck with following monad problem: Let's say I have a standard monad
I would like to have some suggestions for the following problem: Let's say you
I have the following problem to solve: Let's say there is a table A
I have the following problem. I got a class PluginLoader which oversees loading of
I have to solve a following problem. there are many files let's say 3
if have the following problem: I have a List which i am going through

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.