In: Computer Science
c++
using polymorphyism , create an inheritance hierarchy for the following types of insurance :
Home, vehicle , life
2 member functions should be included:
- calcpremium
- Home- 0.5% of the value of the home
- Life- 1% of the value of the policy
- Vehicle - 0.5% of the value of the policy
calcCommission
Home- 0.2% of the value of the home
Life- 1% of the value of the policy
Vehicle - 0.5% of the value of the policy
in main
1. instantiate an object for each type of insurance policy.
- Home: value is $457,999.00
- Life: policy value $1,000,000.00
- Vehicle: policy value is $25,000.00
2. Call a global function ( exercising polymorphism) to print out the premium
and commission for each policy.
3. make sure that objects of the base class cannot be instantiated.
#include <iostream>
using namespace std;
//base class to hold insurance details
class Insurance
{
public:
//declare variables
int value;
//functions expected to be overridden by child classed
virtual double calcpremium()=0;
virtual double calcCommission()=0;
//global function to display insurance details
void display()
{
cout<<"\nValue:
\t\t"<<value;
cout<<"\nPremium:
\t"<<calcpremium();
cout<<"\nCommission:
\t"<<calcCommission();
}
};
//home insurance class
class Home:public Insurance
{
public:
Home(int v)
{
value=v;
}
double calcpremium()
{
return value*
0.5/100;
}
double calcCommission()
{
return
value*0.2/100;
}
};
//vehicle class
class Vehicle:public Insurance
{
public:
Vehicle(int v)
{
value=v;
}
double calcpremium()
{
return value*
0.5/100;
}
double calcCommission()
{
return
value*0.5/100;
}
};
//life insurance class
class Life:public Insurance
{
public:
Life(int v)
{
value=v;
}
double calcpremium()
{
return value*
0.1/100;
}
double calcCommission()
{
return
value*0.1/100;
}
};
//main function which triggers the class
int main()
{
//delcare insurance object
Insurance *i;
//declare objects of three classes
Home home(457999);
Vehicle vehicle(25000);
Life life(1000000);
//assign home object to base class object
cout<<"\n***Home Insurance****";
i=&home;
i->display();
//assign life object to base class object
cout<<"\n***Life Insurance****";
i=&life;
i->display();
//assign vehicle object to base class object
cout<<"\n***Vehicle Insurance****";
i=&vehicle;
i->display();
return 0;
}
Sample Output:
***Home Insurance****
Value: 457999
Premium: 2289.99
Commission: 915.998
***Life Insurance****
Value: 1000000
Premium: 1000
Commission: 1000
***Vehicle Insurance****
Value: 25000
Premium: 125
Commission: 125