Given the savingsAcct class, implement and test all member and friend functions
ID: 3578067 • Letter: G
Question
Given the savingsAcct class, implement and test all member and friend functions of the class.
class savingsAcct
{
friend istream& operator>>(istream&, savingsAcct&);
friend ostream& operator<<(ostream&, const savingsAcct&);
public:
savingsAcct();
savingsAcct(string acctNum, double amt);
// Postcondition: balance is set to the value passed to parameter amt
static double * getAddressOfVar_annulRate();
// Postcondition: return the address of static variable annualRate (should the same for all objects!)
static double getAnnualRate();
// Postcondition: returns annual interest rate
static void setAnnualRate(double rate); // change the annual interest rate
// Postcondition: annualRate has been changed to the value passed to parameter rate
void computeIntrest();
// Interest is computed at the end of each month using monthly interest rate (i.e.,annualRate/12)
// Postcondition: interest is computed at the end of the month using the current balance
private:
string acctNo; // e.g., A1234
double balance;
static double annualRate; // classwide; store in a memory location shared by all objects
double interest; // monthly interest
};
Note: The code for the main function is provided below along with a sample output to help you learn the concept and practices of static members of a class in a systematic way. Do note that a static variable must be initialized in file scope.
int main()
{
cout << "As of now, no object has been declared. ";
cout << "However, static data members exist before any object is declared:" << endl;
cout << " The address of its storage location: "
<< int(savingsAcct::getAddressOfVar_annulRate()) << endl;
cout << " Annual interest rate: " << savingsAcct::getAnnualRate() * 100
<< "%" << endl << endl;
cout << "Still no object has declared, we now set the interest rate to 1.5%." << endl;
savingsAcct::setAnnualRate(0.015);
cout << " Now, the annual interest rate is: " << savingsAcct::getAnnualRate() * 100
<< "%" << endl << endl;
cout << "We now declare and initialize 2 savingAcct objects." << endl;
cout << "We also compute interest at the end of 1st month and display their cnotents: ";
savingsAcct s1("A1234", 5000), s2("A9876", 8000);
s1.computeIntrest();
s2.computeIntrest();
cout << s1 << endl << endl;
cout << s2 << endl << endl;
cout << "We now display the address of static variable annualRate" << endl;
cout << " by calling "getAddressOfVar_annulRate()" member function" << endl << endl;
cout << "Via s1: the address of "annualRate" is " << int(s1.getAddressOfVar_annulRate())
<< endl;
cout << "Via s2: the address of "annualRate" is " << int(s2.getAddressOfVar_annulRate())
<< endl;
cout << "Via s1: the address of "annualRate" is "
<< int(savingsAcct::getAddressOfVar_annulRate()) << endl;
cout << " It should be quite clear that static members are indeed "class-wide!!!"" << endl;
cout << "Note: calling a static member function, "this" pointer is not involved!!! ";
return 0;
Explanation / Answer
Hi, Friend your output imahe is not visible(not uploaded properly) so i can not verify input.output.
Please let me know in case of any issue.
Please find my savingsAcct implementation.
#include <iostream>
#include "savingsAcct.h"
using namespace std;
savingsAcct::savingsAcct(){
acctNo = "A123";
balance = 0;
annualRate = 0;
interest = 0;
}
savingsAcct::savingsAcct(string acctNum, double amt){
acctNo = acctNum;
balance = amt;
}
double* savingsAcct::getAddressOfVar_annulRate(){
return (double *)&annualRate;
}
double savingsAcct::getAnnualRate(){
return annualRate;
}
void savingsAcct::setAnnualRate(double rate){
annualRate = rate;
}
void savingsAcct::computeIntrest(){
double r = annualRate/12;
interest = r*balance;
}
istream& operator>>(istream& in, savingsAcct& acct){
cout<<"Enter account number: ";
in>>acct.acctNo;
cout<<"Enter balance: ";
in>>acct.balance;
return in;
}
ostream& operator<<(ostream& out, const savingsAcct& acct){
out<<"Account Number: "<<acct.acctNo<<endl;
out<<"Balance: "<<acct.balance<<endl;
return out;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.