The program worked fine before implemeting operator overloading, demonstrated in objects #1-3.
The goal is to add 4 overloaded operators as following:
>>read new name and balance from keyboard and update the object's data
<< display the info for the object
+= increase balance
-= decrease balance
Now, when I run my program. Only the info for object 4 shows and it does not output a second time after adding 1000 to the total.
Anything criticism and guidance is appreciated.
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <conio.h>
#include "SavingsAccount.h"
using namespace std;
ofstream fileout;
int main()
{
fileout.open("E:\\SavingsOut.txt");
SavingsAccount Saver4 ("John", "Claude", 3250, 4);
cout << Saver4 << endl;
cin >> Saver4;
Saver4 += 1000;
cout << Saver4 << endl;
// declare objects
SavingsAccount Saver1 ("Margaret", "Olson", 2000, 1);
SavingsAccount Saver2 ("Debra", "Baxter", 2);
SavingsAccount Saver3 (3);
Saver1.printData();
Saver2.printData();
Saver3.printData();
// set new parameters for Saver2
Saver2.setBalance (5000);
// set new parameters for Saver3
Saver3.setFirstName ("Arturo");
Saver3.setLastName ("Ortiz");
Saver3.setBalance (10000);
// print new data for the objects
cout<<"\nNew data for Saver 2:";
fileout<<"\nNew data for Saver 2:";
Saver2.printData();
cout<<"\nNew data for Saver 3:";
fileout<<"\nNew data for Saver 3:";
Saver3.printData();
cout<<"\nPress any key to continue.\n"<<endl;
getch();
}
Code:
#include "SavingsAccount.h"
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
extern ofstream fileout;
double SavingsAccount::annualInterestRate = 0.00;
// constructor function with 0 parameters
SavingsAccount::SavingsAccount(int serial):objectnumber (serial)
{
setFirstName("");
setLastName ("");
setBalance (0.0);
setInterestRate (.05);
}
// constructor functions with 1 parameter
SavingsAccount::SavingsAccount (string first, string last, int serial ):objectnumber (serial)
{
setFirstName (first);
setLastName (last);
setBalance (0.0);
setInterestRate (.05);
}
// constructor function with 2 parameters
SavingsAccount::SavingsAccount(string first, string last, double balance, int serial):objectnumber (serial)
{
setFirstName (first);
setLastName (last);
setBalance (balance);
setInterestRate (.05);
}
//destructor
SavingsAccount::~SavingsAccount()
{
cout<< "Object " << objectnumber << " no longer exists" <<endl;
fileout<< "Object " << objectnumber << " no longer exists" <<endl;
}
// calculate balance
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;
}
// set balance
void SavingsAccount::setBalance (double b)
{
savingBalance = b;
}
// get functions
double SavingsAccount::getInterestRate()
{
return annualInterestRate;
}
int SavingsAccount::getObjectNumber () const
{
return objectnumber;
}
string SavingsAccount::getFirstName()
{
return firstName;
}
string SavingsAccount::getLastName()
{
return lastName;
}
// get balance
double SavingsAccount::getBalance() const
{
return savingBalance;
}
//overloaded functions
istream& operator>> (istream &cin, SavingsAccount &Saver4)
{
string f, l;
double s;
cin >> f >> l >> s;
Saver4.firstName = f;
Saver4.lastName = l;
Saver4.savingBalance = s;
return cin;
}
ostream& operator<< (ostream &out, SavingsAccount &Saver4)
{
cout << Saver4.getFirstName() << " " << Saver4.getLastName() << " " << Saver4.getBalance();
return cout;
}
void SavingsAccount::operator += (double b)
{
savingBalance += b;
}
void SavingsAccount::operator -= (double b)
{
savingBalance -= b;
}
// print function
void SavingsAccount::printData()
{
cout<< "\nSerial Number: " << objectnumber;
cout<< "\nName: " << firstName <<" "<< lastName;
cout<< "\nBalance: " << savingBalance;
cout<< "\nNew Balalce: " << calcNewBalance()<<endl;
fileout<< "\nSerial Number: " << objectnumber;
fileout<< "\nName: " << firstName <<" "<< lastName;
fileout<< "\nBalance: " << savingBalance;
fileout<< "\nNew Balalce: " << calcNewBalance();
}
Code:
#ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H
#include <string>
using namespace std;
class SavingsAccount
{
public:
//overloaded constructor functions
SavingsAccount (int);
SavingsAccount (string, string, int);
SavingsAccount (string, string, double, int);
//destructor
~SavingsAccount();
//set functions
void setFirstName ( string );
void setLastName ( string );
void setBalance ( double );
void static setInterestRate ( double );
//get functions
string getFirstName();
string getLastName();
double getBalance() const;
int getObjectNumber () const;
static double getInterestRate ();
//calculation functions
double calcNewBalance();
//print functions
void printData();
//overloaded functions
friend std::ostream& operator<< (ostream &, SavingsAccount&);
friend std::istream& operator>> (istream &, SavingsAccount&);
void operator += (double);
void operator -= (double);
private:
string firstName;
string lastName;
double savingBalance;
double monthlyRate;
static double annualInterestRate;
const int objectnumber;
};
#endif