1. 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.

here are the links to the three files for the pervious project:

https://www.dropbox.com/s/g7k595h2hmsvi58/SavingsAccount1.h

https://www.dropbox.com/s/d8z9nuhlvl4i5bj/Savingsmain.cpp

https://www.dropbox.com/s/hsm97wvscj6ot3c/SavingsAccount1.cpp

here is the assignment as well from the pervious probject:

https://www.dropbox.com/s/qzeg3ocji5aebdy/Asmt3.doc

Recommended Answers

All 3 Replies

Use a friend method to overload those operators, for example

#include <iostream>
#include <fstream>
using namespace std;

class SavingsAccount
{
public:
    friend ofstream& operator<<(ofstream& out, SavingsAccount& obj);
public:
    string firstName;
    string lastName;
    float savingBalance;
    float annualInterestRate;
    int objectnumber;
...
...

};


ofstream& operator<<(ofstream& out, SavingsAccount& obj)
{
    out << obj.firstName << " " << obj.lastName
        << " " << obj.savingBalance << " " << obj.annualInterestRate << '\n';
    return out;

}

The other operators are similar to the above. For the >> operator use istream instead of ofstream and call it with cin

SavingsAccount obj;
cin >> obj;

For the << operator which I showed you how to code you just open an output stream.

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

How do you write the other overloaded operators += and -= using a void member funtion? Also confused on how to write the 4th object on step E

I'm not going to write your whole assignment for you. Doesn't your book cover operator overloading? There are lots of online tutorials, such as this one.

Don't worry about step E until you have the others finished.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.