Harry Potter Currency Conversion

A while back I tried to help someone with their Javascript Harry Potter currency conversion, but the Javascript was horribly obfuscated. I ended up writing the whole thing sloppily in C++ in about half an hour. I want to make something very clear, you should not write sloppy C or C++ code on purpose. Memory is not always allocated or especially deallocated the same way you would expect with Java or Python. Not using dealloc in C or if you're working with classes C++ a destructor can cause memory leaks. Ignoring class destructors for my code worked, but it's not best practice for programming in C++. Now, moving slowly because this code is pretty complicated I would love to walk through it with you.

The first thing I do is note that while there are two conversion rates for the currency, there is only one currency. A really neat feature in Object Orientated Programming is that you can create a parent class with basic parameters, and then let child classes inherit from that parent class. In this case I used that structure to save time and keep my code readable. From there, I created the parent class with methods that can get or set the main variables. Since the main variables can be modified without being directly accessed I limited the scope of their availability by making them "protected." In effect, I am forcing programmers to use the methods I set for accessing those variables instead of letting them access the variables themselves. This lets me communicate how those variables are intended to be used. I also included a "convert" method in the parent class so it can be overridden by the child classes with more specific information. Final note, the "include" and "using" statements are just standard C++ statements for the compiler.

HP Currency class with all the getter, setter, and modifcation methods we'll need later on

The next two classes will throw you off because they will look significantly different. Basically, when you tell a compiler you want children classes to inherit methods from a parent class you specify the parent class, and then other side of the colon specify the type of access the child class will have (public, protected, or private) and the name of that child class. The primary purpose of inheritance is to help reduce redundancy, and in this case the reduction was pretty significant. The classes for each individual currency just need a conversion method that returns values specific to that conversion method. Can you imagine how much larger the code below would have been if I had to write methods to alter the currency values individually?

classes for the conversion methods with an overridden conversion function to account for different rates between methods and keep the other methods as they are

The final step is to collect information from the user, and I do that in the main function. In hindsight, I really wish I had written a "conversion" function, and kept the code within main as small as possible. Either way, C++ uses input and output streams to collect information from the user and output information to the screen. Those are represented by arrows, "cin", and "cout." After that point I define my class objects, and use their methods to change the values being converted based on user input. Then I use the class methods to tell the user the currency conversion from HP to USD.

collect information about how much money the user wants to convert and perform the conversion

All-in-all, it was a nice exercise in decent Object Oriented Programming. There are still a few things I could do differently, but I did not put a whole lot of time into this project. The video below demonstrates it in action, and you will notice a slight modification I made. Sometimes programs exit as soon as the final instruction is completed, but that's not always the case. In this case my program needed a few extra lines to end execution only after you hit a key and pressed enter. You can check it out in the video below.