+ 12
Define a class Ele_Bill in C++
Private members : Cname (char array) Pnumber(long) No_of_units(int) Amount(float) Calc_Amount() this member function should calculate the amount as No_of_units*cost. Amount can be calculated according to the following conditions: No of units. Cost 1st 50 units. Free Next 100 units. 0.80@unit Next 200 units. 1.00@unit Remaining units. 1.20@unit Public members: 1.A function Accept () which allows user to enter Cname, Pnumber, no_of _units and invoke function Calc_Amount() 2.A function Display() to display the values of all the data members on the screen
1 Answer
+ 2
class Ele_Bill{
// by default all members of class are private n C++
char* Cname;
long Pnumber;
int No_of-units;
float Amount;
void Calc_amount (int nou){
int n = nou;
float a = 0.0f;
if(n > 350){a += (n - 350) * 1.2; n = 350;} // get cost for units 350+
if(n > 150){a += (n - 150) * 1.0; n = 150;} // get cost for units 150-350
if(n > 50){a += (n - 50) * 0.8; n = 50;} // get cost for units 50-150
this.Amount = a; // store final amount in Amount
}
public void Display(){
cout << "Name " << this.Cname
<< " Pnumber " << this.Pnumber
<< " No_of_units " << this.No_of_units
<< " Amount " << this.Amount;
}
float Calc_amount (int nou){
int n = nou;
float a = 0.0f;
if(n > 350){a += (n - 350) * 1.2; n = 350;} // get cost for units 350+
if(n > 150){a += (n - 150) * 1.0; n = 150;} // get cost for units 150-350
if(n > 50){a += (n - 50) * 0.8; n = 50;} // get cost for units 50-150
this.Amount = a; // store final amount in Amount
}
}