Question: In C++ Operator Overloading You are creating a program for a Bank to manage Accounts. The given code declares an Account class which has a

In C++

Operator Overloading

You are creating a program for a Bank to manage Accounts. The given code declares an Account class which has a balance and interest members. The bank asks you to add a new functionality to merge two accounts together, resulting in a new account with the sum of the balances and interests of the given accounts. Overload the + operator to allow adding two Account objects, adding the balances and interests.

The overloaded operator should return a new Account object, with the corresponding member values.

#include

using namespace std;

class Account {

private:

int balance=0;

int interest=0;

public:

Account() {}

Account(int a): balance(a)

{

interest += balance/10;

}

int getTotal() {

return balance+interest;

}

//your code goes here

};

int main() {

int n1, n2;

cin >> n1 >> n2;

Account a(n1);

Account b(n2);

Account res = a+b;

cout << res.getTotal();

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!