This assignment asks to add some code from to a previous assignment I had. I have been confused about this overload operators alot and im confused on how to do it in my code any help with this will be good
A) overload the >> operator to allow for reading a new name and new balance from the keyboard and use the data to update the object's data members
B) Overload the << operator to dsiplay the Name, Balance, and Interest Rate of an object on the screen with proper labels.
C) Overload the += operator to allow an increase to a saver's Balance. (Saver1 += 1000;) use a void member function
D) Overload the -= operator to allow a decrease to a saver's Balance. (Saver1 -= 1000;) Use a void member function.
E) create a 4th object and use it to test all the member functions and overloaded operator functions.
I have done part A and B and wonder if that is correct and also wondering how i would put in part C especially in my SavingsAccount.cpp. I'm not that strong at C++ and this subject of overloading has me really confused so any help would be appreciated
Here is my Header:
#ifndef SAVINGSACCOUNT_H
#define SAVINGSACCOUNT_H
#include<iostream>
#include<string>
using namespace std;
class SavingsAccount
{
public:
friend ofstream& operator<<(ofstream& out, SavingsAccount& obj);
friend istream& operator>>(istream& cin, SavingsAccount& obj);
public:
string firstName;
string lastName;
float savingBalance;
float annualInterestRate;
int objectnumber;
SavingsAccount();
SavingsAccount(string fname,string sname);
SavingsAccount(string fname,string sname,float balance);
void setName(string fname,string sname);
const string getName();
void setBalance(float bal);
const float getBalance();
void setInterestRate(float i);
float getInterestRate();
const int getNumber();
void calculateNewBalance();
void display();
};
#endif
here is my SavingsAccount cpp file:
#include<iostream>
#include<string>
#include<fstream>
#include "SavingsAccount2.h"
using namespace std;
extern ofstream Fileout;
ofstream& operator<<(ofstream& out, SavingsAccount& obj)
{
out << obj.firstName << " " << obj.lastName
<< " " << obj.savingBalance << " " << obj.annualInterestRate << '\n';
return out;
}
istream& operator>>(istream& cin, SavingsAccount& obj)
{
cin >> obj.firstName >> obj.lastName >> obj.newbalance;
return cin;
}
SavingsAccount::SavingsAccount()
{
firstName="";
lastName="";
savingBalance=0;
annualInterestRate=5.0;
objectnumber=1;
}
SavingsAccount::SavingsAccount(string fname,string sname)
{
firstName=fname;
lastName=sname;
savingBalance=0;
annualInterestRate=5.0;
objectnumber=1;
}
SavingsAccount::SavingsAccount(string fname,string sname,float balance)
{
firstName=fname;
lastName=sname;
savingBalance=balance;
annualInterestRate=5.0;
objectnumber=1;
}
void SavingsAccount::setName(string fname, string sname)
{
firstName= fname;
lastName= sname;
}
const string SavingsAccount::getName()
{
return lastName;
}
void SavingsAccount::setBalance(float bal)
{
savingBalance=bal;
}
const float SavingsAccount::getBalance()
{
return savingBalance;
}
void SavingsAccount::setInterestRate(float i)
{
annualInterestRate=i;
}
float SavingsAccount::getInterestRate()
{
return annualInterestRate;
}
const int SavingsAccount::getNumber()
{
return objectnumber;
}
void SavingsAccount::calculateNewBalance()
{
savingBalance= (savingBalance + (annualInterestRate/12)*savingBalance/100);
}
void SavingsAccount::display()
{
Fileout<<"firstName= "<<firstName<<"\tlastName= "<<lastName<<"\tsavingBalance= "<<savingBalance<<"\tannualInterestRate= "<<annualInterestRate<<endl;
}
and here is my main cpp file:
#include<iostream>
#include<string>
#include<fstream>
#include "SavingsAccount2.h"
using namespace std;
ofstream Fileout;
int main()
{
Fileout.open("F:\\SavingsAccount1.txt");
SavingsAccount* sa1= new SavingsAccount("Margaret", "Olson", 2000);
SavingsAccount* sa2= new SavingsAccount("Debra", "Baxter");
SavingsAccount* sa3= new SavingsAccount();
sa2->setBalance(5000);
sa3->setName("Arturo", "Ortiz");
sa3->setBalance(10000);
sa1->calculateNewBalance();
sa2->calculateNewBalance();
sa3->calculateNewBalance();
Fileout<<"Displaying data for rate =5%"<<endl;
Fileout<<"Data for saver 1: \n";
sa1->display();
Fileout<<"Data for saver 2: \n";
sa2->display();
Fileout<<"Data for saver 3: \n";
sa3->display();
sa1->setInterestRate(10);
sa2->setInterestRate(10);
sa3->setInterestRate(10);
sa1->calculateNewBalance();
sa2->calculateNewBalance();
sa3->calculateNewBalance();
Fileout<<endl<<"Displaying data for rate =10%"<<endl;
Fileout<<"Data for saver 1: \n";
sa1->display();
Fileout<<"Data for saver 2: \n";
sa2->display();
Fileout<<"Data for saver 3: \n";
sa3->display();
return 0;
}