Harry Potter Currency Conversion
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.
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?
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.
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.