Coders Packet

Calculation of Electricity bill using C++

By Pratiti Prashant Pande

A C++ program to calculate the electricity bill by defining Constructor, Destructor, Class and if-else structure.


Today, we will see a C++ program to calculate the electricity bill by defining Constructor, Destructor, Class and if-else structure

So let us first fix the rate and unit values for calculating the bill. It can be varied according to the user's need


                          Units             Rates

  1.                0-50            Rs 2 per unit
  2.              50-200          Rs 3.5 per unit
  3.             200-500         Rs 4.5 per unit
  4.         500 and above    Rs 5 per unit

  Also, we define some member functions. 1) Getdata()--> to get meter number,previous and current readings

2) Process()--> to check whether the current readings are greater than the previous unit or not. If not display an appropriate message and restart the program 

3) Calculate()--> to calculate electricity bill 

4) Display()--> to display bill and other details


So now diving into our C++ program. The execution of the program starts from the main function that is void main(). Inside this main function, an object E for the class electricity is created. Then a label(abc) is created whose use is justified by the goto statement and we will come to it when its time of execution is there. 

Now we have E.Getdata(), the compiler will now go and search if there is a class whose object is E. If it finds such a class it will execute the program under it. Since class electricity exists in our program the statements under electricity::Getdata(void)() would get executed.

Similarly with E.Process, E.Calculate, E.Display

If the condition written inside the Process is not met it will ask to re-enter the data and the goto statement will transfer the control to label abc which would clear the screen. If the condition is met then the statements under Calculate and Display would get executed thereby giving us the electricity bill.

For example let us take Meter no:1234, Current readings=600, and Previous readings=300. Now, as defined we first need to calculate the difference, which will give us the units used, and based on the difference obtained the if-else conditions get executed. Difference=600-300=300. So the 'else' part would get executed[bill=bill+(50*2)=0+100=100]. After this the second else part gets executed[bill=100+(150*3.5)=625]

Now the 'if' condition gets executed and we get 1075 and we come out of the if-else structure which tells us our final bill.

Note that: Current readings should be greater than the previous readings.

Also, here are some important points of a constructor and a destructor:

1) A constructor is a special member function of the class. The constructor is invoked whenever an object of its associated class

is created. A constructor can never return any value

2) A constructor should be declared in a public section

3) The destructor function name is the same as that of the class name but is preceded by a tilde(~) symbol. It takes no arguments

4) It should have public access in the class declaration.


A sample output of the electricity bill Calculation



Download Complete Code


No comments yet