1. Write a program that includes a SavingsAccount class. Data members: firstname
ID: 3759240 • Letter: 1
Question
1. Write a program that includes a SavingsAccount class.
Data members:
firstname(string)
lastname(string)
savingsBalance
annualInterestRate(static)
objectnumber(1,2,3...)const
Function members:
Default constructor - allows objects to be created without arguments. Set the names to the empty string and the balance to zero
Constructor that allows an object to be created with name arguments only. Set the balance to zero.
Constructor that allows an object to be created with name arguments and a beginning balance.
(All constructors will provide a serial number to each object.)
setName
get Name const
setBalance
getBalance const
setInterestRate static
getInterestRate static
getNumber const
display - display all account information
calculateNewBalance (monthly rate = annualInterestRate / 12)
Destructor(Display message to indicate that the object no longer exists)
2. Write a "driver" program to test the class. Perform the following tasks in this order:
Declare at least 3 objects:
Saver1: Margaret Olson with a balance of $2,000
Saver2: Debra Baxter with no balance
Saver3: object without name or balance
Set Baxter's balance to $5,000
set Saver3's name to Arturo Ortiz and the balance to $10,000
calculate the new monthly balance for each customer (new balance = old balance + monthly interest earned)
display all the data for all of the objects
Set the annual interest rate to 10.0%
calculate the new monthly balance for each customer again
display all the data for all of the objects
3. A header file will be used to declare the class. Other header files may be used if you wish. A separate .cpp file will be used to implement the class member functions. Hence, your program will consist of at least one header file and 2 .cpp files.
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
extern ofstream fileout;
SavingsAccount::SavingsAccount()
{
setFirstName("");
setLastName ("");
setBalance (0.0);
setInterestRate (.05);
}
SavingsAccount::SavingsAccount (string first, string last )
{
setFirstName (first);
setLastName (last);
setBalance (0.0);
setInterestRate (.05);
}
SavingsAccount::SavingsAccount(string first, string last, double balance)
{
setFirstName (first);
setLastName (last);
setBalance (balance);
setInterestRate (.05);
}
double SavingsAccount::calcNewBalance()
{
monthlyRate = annualInterestRate/12;
return (monthlyRate * savingBalance) + savingBalance;
}
void SavingsAccount::setInterestRate(double rate)
{
annualInterestRate = rate;
}
void SavingsAccount::setFirstName(string f)
{
firstName = f;
}
void SavingsAccount::setLastName(string l)
{
lastName = l;
}
void SavingsAccount::setBalance (double b)
{
savingBalance = b;
}
double SavingsAccount::getInterestRate(double rate)
{
return annualInterestRate;
}
string SavingsAccount::getFirstName()
{
return firstName;
}
string SavingsAccount::getLastName()
{
return lastName;
}
double SavingsAccount::getBalance() const
{
return savingBalance;
}
void SavingsAccount::printData()
{
cout<< " Name: " << firstName <<" "<< lastName;
cout<< " Balance: " << savingBalance;
cout<< " New Balalce: " << calcNewBalance();
}
#include <string>
using namespace std;
class SavingsAccount
{
public:
~SavingsAccount ();
SavingsAccount (string, string);
SavingsAccount (string, string, double);
void setFirstName ( string );
void setLastName ( string );
void setBalance ( double );
void static setInterestRate ( double );
string getFirstName();
string getLastName();
double getBalance() const;
static double getInterestRate (double);
double calcNewBalance();
void printData();
private:
string firstName;
string lastName;
double savingBalance;
double monthlyRate;
static double annualInterestRate;
int objectnumber;
};
*********************************
2
#include <iostream>
#include <iomanip>
#include <fstream>
#include "SavingsAccount.h"
using namespace std;
ofstream fileout;
int main()
{
fileout.open(":\SavingsOut.txt");
SavingsAccount Saver1 ("Margaret", "Olson", 2000, 1);
SavingsAccount Saver2 ("Debra", "Baxter", 2);
SavingsAccount Saver3 ("","",3);
Saver1.printData();
Saver2.printData();
Saver3.printData();
Saver2.setBalance (5000);
Saver3.setFirstName ("Arturo");
Saver3.setLastName ("Ortiz");
Saver3.setBalance (10000);
cout<<" New data for Saver 2:";
fileout<<" New data for Saver 2:";
Saver2.printData();
cout<<" New data for Saver 3:";
fileout<<" New data for Saver 3:";
Saver3.printData();
int c;
cin>> c;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.