STATEMENT:1. Write a class electricity_bill that contains the following members:
Data Members:
· Meter Id
· Units consumed
· amount of bill
Member Functions:
· constructor (default argument constructor with fixed amount bill 0)
· destructor to destroy objects
· Function to calculate the bill according to given criteria
§ For the first 100 units cost is 4 Rs per unit
§ For the next 200 units cost is 6 Rs per unit
§ Beyond 300 units cost is 8 Rs per unit
§ Add 50 RS as meter charges in the bill
§ If bill amount is greater than 300 Rs then add 15% additional surcharges in the bill
§ Finally update the bill field
· Function to show the meter id, units consumed, and bill amount
In main initialize three objects with 15, 115, 315 units respectively, calculate the bill for each object and display the details of each object.Solution:
#include<iostream.h> // class exc Q # 3
#include<conio.h>
class bill
{
private:
int id;
int units;
float t_bill;
public:
void cal_bill();
bill(int i, int u)
{
id=i;
units=u;
t_bill=0;
}
void showdata();
~bill()
{
cout<<"Object gona be destroyed...!!!\n";
showdata();
cout<<"---\n";
getch();
}
};
//////////////////////
void main()
{
bill b1(123,15),b2(456,115),b3(789,315);
cout<<"before calculating bill of object 1\n";
b1.showdata();
b1.cal_bill();
cout<<"after calculating bill of object 1\n";
b1.showdata();
cout<<"before calculating bill of object 2\n";
b2.showdata();
b2.cal_bill();
cout<<"after calculating bill of object 2\n";
b2.showdata();
getch();
}
void bill::showdata()
{
cout<<"Meter id = "<<id<<endl;
cout<<"Units consumed = "<<units<<endl;
cout<<"Total bill = "<<t_bill<<endl;
}
void bill::cal_bill()
{
if(units<=100)
t_bill=units*4;
else
if(units<=300)
t_bill=units*6;
else//(units>300)
t_bill=units*8;
if(t_bill>300)
{
float temp;
temp=(t_bill*15)/100;
t_bill=t_bill+temp;
}
}
No comments:
Post a Comment