Part1:Create a SavingsAccount class and write a program that uses it, by carring
ID: 3568635 • Letter: P
Question
Part1:Create a SavingsAccount class and write a program that uses it, by carring out the following steps. Create a C++ class declaration for the SavingAccount class which has the following private member data: long dollars, cents; and which has the following public member functions: 1. A default constructor to create an account with an initial balance of 100 dollars and 0 cents. 2. a constructor to create an account with the initial dollars and cents passed to it. 3. A function that makes a deposit. This adds the dollars and cents passed to it onto the balance. 4. A function that makes a withdrawal. Providing the balance is greater than or equal to the amount to be withdrawn, the dollars and cents passed to it are substracted from the balance; otherwise an insufficient funds message is printed. 5. A function to return the balance converted to dollars. That is, if dollars is 95 and cents is 2, it would return 95.02 Write the implementation code for all the member functions. Be sure to perform normalization on cents. This means that if cents is 100 more, it must increment dollars by the appropriate amount. Example if cents is 234, then dollars must be increased by 2 and reduced to 34.Be sure also your program can correctly handle actions like withdrawing 10 dollars and 85 cents from balance of 135 dollars and 12 cents. Write the code for a main function that creates an object of your class called acct1 with an initial balance of $200.50. Deposit $40.50.Withdraw $100.98. Get and print the balance. These amounts may be hard coded into the program. The output should display with 2 decimal points and look like the following. balance: $140.02 additional information: The class looks llike the following: Class SavingsAccount { public: SavingsAccount(); SavingsAccount(int newDol, int newCent); void deposit (int depDol,int depCent); void withdrawal(int wdDol, int wdCent); double showBalance(); private: long dollars; long cents; };
Explanation / Answer
Savings Account class? Program :
#include<iostream>
using namespace std;
class SavingsAccount
{
private:
int dollars;
int cents;
public:
void setOpen(int, int);
void setDeposit(int, int);
void setWithdrawl(int, int);
void showBalance();
};
void SavingsAccount::setOpen(int d, int c)
{
dollars = d;
cents = c;
cout << "Input Dollars for Opening Balance:"<<endl;
cin >> dollars;
cout << "Input Cents for Opening Balance:"<<endl;
cin >> cents;
while(cents >=100)
{
cents-=100;
dollars++;
}
}
void SavingsAccount::setDeposit(int d, int c)
{
dollars = d;
cents = c;
cout << "Input Dollars to Deposit:"<<endl;
cin >> dollars;
cout << "Input Cents to Deposit:"<<endl;
cin >> cents;
dollars += d;
cents += c;
while(cents >= 100)
{
cents -= 100;
dollars++;
}
}
void SavingsAccount::setWithdrawl(int d, int c)
{
dollars = d;
cents = c;
cout << "Input Dollars to Withdrawl:"<<endl;
cin >> dollars;
cout << "Input Cents to Withdrawl:"<<endl;
cin >> cents;
while(c >=100)
{
c -= 100;
d++;
};
if(c > cents)
{
dollars--;
cents += 100;
};
dollars -= d;
cents -= c;
}
void SavingsAccount::showBalance()
{
cout << "Your current balance is: $" << dollars << "." << cents << endl;
};
#include<iostream>
#include "account.h"
using namespace std;
int main()
{
char answer; // To hold Y or N input.
SavingsAccount money;
int moneydollars, moneycents;
cout << "This program will help you open and manage a new Savings Account!"<<endl;
money.setOpen(moneydollars, moneycents);
cout << "Do you wish to make a Deposit?" <<endl;
cout << "Press 'Y' for Yes, and 'N' for No." <<endl;
cin >> answer;
do
{
money.setDeposit(moneydollars, moneycents);
} while ((answer = 'Y') ||( answer = 'y'));
cout << "Do you wish to make a Withdrawl?"<<endl;
cout << "Press 'Y' for Yes, and 'N' for No."<<endl;
cin >> answer;
do
{
money.setWithdrawl(moneydollars, moneycents);
} while( (answer = 'Y') ||( answer = 'y'));
money.showBalance();
system("pause");
return 0;
};