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;
}

It's just a simple inline method, or you can put the implementation code in the *.cpp file if you wish.

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;
    void operator+=(int val) // <<<<<<<<<<<<<<<<<<<<<<<
    {
        this->savingBalance += val;
    };

Edited 2 Years Ago by Ancient Dragon

in this case the this pointer is optional -- I like to add it so that it is very clear that savingBalance is a member of the class and not some other kind of variable.

Okay thank you and am i doing the part A and B right on my SavingsAccount.cpp especially with the overload operator >>

I get a error on my main cpp file:

#include<iostream>
#include<string>
#include<fstream>
#include<istream>
#include "SavingsAccount3.h"

using namespace std;

ofstream Fileout;

int main()
{
    Fileout.open("E:\\SavingsAccoun.txt");

    SavingsAccount* sa1= new SavingsAccount("Margaret", "Olson", 2000);
    SavingsAccount* sa2= new SavingsAccount("Debra", "Baxter");
    SavingsAccount* sa3= new SavingsAccount();

    SavingsAccount obj;
    cin >> obj;

    SavingsAccount obj;
    ofstream out("SavingsAccount.txt");
    out << obj;

    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;
}

1>c:\project 4x\project 4x\savingsmain3.cpp(22): error C2086: 'SavingsAccount obj' : redefinition
1>          c:\project 4x\project 4x\savingsmain3.cpp(19) : see declaration of 'obj'

You're trying to declare the same object obj twice at lines 19 and 22.

Your use of Fileout as a global object is extremely bad practice and the sort of thing that leads to unmanageable spagetti code. make Fileout local to main and then pass a reference to it to SavingsAccount::display. If you make the parameter for SavingsAccount::display a ostream& then you can pass all sorts of streams to it, cout, ofstream ostringstream etc.

This article has been dead for over six months. Start a new discussion instead.