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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:07:13+00:00 2026-06-05T22:07:13+00:00

I’m not good at OOP design, but I need to demonstrate my knowledge to

  • 0

I’m not good at OOP design, but I need to demonstrate my knowledge to potential employer.
The situation is following:

I’ve got a file with parameters of key-value type, called parameters.txt.
I’ve got a map <string, CommonParamValue> as a container. I need to fill it with elements, using the file with parameters. The key is always a std::string parameter, the CommonParamValue can be represented with int, double, string and a standard function call. To implement this, the CommonParamValue is a base class with virtual methods. It has childs – StringParamValue, DoubleParamValue, CurTimeParamValue. The base class and every child have a method virtual string GetValue() that returns a string representation of inner data; virtual void SetValue(string) to set the value.

The question is how to fill the container map <string, CommonParamValue> with appropriate data in runtime using polymorphism? Now I’ve got such situation:

parameters.txt

*user_name=Jane
*order_number=1325
current_date=

Routine for filling map

ifstream in(fileParamsPath);
    if (! in)
    {
        cout << "Cannot open file with parameters, program is terminating." << endl;
        cin.get();
        exit(-1);
    }
    string key = "", value = "";
    while(in)
    {
        getline(in, key, '=');
        getline(in, value, '\n');

        // put key and value into the container
        // right here we need to analyze the data type and choose appropriate container.
        // CommonParamValue *paramValue = new DoubleParamValue(); or
        // CommonParamValue *paramValue = new CurTimeParamValue(); or
        // CommonParamValue *paramValue = new StringParamValue();

        paramValue->SetValue(value);
        params.insert(make_pair(key, *paramValue)); // params is a map <string, CommonParamValue>
        delete paramValue;
    }
    in.close();

There’s an idea to keep the type of value-parameter in file parameters.txt and analyze it when filling map <string, CommonParamValue>

parameters.txt

*user_name=Jane
string

*order_number=1325
int

current_date=
function

And modify routine for filling map <string, CommonParamValue> this way:

string key = "", value = "", type = "";
    while(in)
    {
        getline(in, key, '=');
        getline(in, value, '\n');
        getline(in, type, '\n');
        in.get(); // got the dividing empty string
        // put key and value into the container
        // right here we need to analyze the data type and choose appropriate container.
        if(type == "int")
        {
            CommonParamValue *paramValue = new IntParamValue();
        }
        else if(type == "function")
        {
            CommonParamValue *paramValue = new CurTimeParamValue();
        }
        else if(type == "string")
        {
            CommonParamValue *paramValue = new StringParamValue();
        }
        else
        {
            // error
            exit(1);
        }

        paramValue->SetValue(value);
        params.insert(make_pair(key, *paramValue)); // params is a map <string, CommonParamValue>
        delete paramValue;
    }

Is that a good decision or bad? Maybe my potential employer wanted me to implement it in other way, but I have only this decision. Is there any better one for a junior C++ programmer?

  • 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-05T22:07:15+00:00Added an answer on June 5, 2026 at 10:07 pm

    The most classic design applicable is that of a factory method design.

    CommonParamValue* createParamValue( const std::string &value );
    

    The createParamValue method takes the responsibility of creating the appropriate derived ParamValue and return it as a CommonParamValue.

    You can avoid pumping additional strings for “type” into parameters.txt by inferring the type, inside createParamValue, using the value string. A regular expression sounds like the most elegant for int, double and CurTime. Boost regex is an example library lets you do this in c++. But of course if you’re looking for a quick solution, then writing your own 30-line decision tree should not be a problem for these simple grammars.

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

Sidebar

Related Questions

In my XML file chapters tag has more chapter tag.i need to display chapters
I need to clean up various Word 'smart' characters in user input, including but
I need a function that will clean a strings' special characters. I do NOT
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported

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.