![]() ![]() Best practice is to make those magic numbers defined constants of some sort. Your functions have a lot of magic numbers in them.But with no data, why use a class? A namespace works just as well. With no data members, those conversion functions could be static functions, as opposed to instance functions. The code would be shorter if you eliminated those data members. The converters set but never access the data members in the class. The posted implementation suggests a very different kind of implementation. If you want to have this conversion as a class, I'd suggest following Morwenn's advice. Your compiler should have warned you about it if you compile with a sufficient level of warnings. Your main function contains an unused count variable that you could delete. Since you have in-class initializers, the explicitly defaulted default constructor will use them to initialize the class members. Actually, use = default to explicitly the compiler to generate it for you sinnce it you don't explicitly ask for it, it will be implicitly deleted because of the presence of other user-defined constructors: Temperature() = default That said, you could simply drop the default constructor. What you should have done is use 0.0 directly, which is the correct value of the correct type. So what is happening is that NULL is interpreted as 0 then converted to the double value 0.0. However, in your case, the use of NULL/ nullptr is wrong: you're not initializing pointers anywhere, only double values. Therefore, you should use nullptr instead of NULL when you can. Since you use in-class initializers, I guess that you at least use C 11. ![]() The user does not have to know how it is stored and what they have stored, only how to set and get the temperature the way they want. the user does not have to know about itįor example, you could always store Kelvin degrees and let the functions do the conversions ( set_kelvin and as_kelvin wouldn't do more than assigning and returning the value). Here, *you* choose how you store the temperature, Basically, it would be something like that: class Temperature You should be able to tell to the class which degrees you are storing and which you want to retrieve. Ideally, you should have had a class Temperature which does not expose its representation. That's some strong cognitive burden for users. I have the feeling that your class will be hard to use and maintain in a project: you are storing three different representations for a temperature and the user has to know which was used before using any the methods. Most of the classes we write don't manage resources and in this case, it's better to let the compiler generate the special functions. Basically, there are two kinds of classes: the ones that manage resources and only do that (a class shouldn't try to do too many things, it should focus on doing one thing, that's the single responsibility principle) and the ones that don't. That's called the Rule of Zero and it helps to easily write classes. Std::cout << temp.celToKel(degrees) << " Kelvin\n" Īddressing your first question about destructors: the general rule to follow is that you hardly ever need to hand-code copy/move constructors, copy/move assignment operators and destructors, unless your class manages resources. Return celsius = (5.0 / 9) * (fahr - 32) ĭouble Temperature::fahrToKel(double fahr) Return fahrenheit = cel * (9.0 / 5) 32 ĭouble Temperature::fahrToCel(double fahr) Return fahrenheit = (9.0 / 5) * (kel - 273.15) 32 ĭouble Temperature::celToFahr(double cel) : kelvin(kel), fahrenheit(fahr), celsius(cel)ĭouble Temperature::kelToFahr(double kel) Temperature(double kel, double fahr, double cel) constructor: Kelvin, Fahrenheit, Celsius Temperature() : kelvin(NULL), fahrenheit(NULL), celsius(NULL) *Ĭreate a Temperature class that returns a conversion to a user Also, any other feedback is welcomed too. ![]() This leaves me to think it's best to delete the constructors I defined. ![]() All of the functions take a single argument of type double. Since there are three scale measurements, kelvin, Fahrenheit, and Celsius, I decided it was best to define specific functions for the desired conversion. Here, the intention of the Temperature class is to simply convert between one temperature scale to another. I am curious if it would ever be in good practice to omit constructor definitions from a class. ![]()
0 Comments
Leave a Reply. |