Add the following to the IntAccount class. Add a data member type double called
ID: 3552951 • Letter: A
Question
Add the following to the IntAccount class. Add a data member type double called fees. Add a function that subtracts fees from the balance. This function is like addInt. It is void and it has no arguments. It will subtract the amount in the data member fees from the balance. It would be expected that this method would be called perhaps once a month. Since there is a new data member, it should be added to the parameter constructor and the set function of IntAccount class as well. Its value should also be set to a default in the default constructor. Since this value is subtracted, the value fees should not be negative. Make sure the set function and the parameter constructor does not allow it to be negative.
#include <iostream>
#include <string>
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;
protected:
double balance;
};
class IntAccount : private 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 = balance*(1+rate);
Account::set(Account::getName(), bl);
}
string getName()
{
return Account::getName();
}
double getBal()
{
return balance;
}
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() << " ";
cout << ia2.getName() << " has " << ia2.getBal()
<< " dollars with a rate of " << ia2.getRate() << " ";
ia2.addInt();
cout << "after adding interest ";
cout << ia2.getName() << " now has " << ia2.getBal()
<< " dollars ";
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
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;
protected:
double balance;
};
class IntAccount : private Account
{
public:
IntAccount()
{
rate = 0.0;
fees = 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,double f)
{
Account::set(n, b);
if(f<0)
fees = 0;
else
fees = f;
if (0.0 <= r && r <= 1.0)
rate = r;
else
rate = 0.0;
}
double getRate()
{
return rate;
}
void addInt()
{
double bl;
bl = balance*(1+rate);
Account::set(Account::getName(), bl);
}
void subFees()
{
double res;
res = balance - fees;
}
string getName()
{
return Account::getName();
}
double getBal()
{
return balance;
}
private:
double rate;
double fees;
};
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() << " ";
cout << ia2.getName() << " has " << ia2.getBal()
<< " dollars with a rate of " << ia2.getRate() << " ";
ia2.addInt();
cout << "after adding interest ";
cout << ia2.getName() << " now has " << ia2.getBal()
<< " dollars ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.