An electricity board charges the following rates to domestic users to discourage large consumption of energy :
For the first 100 units − 60 P per unit
Beyond 300 units − 70 P per unit
If the total cost is more than Rs.250.00 then an additional surcharge of 25% is added to the difference. Define a class Electricity in which the function Bill computes the cost. Define a derived class More_Electricity and override Bill to add the surcharge.
Program Code:
#include<iostream>
using namespace std;
class electricity
{
protected:
float unit;
float cost;
public:
void bill()
{
cout<<"Enter the no. of units consumed : "<<endl;
cin>>unit;
if(unit <= 100)
{
cost=0.60*unit;
cout<<"Cost up to 100 unit is Rs : "<<cost;
cout<<endl;
cout<<endl;
}
else
{
if(unit>300)
{
cost=0.70*unit;
cout<<"Beyond 300 units is Rs : "<<cost;
cout<<endl;
}
}
}
};
class more_electricity : public electricity
{
float surcharge,diff,total_cost;
public:
void bill()
{
electricity :: bill();
if(cost>250.00)
{
diff = cost-250;
surcharge = diff * 0.25;
total_cost=cost+surcharge;
cout<<"Bill amount with surcharge is Rs :"<<total_cost;
cout<<endl;
}
else
{
cout<<"Bill amount is Rs :"<<cost;
cout<<endl;
}
}
};
void main()
{
more_electricity obj;
obj.bill();
return 0;
}
The program output is tested on www.jdoodle.com
Output:
Enter the no. of units consumed : 400
Beyond 300 units is Rs : 280
Bill amount with surcharge is Rs : 287.5
Thanks
Mukesh Rajput
Post A Comment:
0 comments: