Question: A. Create a currency class with four attributes - currency name, whole parts and fractional parts and fractional's name such that 100 fractional parts equals
A. Create a currency class with four attributes - currency name, whole parts and fractional parts and fractional's name such that 100 fractional parts equals 1 whole part.
B. Create 5 derived classes for the following currencies - Dollar, Euro, Yen, Rupee and Yuan. I know this program can be done without inheritance but it is a requirement for this lab. You will be adding and subtracting currency values using the form:
Dollar, 1, 25, cent
Euro, 3, 33, cent
Yen, 100, 54, sen
Rupee, 7, 11, paise
Yuan, 50, 86, fen
C. Define overloaded operators to add or subtract different currency objects - care should be taken that you can only perform these operation on objects of the same currency. Also, take care that fractional parts roll into whole parts.
D. Define an overloaded input stream operator to accept values of any currency as well as an overloaded output stream operator to write out the value of the currency..
E. When initializing currency objects, your code should demonstrate polymorphic construction. Your constructors should also use initialization lists.
F. Now, define a Wallet class that will contain the 5 individual currency types above and will implement the following - number of individual currency types, check if a currency type exists in the wallet, add money by currency, remove money by currency, remove all currencies, check if wallet is empty. What this means is that your wallet can only contain one object of each currency type. To add or remove money into/from wallet, check if currency exists in the wallet. If so, add or remove the amount specified.
G. Your main should allow the user to add a maximum of 5 different currency types to the wallet in the form of the base currency array - this will help you demonstrate the polymorphic construction. Demonstrate the overloaded subscript operator for the Wallet class. User should be able to add or remove as many currency values for any of the five currency types. The user should also be able to output the total value by name of the currencies in the wallet. Finally they should be able to empty the wallet and print relevant information about the operation. User Interactivity is extremely important - give due consideration to it.
This is what i have:
#include
#include
using namespace std;
const int SIZE = 5;
class Currency{
protected:
string currency_name, fractional_name;
int wholeNum, fractNum;
int arr[SIZE];
public:
string getCurrencyName();
string getFractionalName();
int getWholeNum();
int getFractNum();
Currency() {
for(int i = 0; i <= SIZE; i++) {
arr[i] = i;
}
}
int &operator[](int i) {
if( i > SIZE ) {
cout << "Index out of bounds" < // return first element. return arr[0]; } return arr[i]; } friend ostream &operator<<( ostream &output, const Currency &C ); friend istream &operator>>( istream &input, Currency &C ); }; string Currency::getCurrencyName() { return currency_name; } string Currency::getFractionalName() { return fractional_name; } int Currency::getWholeNum() { return wholeNum; } int Currency::getFractNum() { return fractNum; } class Dollar : public Currency{ public: Dollar(){}; Dollar(int whole, int fract) { currency_name = "Dollar"; fractional_name = "Cent"; wholeNum = whole; fractNum = fract; } ~Dollar(){}; // destructor to empty dollars in wallet Dollar operator+(const Dollar &dollar) { Dollar tempDol; tempDol.wholeNum = wholeNum + dollar.wholeNum; wholeNum++; tempDol.fractNum = fractNum + dollar.fractNum; fractNum++; return tempDol; } Dollar operator-(const Dollar &dollar1) { Dollar tempDol1; tempDol1.wholeNum = wholeNum = dollar1.wholeNum; tempDol1.fractNum = fractNum = dollar1.fractNum; return tempDol1; } friend istream &operator>>( istream &input, Dollar &D ) { input >> D.wholeNum >> D.fractNum; return input; } friend ostream &operator<<( ostream &output, const Dollar &D ) { output<<"You now have " << D.wholeNum <<" Dollars and " << D.fractNum <<" Cents in your wallet" < return output; } }; class Euro : public Currency{ public: Euro(){}; Euro(int whole, int fract) { currency_name = "Euro"; fractional_name = "Cent"; wholeNum = whole; fractNum = fract; } ~Euro(){}; // destructor to empty dollars in wallet Euro operator+(const Euro &euro) { Euro tempEur; tempEur.wholeNum = wholeNum + euro.wholeNum; tempEur.fractNum = fractNum + euro.fractNum; return tempEur; } Euro operator-(const Euro &euro1) { Euro tempEur1; tempEur1.wholeNum = wholeNum - euro1.wholeNum; tempEur1.fractNum = fractNum - euro1.fractNum; return tempEur1; } friend istream &operator>>( istream &input, Euro &E ) { input >> E.wholeNum >> E.fractNum; return input; } friend ostream &operator<<( ostream &output, const Euro &E ) { output<<"You now have " << E.wholeNum <<" Euros and " << E.fractNum <<" Cents in your wallet" < return output; } }; class Yen : public Currency{ public: Yen(){}; Yen(int whole, int fract) { currency_name = "Yen"; fractional_name = "Sen"; wholeNum = whole; fractNum = fract; } ~Yen(){}; // destructor to empty dollars in wallet Yen operator+(const Yen ¥) { Yen tempYen; tempYen.wholeNum = wholeNum + yen.wholeNum; tempYen.fractNum = fractNum + yen.fractNum; return tempYen; } Yen operator-(const Yen ¥1) { Yen tempYen1; tempYen1.wholeNum = wholeNum - yen1.wholeNum; tempYen1.fractNum = fractNum - yen1.fractNum; return tempYen1; } friend istream &operator>>( istream &input, Yen &Y) { input >> Y.wholeNum >> Y.fractNum; return input; } friend ostream &operator<<( ostream &output, const Yen &Y ) { output<<"You now have " << Y.wholeNum <<" Yens and " << Y.fractNum <<" Sens in your wallet" < return output; } }; class Rupee : public Currency{ public: Rupee(){}; Rupee(int whole, int fract) { currency_name = "Rupee"; fractional_name = "Paise"; wholeNum = whole; fractNum = fract; } ~Rupee(){}; // destructor to empty dollars in wallet Rupee operator+(const Rupee &rupee) { Rupee tempRupee; tempRupee.wholeNum = wholeNum + rupee.wholeNum; tempRupee.fractNum = fractNum + rupee.fractNum; return tempRupee; } Rupee operator-(const Rupee &rupee1) { Rupee tempRupee1; tempRupee1.wholeNum = wholeNum - rupee1.wholeNum; tempRupee1.fractNum = fractNum - rupee1.fractNum; return tempRupee1; } friend istream &operator>>( istream &input, Rupee &R) { input >> R.wholeNum >> R.fractNum; return input; } friend ostream &operator<<( ostream &output, const Rupee &R ) { output<<"You now have " << R.wholeNum <<" Rupees and " << R.fractNum <<" Paises in your wallet" < return output; } }; class Yuan : public Currency{ public: Yuan(){}; Yuan(int whole, int fract) { currency_name = "Yuan"; fractional_name = "Fen"; wholeNum = whole; fractNum = fract; } ~Yuan(){}; // destructor to empty dollars in wallet Yuan operator+(const Yuan &yuan) { Yuan tempYuan; tempYuan.wholeNum = wholeNum + yuan.wholeNum; tempYuan.fractNum = fractNum + yuan.fractNum; return tempYuan; } Yuan operator-(const Yuan &yuan1) { Yuan tempYuan1; tempYuan1.wholeNum = wholeNum - yuan1.wholeNum; tempYuan1.fractNum = fractNum - yuan1.fractNum; return tempYuan1; } friend istream &operator>>( istream &input, Yuan &Y) { input >> Y.wholeNum >> Y.fractNum; return input; } friend ostream &operator<<( ostream &output, const Yuan &Y ) { output<<"You now have " << Y.wholeNum <<" Yuans and " << Y.fractNum <<" Fens in your wallet" < return output; } }; class Wallet { private: int numOfCurTypes; int inputWholeNum; int inputFractNum; bool isCurExists; bool isWalletEmpty; public: int getNumOfCurTypes() { return numOfCurTypes; } bool checkCurExists() { return isCurExists; } bool checkWallet() { return isWalletEmpty; } void addMoney() { } void subtractMoney() { } void removeAllCur() { } void addCurType() { } void removeCurType() { } }; int main() { int answer1 = 0, answer2 = 0; Dollar dollar; Euro euro; Yen yen; Rupee rupee; Yuan yuan; Currency *curr1 = $ Currency *curr2 = € Currency *curr3 = ¥ Currency *curr4 = &rupee; Currency *curr5 = &yuan; do{ cout<<"Select an option from the menu:" < cout<<" 1. Add money"< cout<<" 2. Subtract money" < cout<<" 3. Fully remove one type of money" < cout<<" 4. Display contents of the wallet" < cout<<" 5. Empty wallet" < cout<<" 6. Quit" < cout<<"Your input: "; cin>>answer1; if(answer1 >= 1 && answer1 <= 3) { cout<<"Select the type of currency: " < cout<<" 1. Dollar" < cout<<" 2. Euro" < cout<<" 3. Yen" < cout<<" 4. Rupee" < cout<<" 5. Yuan" < cout<<"Your input: "; cin>>answer2; } if(answer1 == 1 && answer2 >= 1 && answer2 <= 5) { if(answer2 == 1) { cout<<"Please enter amount of Dollars & Cents: " < cin>>dollar; cout< } else if(answer2 == 2) { cout<<"Please enter amount of Euros & Cents: " < cin>>euro; cout< } else if(answer2 == 3) { cout<<"Please enter amount of Yens & Sens: " < cin>>yen; cout< } else if(answer2 == 4) { cout<<"Please enter amount of Rupees & Paises: " < cin>>rupee; cout< } else if(answer2 == 5) { cout<<"Please enter amount of Yuans & Fens: " < cin>>yuan; cout< } } if(answer1 == 2 && answer2 >= 1 && answer2 <= 5) { if(answer2 == 1) { cout<<"Please enter amount of Dollars & Cents: " < cin>>dollar; cout< } else if(answer2 == 2) { cout<<"Please enter amount of Euros & Cents: " < cin>>euro; cout< } else if(answer2 == 3) { cout<<"Please enter amount of Yens & Sens: " < cin>>yen; cout< } else if(answer2 == 4) { cout<<"Please enter amount of Rupees & Paises: " < cin>>rupee; cout< } else if(answer2 == 5) { cout<<"Please enter amount of Yuans & Fens: " < cin>>yuan; cout< } } else cout<<"Please enter a valid input" < }while(answer1 != 6); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
