In: Computer Science
The main problem of this question can be faced when accessing the private variables of the parent class (in this case, Account Class). But we have methods set for that purpose, and thus the child class (Savings) will also inherit them.
class Account{
private:
double balance;
public:
void setBalance(double amount);
const double getBalance();
};
void Account::setBalance(double amount){
this->balance = amount;
}
const double Account::getBalance(){
return this->balance;
}
The above code snippet just describes the structure of our parent class (from now on we can refer as Account class). The account class has the standard variables and methods, described in the question.
Adding the code of the child class(Savings Class) below:
class Savings : public Account{ // Savings Class inheriting the parent class
private:
double interest_rate;
public:
// Instead of using constructor, we are using a method to set
// the interest rate
void setInterestRate(double interest_rate);
void addInterest();
};
void Savings::setInterestRate(double interest_rate){
this->interest_rate = interest_rate;
}
void Savings::addInterest(){
double new_balance;
double old_balance = getBalance();
new_balance = (old_balance) * (1 + (interest_rate)/100);
setBalance(new_balance);
}
Just for the sake of representation, we have created two variables (old_balance and new_balance) in the addInterest() method of the Savings class. We use the setBalance() method in the savings class, as the child class cannot access the private members of the parent class, thus using the method provided by the parent class. Attaching the code of the driver class:
int main(){
// Initialising our saving account and then adding a
// balance of 100.00 units
Savings *savings = new Savings();
savings->setBalance(100.00);
const double trial_get_balance = savings->getBalance();
cout << "Initial Balance: "<< trial_get_balance << endl;
// Creating a Saving (child class) and changing the value
// of current balance using an interest_rate
// In this class, we can set the interest_rate either using a
// constructor or a method. I have used a method to set the
// interest rate.
savings->setInterestRate(2.00);
savings->addInterest();
cout << "After Adding interest: "<< savings->getBalance() << endl;
return 0;
}
As the code comments explain, we are using the driver class to create an instance of the object of the Savings class (child class of Account) and thus inheriting the methods and variables. But the private variables (or methods) would not be accessible to it. Furthermore, we are adding the setInterest() (Here adding 2% as the interest rate) method to set the interest value and updating the balance in the addInterest() method. The addInterest() method uses the setBalance() method of the Account class to set its private variable.
The output of the program would be as follows:
Thus summing up the question, the whole code is pasted below:
#include<bits/stdc++.h>
using namespace std;
class Account{
private:
double balance;
public:
void setBalance(double amount);
const double getBalance();
};
void Account::setBalance(double amount){
this->balance = amount;
}
const double Account::getBalance(){
return this->balance;
}
class Savings : public Account{
private:
double interest_rate;
public:
// Instead of using constructor, we are using a method to set
// the interest rate
void setInterestRate(double interest_rate);
void addInterest();
};
void Savings::setInterestRate(double interest_rate){
this->interest_rate = interest_rate;
}
void Savings::addInterest(){
double new_balance;
double old_balance = getBalance();
new_balance = (old_balance) * (1 + (interest_rate)/100);
setBalance(new_balance);
}
int main(){
// Initialising our saving account and then adding a
// balance of 1000.00 units
Savings *savings = new Savings();
savings->setBalance(100.00);
const double trial_get_balance = savings->getBalance();
cout << "Initial Balance: "<< trial_get_balance << endl;
// Creating a Saving (child class) and changing the value
// of current balance using an interest_rate
// In this class, we can set the interest_rate either using a
// constructor or a method. I have used a method to set the
// interest rate.
savings->setInterestRate(2.00);
savings->addInterest();
cout << "After Adding interest: "<< savings->getBalance() << endl;
return 0;
}
In case of any doubts, please revert back.