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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:56:39+00:00 2026-05-29T14:56:39+00:00

I have a number of classes that represent various computer components, each of which

  • 0

I have a number of classes that represent various computer components, each of which have an overloaded << operator declared as follows:

friend ostream& operator << (ostream& os, const MotherBoard& mb);

Each returns an ostream object with a unique stream describing that component, some of which are composed of other components. I decided to create a base class called Component in order to generate a unique id as well as some other functions that all the components will publicly derive. Of course, the overloaded << operator doesn’t work with pointers to Component objects.

I was wondering how I would effect something like a pure virtual function that will be overwritten by each derived class’s << operator so I could do something like:

Component* mobo = new MotherBoard();

cout << *mobo << endl;

delete mobo;

Also related to: overloading << operators and inherited classes

  • 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-29T14:56:42+00:00Added an answer on May 29, 2026 at 2:56 pm

    Maybe something like this:

    #include <iostream>
    
    class Component 
    {
    public:
        // Constructor, destructor and other stuff
    
        virtual std::ostream &output(std::ostream &os) const
            { os << "Generic component\n"; return os; }
    };
    
    class MotherBoard : public Component
    {
    public:
        // Constructor, destructor and other stuff
    
        virtual std::ostream &output(std::ostream &os) const
            { os << "Motherboard\n"; return os; }
    };
    
    std::ostream &operator<<(std::ostream &os, const Component &component)
    {
        return component.output(os);
    }
    
    int main()
    {
        MotherBoard mb; 
        Component &component = mb;
    
        std::cout << component;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several obj-c classes, each of which require a number of constants that
I have a number of data classes representing various entities. Which is better: writing
I have a number of classes that call say Trace.WriteLine(LogSomethingClassSpecific), in various methods all
I have a number of classes that are all related conceptually, but some more-so
I have a number of classes that inherit from one superclass. The superclass as
I have a number of DAO classes that extend SqlMapClientDaoSupport, and call getSqlMapClientTemplate() to
I have a number of static classes that contain tables like this: using System;
I have a number of simple controller classes that use Doctrine's entity manager to
I am using spring+struts2 and I have a large number of action classes that
I have a python module that defines a number of classes: class A(object): def

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.