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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:36:24+00:00 2026-05-23T20:36:24+00:00

I am trying to practice OO design. I’ve made the following classes, which will

  • 0

I am trying to practice OO design. I’ve made the following classes, which will eventually let a user pick a temperature unit type (like fahrenheit) from a list, and convert celcius temperatures to their chosen temperature:

TemperatureUnits.h:

#include "fahrenheittemperatureunit.h"
#include "kelvintemperatureunit.h"
#include "temperatureunit.h"

#include <list>

class TemperatureUnits
{
public:
    static std::list<TemperatureUnit> All();
    const static FahrenheitTemperatureUnit kFahrenheit;
    const static KelvinTemperatureUnit kKelvin;
};

TemperatureUnits.cpp:

#include "temperatureunits.h"

std::list<TemperatureUnit> TemperatureUnits::All()
{
    std::list<TemperatureUnit> units;
    units.push_back(TemperatureUnits::kFahrenheit);
    units.push_back(TemperatureUnits::kKelvin);
    return units;
}

const KelvinTemperatureUnit TemperatureUnits::kKelvin;
const FahrenheitTemperatureUnit TemperatureUnits::kFahrenheit;

TemperatureUnit.h

class TemperatureUnit
{
public:
    explicit TemperatureUnit(const std::string &name);
    virtual ~TemperatureUnit() {}

    const std::string& Name() const;
    virtual float FromCelcius(float celcius) const {return 0;}

private:
    std::string name_;
};

TemperatureUnit.cpp:

#include "temperatureunit.h"

TemperatureUnit::TemperatureUnit(const std::string &name)
    :
    name_(name)
{

}

const std::string& TemperatureUnit::Name() const
{
    return name_;
}

Lastly, an example subclass:

KelvinTemperatureUnit.h:

#include "temperatureunit.h"

class KelvinTemperatureUnit : public TemperatureUnit
{
public:
    explicit KelvinTemperatureUnit();
    virtual float FromCelcius(float celcius) const;
};

KelvinTemperatureUnit.cpp:

#include "kelvintemperatureunit.h"

KelvinTemperatureUnit::KelvinTemperatureUnit()
    :
    TemperatureUnit(std::string("Kelvin"))

{

}

float KelvinTemperatureUnit::FromCelcius(float celcius) const
{
    return celcius - 273;
}

There are a number of flaws (that I know of) in the above:

  • I’m not sure the class TemperatureUnits{ ... static std::list<TemperatureUnit> All(); pattern is the correct one to use, but unsure what would actually be better.

  • By storing the types of TemperatureUnit in a std::list, they are treated as the base class TemperatureUnit, instead of their specialisations like KelvinTemperatureUnit. This means calling the FromCelcius method doesn’t work correctly. I am pretty sure I used to do this type of thing using Java.

  • I had a go, but it is probably pretty ugly and will offend someone 🙁

Does anyone have any ideas on the best way to go about this?

thanks!

  • 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-23T20:36:25+00:00Added an answer on May 23, 2026 at 8:36 pm
    I am pretty sure I used to do this type of thing using Java.
    

    Java objects are references. C++ objects are values. Simply convert to a reference, and then you will have more equivalent code. Oh, and a Java list is a C++ vector. A C++ list is a linked list.

    Quite simply, do not bother to carry over any Java knowledge to C++, they are extremely different languages, you should start with C++ from scratch. C++ worth writing looks extremely different to Java.

    int main() {
        std::vector<TemperatureUnit*> units;
        FahrenheitTemperatureUnit kFahrenheit;
        KelvinTemperatureUnit kKelvin;
        units.push_back(&kFahrenheit);
        units.push_back(&kKelvin);
    
        // Do something here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to find the best practice for generating and outputting html which
I'm trying to create my custom toolbox which imitates jQuery's design pattern. Basically, the
I'm trying to find a design pattern or a best practice, or some other
I'm trying to design application that will have UI with database in the backend.
I am puzzled by some issues when trying to design interacting classes. If a
Recently I was trying some practice programs in python and I came across this
I'm trying to practice writing better code, so I wanted to validate my input
I'm trying to practice my F# by writing small console scripts in F# in
I am fairly new to vim. I am trying to practice (been reading a
I'm using Eclipse with C++ plugins on my macbook, trying some practice projects 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.