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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:08:48+00:00 2026-05-27T01:08:48+00:00

This is probably a basic question for a regular C++ user. Functionally, I have

  • 0

This is probably a basic question for a regular C++ user. Functionally, I have an ECG monitor, and want to choose which output format to use at runtime. I have set up two classes that expose the same methods and members (ECGRecordingDefaultFormat and ECGRecordingEDFFormat) eg. ->InsertMeasure, ->setFrequency, ->setPatientName etc.

I know I could define one instance of each format class type, then put in:

if (ECGFormatToUse == ECGFormat.EDF) {
    ecgDefaultFormat.InsertMeasure(x);
}
if (ECGFormatToUse == ECGFormat.Default) {
    ecgEDFFormat.InsertMeasure(x);
}

all throughout the code, but I think I might not be using C++’s dynamic typing to its full extent.

The question is: can I define just one variable in main(), and after choosing the format I want at runtime, have the code use the right class with its exposed ‘InsertMeasure’ method, avoiding a whole lot of if/else’s throughout the code?

I’d be happy with just a reference to which aspect of inheritance/polymorphism (?) I should be using, and can google away the rest.

Thanks guys.

Pete

  • 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-27T01:08:49+00:00Added an answer on May 27, 2026 at 1:08 am

    You can combine the factory pattern with C++’s polymorphism.

    class Base
    {
       virtual void InsertMeasure() = 0; //virtual pure, make the base class abstract
    };
    
    class ECGRecordingDefaultFormat : public Base
    {
       virtual void InsertMeasure();
    };
    
    class ECGRecordingEDFFormat : public Base
    {
       virtual void InsertMeasure();
    };
    
    class Factory
    {
       static Base* create(ECGFormat format)
       {
          if ( format == ECGFormat.EDF )
             return new ECGRecordingEDFFormat;
          if ( format == ECGFormat.Default )
             return new ECGRecordingDefaultFormat;
          return NULL;
       }
    };
    
    int main()
    {
       ECGFormat format;
       //set the format
       Base* ECGRecordingInstance = Factory::create(format);
       ECGRecordingInstance->InsertMeasure();
       return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a basic html/css question... I have a simple one-button form that
This is probably a basic question. I have two projects: A web application project,
This is probably a basic LINQ question. I have need to select one object
This is probably a basic question, since I'm new to WPF.. I have a
this is probably a very basic question. I have installed rails 3 and generated
This is probably a basic question, but I have never used this syntax before.
I realize that this is probably a very basic question, but I have spent
This is probably a basic question, but I have been having a hard time
This is probably a really basic question - but I have a list of
This is probably a pretty basic question, but just something that I wanted to

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.