I have to implement polymorphism in my project. I have a virtual class called "Account". Then there are 4 subclasses: USD, EUR, GBP, and CHF. I need to read the current balance from a text file like this:
USD 50
CHF 80
GBP 10
EUR 90
and make a subclass depending on the currency.
Every currency should have its own object. Later in the program, I will implement currency exchange, exchange rates will be read from the file. I don't have any idea of how to start with these classes. What should I learn?
My code so far:
class Account{
std::string currency;
public:
virtual void balance() = 0;
};
class currencyAcc: public Konto {
std::string currency;
float balance;
walutowe(std::string currency,float balance) {
this->currency= currency;
this->balance= balance;
}
void AccBallance() {
std::cout << balance<< std::endl;
}
};
What should I learn?
Well, if you have covered the basics, you sure need some practice and guidance!
You could have a global function that:
Account * parseOne(std::fstream& file); // Reference to opened file
Even if you just want the code, you will still have to go through an explanation. :)
Let us see it in a general sense.
Very simply:
std::getline(file, line);
it. You should also check if the read was successful.
You can do this as:
std::stringstream parse_me(line);
parse_me >> some_data_1;
parse_me >> some_data_2;
...
Here, you need to create it on the basis of currency_type
. Do:
if(currency_type == "GBP")
{
new_currency_object = new GBP(balance);
}
for each derived class.
Putting it together:
Account * parseOne(std::fstream& file) // Reference to opened file
{
// To read a line from the file
std::string line;
// If the read failed, return NULL
if(!std::getline(file, line))
{
return 0;
}
// Read success
// Using stringstream so that different types of data can be parsed
std::stringstream line_buffer(line);
// Declare variables to be parsed
std::string currency_type;
float balance;
// Now parse it (note the order!)
line_buffer >> currency_type >> balance;
// Create a pointer to base...
Account * new_currency_object;
// ...and create the object based on the currency_type
if(currency_type == "USD")
{
new_currency_object = new USD(balance);
}
... for each currency
// We are done, return the fruits of our labour
return new_currency_object;
}
(Note that I assume you have a USD(float balance)
constructor. If not, set balance
yourself)
to be used as:
// open the file
std::fstream currency_file("my_currencies.txt");
// Read a currency
Account * a_currency;
// When the read finishes, we get NULL
while(a_currency = parseOne(currency_file))
{
// do something with a_currency. Maybe:
// list_of_currencies.push_back(a_currency) it?
}
Edit: And be sure to deallocate the memory once done! In fact, use of new
and raw pointers are not encouraged anymore. Thanks to this comment for suggesting it.
For further reading, see How to implement the factory method pattern in C++ correctly
.
Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With