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

  • Home
  • SEARCH
  • 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 838743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:21:17+00:00 2026-05-15T05:21:17+00:00

I have a few classes ( heat , gas , contact , pressure )

  • 0

I have a few classes (heat, gas, contact, pressure) inheriting from a main one (sensor).

I have a need to store them in a vector<Sensor *> (part of the specification).

At some point in time, I need to call a function that indiscriminately stores those Sensor *. (also part of the specification, not open for discussion)

Something like this:

for(size_t i = 0; i < Sensors.size(); ++i)
    Sensors[i]->storeSensor(os) //os is an ofstream kind of object, passed onwards by reference

Where and how shall storeSensor be defined?

Is there any simple way to do this or will I need to disregard the specification? Mind you, I’m a beginner!

Thanks for your time!

  • 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-15T05:21:18+00:00Added an answer on May 15, 2026 at 5:21 am

    You need to make it a pure virtual function, which you then implement in each of the derived classes. The implementation can be pretty simple:

    class sensor {
       ....
       virtual void storeSensor( ostream & os ) = 0;
    };
    
    class heat : public sensor {
       ....
      void storeSensor( ostream & os ) {
         os << * this;
      }
    
    };
    
    class light : public sensor {
       ....
      void storeSensor( ostream & os ) {
         os << * this;
      }
    
    };
    

    This assumes you have defined a suitable operator<< for each of your classes. If not, you need to write the specific sensors member variables explicitly. And that is the easy bit – the difficulty starts when you want to read the sensors back in again 🙂

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

Sidebar

Related Questions

I have a few model classes with basic one-to-many relationships. For example, a book
I need to convert several Java classes to C#, but I have faced few
i have a few classes that i am trying to move to using generics
I have a few classes which do nothing except in their constructors/destructors. Here's an
I have a few classes, such as those that outline database table structure or
I have a few classes such that: public class XMLStatusMessage extends XMLMessage {} public
I have few asynchronous tasks running and I need to wait until at least
I have a few classes which have a property which is unique to that
I have a few classes (about 15 or so) in VB.net (2005) that I
I have a few classes: class Vehicle { } class Car : Vehicle {

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.