ONLY PART 1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! LA
ID: 3568512 • Letter: O
Question
ONLY PART 1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
LAB 7.8 Student-Generated Code Assignment (Homework)
PART 1: Create a SavingsAccount class and write a program that uses it, by
carrying out the following steps.
Create a C++ class declaration for the SavingsAccount 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 subtracted 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 or more, it
must increment dollars by the appropriate amount. Example: if cents is
234, then dollars must be increased by 2 and cents reduced to 34. Be sure
also that your program can correctly handle actions like withdrawing 10
dollars and 85 cents from a balance of 135 dollars and 12 cents.
Write the code for a main function that creates an object of your class
called acctl 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
only part 1 please
Explanation / Answer
#include <iostream>
using namespace std;
class SavingsAccount{private:
long dollars,cents;
public:
SavingsAccount()
{dollars=0;
cents=0;
}
SavingsAccount(int newDol, int newCent)
{dollars=newDol;
cents=newCent;
}
void deposit (int depDol,int depCent)
{dollars+=depDol;
cents+=depCent;
int extra=cents/100;
dollars+=extra;
cents=cents-extra*100;
}
void withdrawal(int wdDol, int wdCent)
{int extra;
if(wdCent>100)
{extra=wdCent%100;
wdDol+=extra;
wdCent-=(wdCent/100);
}
if(wdDol>dollars)
{cout<<"insufficient funds ";
return;
}
dollars-=wdDol;
cents-=wdCent;
if(cents<0)
{dollars--;
cents+=100;
}
}
double showBalance()
{double amt=dollars+cents/100.;
return amt;
}
};
int main()
{
char choice='y';
int d,c;
cout<<"Input the initial dollars: ";
cin>>d;
cout<<"Input the initial cents: ";
cin>>c;
SavingsAccount a(d,c);
cout<<"Would you like to make a deposit(Y or y for yes)?";
cin>>choice;
while(choice=='y'||choice=='Y')
{cout<<"Input the dollars to be deposited: ";
cin>>d;
cout<<"Input the cents to be deposited: ";
cin>>c;
a.deposit(d,c);
cout<<"Would you like to make another deposit(Y or y for yes)?";
cin>>choice;
}
cout<<"Would you like to make a withdrawal(Y or y for yes)?";
cin>>choice;
while(choice=='y'||choice=='Y')
{cout<<"Input the dollars to be withdrawn: ";
cin>>d;
cout<<"Input the cents to be withdrawn: ";
cin>>c;
a.withdrawal(d,c);
cout<<"Would you like to make another withdrawal(Y or y for yes)?";
cin>>choice;
}
cout<<"Balance: $"<<a.showBalance()<<endl;
system("pause");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.