Take the following C++ code and make the following changes. Change the inheritance to private inheritance. Change the private data member balance to protected. Define get function members of IntAccount class to retrieve name and balance. Since the inheritance is private, an object of IntAccount will not be able to call getName or getBal. Functions that do those must be defined in the IntAccount class. If you realize what protected means, you can change the definition of addInt.
#include
#include
using namespace std;
class Account
{
public:
Account()
{
name = "John Doe";
balance = 0.0;
}
Account(string n, double b)
{
set(n, b);
}
void set(string n, double b)
{
name = n;
balance = b;
}
string getName()
{
return name;
}
double getBal()
{
return balance;
}
private:
string name;
double balance;
};
class IntAccount : public Account
{
public:
IntAccount()
{
rate = 0.0;
}
IntAccount(string n, double b, double r)
: Account(n, b)
{
if (0.0 <= r && r <= 1.0)
rate = r;
else
rate = 0.0;
}
void set(string n, double b, double r)
{
Account::set(n, b);
if (0.0 <= r && r <= 1.0)
rate = r;
else
rate = 0.0;
}
double getRate()
{
return rate;
}
void addInt()
{
double bl;
bl = getBal()*(1+rate);
Account::set(getName(), bl);
}
private:
double rate;
};
int main()
{
IntAccount ia1, ia2("Sarah Smith", 100.0, 0.03);
ia1.set("Mary Smith", 200.0, 0.04);
cout << ia1.getName() << " has " << ia1.getBal()
<< " dollars with a rate of " << ia1.getRate() << "\n\n";
cout << ia2.getName() << " has " << ia2.getBal()
<< " dollars with a rate of " << ia2.getRate() << "\n\n";
ia2.addInt();
cout << "after adding interest\n";
cout << ia2.getName() << " now has " << ia2.getBal()
<< " dollars\n\n";
return 0;
}