, defined a class Account to implement basic bank account functionalities. Redefine the Account class to update the deposit, withdraw and display Account information operations. Overload the + and - operators to perform the deposit and withdraw operations, respectively. Overload the + and - operators such that the value that will be used to increase (or decrease) the Account balance can be on the left or right of the operator. Example: savings + 123.45 (or 123.45 + savings), savings – 123.45 (or 123.45 – savings) where savings is an Account object. Overload the insertion operator to display Account information. Overload < and == operators. An Account is considered lower than another when the balance is lower, and equal when Accounts have the same account number. Write a main() method instantiating two Account objects and test (demonstrate) that all the overloaded operators work correctly. The withdrawal operation should be tested at least two times for a successful and unsuccessful withdrawal.
#include <iostream>
#include <string>
#include<cstdlib>
#include<ctime>
using namespace std;
class Account
{
int accNum;
string name;
float balance;
public:
Account() {}
Account(int _accNum, string _name, float _balance)
:accNum(_accNum), name(_name), balance(_balance) {}
void Assign()
{
srand(static_cast<unsigned int>(time(0)));
accNum = rand();
cout << "Please, enter your name: ";
cin >> name;
cout << "Please, enter your balance: ";
cin >> balance;
}
Account operator+ (float a)
{
balance += a;
return *this;
}
Account operator- (float a)
{
balance -= a;
return *this;
}
bool operator== (Account& a)
{
return balance == a.balance;
}
bool operator< (Account& a)
{
return balance < a.balance;
}
friend float operator+ (float, Account&);
friend float operator- (float, Account&);
friend ostream& operator<< (ostream& os, Account& a);
};
ostream& operator<< (ostream& os, Account& a)
{
return os << "\nYour Account number is " << a.accNum
<< "\nYour Account name is " <<a.name
<< "\nYour balance is " << a.balance<<endl;
}
float operator+ (float f, Account& a)
{
return f + a.balance;
}
float operator- (float f, Account& a)
{
return f - a.balance;
}
int main()
{
Account a(123, "Jack", 2000);
Account b;
b.Assign();
a + 500;
cout << a<<b;
cout << 6000 - a<<endl;
cout << 6000 + a;
b - 200;
cout << b;
cout << (a < b)<<endl;
cout << (a == b);
}
Comments
Leave a comment