#include <iostream> #include <fstream> using namespace std; class CDAccount { pu
ID: 3644781 • Letter: #
Question
#include <iostream>#include <fstream>
using namespace std;
class CDAccount
{
public:
CDAccount(); //defaultconstructor
CDAccount (int new_dollars,intnew_cents,double new_interest_rate,int new_term); //parameterizedconstruction
int get_initial_dollars();//function declaration
int get_dollars_at_maturity();
int get_initial_cents();//function declaration
int get_cents_at_maturity();
double get_interest_rate();
int get_term ();
void input (istream&istr);
void output (ostream&ostr);
private://variable declaration
int dollars; //balance
int cents;
double interest_rate;//interest rate in %
int term; //month cd willmature in
};
int main()//declaring objects with parameters
{
CDAccount account (100,0, 10,6);
account.output(cout);
cout << "Enter the CD initialbalance (dollars and cents separately), interest rate, and term."<< endl;
account.input(cin);
ofstream file_out;
file_out.open("Account_info.txt");
if (file_out.fail())
{
cout <<"Could not open file." << endl;
exit(1);
}
account.output(file_out);
file_out.close();
system("pause");
return 0;
}
CDAccount::CDAccount()
{
dollars = 0;
cents=0;
interest_rate = 0;
term = 0;
}
CDAccount::CDAccount (int new_dollars,int new_cents, doublenew_interest_rate, int new_term) :
dollars(new_dollars),cents(new_cents),interest_rate(new_interest_rate/100.), term(new_term)
{
}
int CDAccount::get_initial_dollars ()
{
return dollars;//returnsbalance-original
}
int CDAccount::get_initial_cents ()
{
return cents;//returnsbalance-original
}
int CDAccount::get_dollars_at_maturity()
{
double interest = (dollars+(cents/100)) *interest_rate * (term/12.0);
return (int)(dollars+ interest);//returnsfinal balance after maturity
}
int CDAccount::get_cents_at_maturity ()
{
double interest = (dollars+(cents/100) ) *interest_rate * (term/12.0);
return (int)(dollars+(cents/100) +interest)%100;//returns final balance after maturity
}
doubleCDAccount::get_interest_rate ()
{
return interest_rate;//return interestrate
}
int CDAccount::get_term ()
{
return term;//returns term
}
void CDAccount::input (istream&istr)//data inputting
{
istr >> dollars;
istr >> cents;
istr >> interest_rate;
interest_rate/=100.;
istr >> term;
}
void CDAccount::output(ostream &ostr)//outputting member of class
{
ostr.setf(ios::fixed);
ostr.setf(ios::showpoint);
ostr.precision(2);
ostr << "CD Account interest rate: " <<get_interest_rate()*100. <<endl;// interest rate
ostr << "CD Account initial balance: " <<get_initial_dollars() <<"."<<get_initial_cents()<< endl;//initial balance
ostr << "When your CD matures in " << get_term()<< " months," << endl;//cd maturity period inmonths
ostr << "it will have a balance of " <<get_dollars_at_maturity()<<"."<<get_cents_at_maturity() << " dollars." << endl;//finalbalance after maturity
ostr << endl;
}
Explanation / Answer
You should be able to follow the error messages you get to locate the problem. In your case, the compilation errors are all due to missing spaces, such as "intnew_cents" instead of "int new_cents". Regarding any performance issues, you need to specify what were the program requirements. But the following fixed code does compile: #include #include using namespace std; class CDAccount { public: CDAccount(); //defaultconstructor CDAccount (int new_dollars,int new_cents,double new_interest_rate,int new_term); //parameterizedconstruction int get_initial_dollars();//function declaration int get_dollars_at_maturity(); int get_initial_cents();//function declaration int get_cents_at_maturity(); double get_interest_rate(); int get_term (); void input (istream&istr); void output (ostream&ostr); private://variable declaration int dollars; //balance int cents; double interest_rate;//interest rate in % int term; //month cd willmature in }; int main()//declaring objects with parameters { CDAccount account (100,0, 10,6); account.output(cout); cout > term; } void CDAccount::output(ostream &ostr)//outputting member of class { ostr.setf(ios::fixed); ostr.setf(ios::showpoint); ostr.precision(2); ostrRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.