Program to Electricity bill calculation using OOP in C++
- Home
- Calculations
- Program to Electricity bill calculation using OOP in C++
- On
- By
- 0 Comment
- Categories: Calculations, function/method, OOP, Oop Java
Program to Electricity bill calculation using OOP in C++
Program to Electricity bill calculation using OOP in C++
In this tutorial, we will discuss the Program to Electricity bill calculation using OOP in C++ language
In this topic, we are going to learn how to calculate the usage of electricity bill using OOP in C++ language
We can calculate monthly electric power usage using different methods in cpp language
In this tutorial, we will discuss two methods for calculating electricity bill using OOP in C++ language
Before we write a program to calculate the electricity bill, we must understand electricity charges and rates.
/*1 - 100 unit - 1/= 101-200 unit - 2/= 201-300 unit - 3/= above 300 - 5/=*/ //for example
Electricity bill calculation using OOP
Example
This program has the following explanations of calculating electricity bill
- Declare total unit consumed by the customer using the variable unit.
- if the unit consumed less or equal to 100 units, calculates the total amount of usage =unit*1
- if unit consumed between 100 to 200 units, calculates the total amount of usage =(100*1)+(unit-100)*2)
- if unit consumed between 200 to 300 units ,calculates total amount of usage =(100*1)+(100*2)+(unit-200)*3
- if unit consumed above 300 units ,calculates total mount of usage =(100*1)+(100*2)+(100*3)+(unit-300)*5
- No additional charge
Electricity bill calculation using class
In this program, we will discuss how to calculate electricity bill using class in C++ language
Program 1
#include <iostream> #include <conio.h> using namespace std; class EleBill { private: int cur_Unit,pre_Unit,amt,unit; //variable drclaration public://function declaration as public void get(); void printAmt(); }; void EleBill::get()//function definition { cout << "Enter previous unit:" << endl; cin>>pre_Unit;//takes input from the user cout << "Enter current unit:" << endl; cin>>cur_Unit;//takes input from the user } /*1 - 100 = 1 101 - 200 = 2 201 - 300 = 3 above 300 - 5*/ void EleBill::printAmt()//function definition { { unit=cur_Unit-pre_Unit; if(unit>0 && unit<=100) { amt=unit*1; } if(unit>100 && unit<=200) { amt=unit*2; } if(unit>200 && unit<=300) { amt=unit*3; } if(unit>300) { amt=unit*5; } cout << "Bill charge: " <<amt <<endl; } int main() { EleBill o; o.get(); o.printAmt(); getch(); return 0; }
When the above code is executed, it produces the following result
Enter previous unit: 4577 Enter current unit: 5012 Bill charge: 2175
Electricity bill calculation of a person
In this code, we will discuss how to calculate electricity bills of a person with full detail using class in C++ language
Program 2
#include <iostream> using namespace std; class Calc_Ebill{ int cus_Id; char cus_Name[50]; int units; double bill_Bal; public: void get() { cout << "Please enter detail of customer below: \n" << endl; cout << "Enter customer id: " << endl; cin>>cus_Id; cout << "Enter customer Name: " << endl; cin>>cus_Name; cout << "Enter the number of units consumed: " << endl; cin>>units; } void put() { cout << "\nCustomer details \n" << endl; cout << "\nCustomer id is: " <<cus_Id<< endl; cout << "\nCustomer Name is: " << cus_Name<<endl; cout << "\nNumber of units consumed: " << bill_Bal<<endl; cin>>units; } /*1 - 100 = 1 101 - 200 = 2 201 - 300 = 3 above 300 - 5*/ void calc_Amt() { if(units<=100) bill_Bal=units*1; else if(units<=200) bill_Bal=100*1+(units-100)*2; else if(units<=300) bill_Bal=100*1+100*2+(units-200)*3; else if(units>300) bill_Bal=100*1+100*2+100*3+(units-300)*5; } }; int main() { cout << "Here your output\n\n" << endl; Calc_Ebill b; b.get(); b.calc_Amt(); b.put(); return 0; }
When the above code is executed, it produces the following result
Suggested for you
Similar post
Java code to calculate electricity bill
C code to calculate electricity bill
C++ code to calculate electricity bill