Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Student Generated Code Assignments Exercise 1: Give a C++ class declaration call

ID: 3779827 • Letter: S

Question

Student Generated Code Assignments Exercise 1: Give a C++ class declaration called savin with the following information Operations (Member Functions) 1. open account (with an initial deposit This is called to put initial values in dollars and cents. 2. Make a deposit. A function that will add value to dollars and cents 3. Make a withdrawal. A function that will subtract values from dollars and cents 4. Show current balance. A function that will print dollars and cents. Data Member Data) 1. dollars 2. cents Give the implementation code for all the member functions. NOTE: You must 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 4, then dollars must be increased by 2 and cents reduced to 34. Write code that will create an object called bankl. The code will then initially place $200.50 in the account. The code will deposit $40.50 and then withdraw $100.98. It will print out the final value of dollars and cents The following output should be produced: Dollars 140 cents 2. Part 2: Change the program to allow the user to input the initial values, deposit and withdrawal.

Explanation / Answer

#include <iostream>
using namespace std;

class SavingsAccount{
public:
int dollars;
int cents;
//Constructor
SavingsAccount();
void openAccount(int,int);
void deposit(int,int);
void withdraw(int,int);
void showBal();
};
SavingsAccount::SavingsAccount(void){
dollars = 0;
cents = 0;
}
void SavingsAccount::openAccount(int d,int c){
dollars = d;
if(c>99){
dollars += c/100;
}
cents = c%100;
}
void SavingsAccount::showBal(){
cout << "Dollars = "<<dollars << "," << " cents = " << cents << endl;
}
void SavingsAccount::deposit(int d,int c){
dollars += d;
c += cents;
if(c>99){
dollars += c/100;
}
cents = c%100;
}
void SavingsAccount::withdraw(int d,int c){
dollars -= d;
if(c>99){
dollars -= c/100;
}
if(cents<c){
dollars -= 1;
cents = 100 - c%100;
}
else{
cents -= c%100;
}
}
int main() {
SavingsAccount ac;
ac.openAccount(200,50);
ac.deposit(40,50);
ac.withdraw(100,98);
ac.showBal();
int d,c;
SavingsAccount ac1;
cout << "1.openAccount 2.deposit 3.withdraw 4.showBal 5.exit"<<endl;
int choice;
cout << "Enter choice:";
cin>>choice;
while(choice!=5){
switch(choice){
case 1:
cout<<"Enter dollars:";
cin>>d;
cout << "Enter cents:";
cin>>c;
ac1.openAccount(d,c);
break;
case 2:
cout<<"Enter dollars:";
cin>>d;
cout << "Enter cents:";
cin>>c;
ac1.deposit(d,c);
break;
case 3:
cout<<"Enter dollars:";
cin>>d;
cout << "Enter cents:";
cin>>c;
ac1.withdraw(d,c);
break;
case 4:
ac1.showBal();
break;
case 5:
cout << "Good bye!" << endl;
break;
default:
cout << "Invalid Input" << endl;
}
cout << "Enter choice:";
cin>>choice;
}
}
/*
sample output
Dollars = 140, cents = 2
1.openAccount
2.deposit
3.withdraw
4.showBal
5.exit
Enter choice: 1
Enter dollars: 50
Enter cents: 50
Enter choice: 4
Dollars = 50, cents = 50
Enter choice: 2
Enter dollars: 100
Enter cents: 250
Enter choice: 4
Dollars = 153, cents = 0
Enter choice: 3
Enter dollars: 3
Enter cents: 0
Enter choice: 4
Dollars = 150, cents = 0
Enter choice: 5
*/