Declare a class Fixed_Deposit with member variables are principal, rate and time, and member functions are MaturityAmount() to calculate the compound interest and maturity amount, and display() to print the total maturity amount(principal+compound interest)
#include <iostream>
#include <iomanip>
using namespace std;
class Fixed_Deposit {
double principal;
double rate;
int time;
public:
Fixed_Deposit(double p, double r, int t);
double MaturityAmount() const;
void display(ostream& os) const;
};
Fixed_Deposit::Fixed_Deposit(double p, double r, int t) :
principal(p), rate(r), time(t) {}
double Fixed_Deposit::MaturityAmount() const
{
double result = principal;
for (int i=0; i<time; i++) {
result *= (1 + rate/100);
}
return result;
}
void Fixed_Deposit::display(ostream& os) const
{
os << "Maturity amount is " << fixed << setprecision(2)
<< MaturityAmount();
}
int main() {
double pr, r;
int t;
cout << "Principal, rate, time ";
cin >> pr >> r >> t;
Fixed_Deposit fd(pr, r, t);
fd.display(cout);
}
Comments
Leave a comment