In: Computer Science
Give a C++ class declaration called SavingsAccount 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
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 234, then dollars must be increased by 2 and cents reduced to 34.
Write code that will create an object called bank1. 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.
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 234, then dollars must be increased by 2 and cents reduced to 34.
Write code that will create an object called bank1. 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.
GIVEN DATA:
Savings Account:
#include <iostream>
using namespace std;
class SavingsAccount {
public:
double dollars;
double cents;
void openAccount(int, int);
void makeDeposit(int, int);
void withdrawal(int, int);
void displayBalance();
};
void SavingsAccount::openAccount(int d, int c)
{
dollars = d;
cents = c;
while(cents >=100)
{
cents-=100;
dollars++;
}
}
void SavingsAccount::makeDeposit(int d, int c)
{
dollars += d;
cents += c;
while(cents >= 100)
{
cents -= 100;
dollars++;
}
}
void SavingsAccount::withdrawal(int d, int c)
{
while(c >=100)
{
c -= 100;
d++;
};
if(c > cents)
{
dollars--;
cents += 100;
};
dollars -= d;
cents -= c;
}
void SavingsAccount::displayBalance()
{
cout << "Dollars = " << dollars << " cents = "
<< cents << endl;
};
int main()
{
SavingsAccount bank1;
bank1.openAccount(200,50);
bank1.makeDeposit(40,50);
bank1.withdrawal(100,98);
bank1.displayBalance();
return 0;
}
Out Put:
Dollars = 140 cents = 2
//program to take input from the user:
#include <iostream>
using namespace std;
class SavingsAccount {
public:
double dollars;
double cents;
void openAccount(int, int);
void makeDeposit(int, int);
void withdrawal(int, int);
void displayBalance();
};
void SavingsAccount::openAccount(int d, int c) {
dollars = d;
cents = c;
while (cents >= 100) {
cents -= 100;
dollars++;
}
}
void SavingsAccount::makeDeposit(int d, int c) {
dollars += d;
cents += c;
while (cents >= 100) {
cents -= 100;
dollars++;
}
}
void SavingsAccount::withdrawal(int d, int c) {
while (c >= 100) {
c -= 100;
d++;
};
if (c > cents) {
dollars--;
cents += 100;
};
dollars -= d;
cents -= c;
}
void SavingsAccount::displayBalance() {
cout << "Dollars = " << dollars << " cents = "
<< cents << endl;
};
int main() {
char answer; // To hold Y or N input.
int dollars = 0, cents = 0;
SavingsAccount bank1;
cout << "This program will help create a new Savings
Account!" << endl;
cout << "Enter the opening Dollars: ";
cin >> dollars;
cout << "Enter the opening cents: ";
cin >> cents;
bank1.openAccount(dollars, cents);
cout << "Input Dollars to Deposit:" << endl;
cin >> dollars;
cout << "Input Cents to Deposit:" << endl;
cin >> cents;
bank1.makeDeposit(dollars, cents);
cout << "Input Dollars to Withdrawl:" << endl;
cin >> dollars;
cout << "Input Cents to Withdrawl:" << endl;
cin >> cents;
bank1.withdrawal(dollars, cents);
bank1.displayBalance();
return 0;
}
Out Put:
This program will help create a new Savings Account!
Enter the opening Dollars: 200
Enter the opening cents: 50
Input Dollars to Deposit:
40
Input Cents to Deposit:
50
Input Dollars to Withdrawl:
100
Input Cents to Withdrawl:
98
Dollars = 140 cents = 2