Inheritance CI/SI/fixedDeposit

Question :
Consider a Base class as defined below:
class fixDeposit {
protected int accno; // account number
protected double amount; // principal amount

public fixDeposit(int a, double p) {accno = a; amount = p;}
public double interest() { cout<<“The memberfunction in fixDeposit”) ; return 0;}
public void update(double d) { amount += d; }
public void display() { cout<<“The memberfunction in fixDeposit”); }
}
Drive classes (from base class fixDeposit) (i) SIdeposit, and (ii) CIdeposit for accounts earning Simple Interest (SI), and Compound Interest (CI) respectively on the principal amount. Besides inherited members, each derived should have additional data members for (i) yearly rate of interest, and (ii) time period of deposit in number of years. Define following methods for each child/derived class:
(a) Constructors for the derived classes, explicitly calling base class constructors
(b) The method ‘interest’ which overrides the method ‘interest’ of the base class, calculates and returns simple/compound interest
(c) The function ‘display’ which overrides the method ‘display’ of the base class, updates the principal amount by adding interest and then displays the final amount
Write a test driver class and explain how the polymorphic feature in C++ is implemented in the above.


Doubt : Getting 0 interest

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<iostream>
#include<cmath>
using namespace std;

class fixDeposit 
{
protected : int accno;  double amount;
            
public:

fixDeposit(int a, double p) {accno = a; amount = p;}	
	
double interest() {cout<<"The memberfunction in  fixDeposit";}
	
void update(double d) { amount = amount+ d; }
	
void display(){cout<<"The memberfunction in  fixDeposit";}

};

class SIdeposit : public fixDeposit
{
protected: double roi; int timePeriod;

public:

SIdeposit(int ac, int am, int r, int t): fixDeposit(ac,am)
{
roi = r;
timePeriod = t;
}
double interest(){double SI; SI = (amount*roi*timePeriod)/(100); return SI;	}
void display(){double inte =interest(); update(inte) ; cout<<amount; }     	
};

class CIdeposit : public fixDeposit
{
protected:
double roi; int timePeriod;
public:
CIdeposit(int ac, int am, int r, int t):fixDeposit(ac,am)
{
roi = r;
timePeriod = t;
}
double interest(){double CI; CI = (amount*pow((1+roi),timePeriod))-amount; return CI;	}
void display(){double inte =interest(); update(inte) ; cout<<amount;}  	
};



int main()
{
SIdeposit p1(101, 1000, 0.05, 3);	
p1.display();
cout<<" "<<p1.interest()<<" ";	
}
Last edited on
line 27, 41 - shouldn't r be a double?
Yes, you are right. Thanks
Topic archived. No new replies allowed.