I have an assignment where I need to write a program that calculates and prints the parking charges for each of four customers who parked their cars in this garage yesterday. Your program should print the results in a neat tabular format. It should also calculate and print the total of yesterday's
receipts. I've done that part.
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
using std::ios;
#include<iomanip>
using std::setw;
using std::setprecision;
using std::setiosflags;
#include<cmath>
double calculateCharges( double );
int main()
{
double hour, currentCharge, totalCharges = 0.0, totalHours = 0.0;
int first = 1;
cout << "Enter the hours parked for 4 cars: ";
for ( int i = 1; i <= 4; i++ ) {
cin >> hour;
totalHours += hour;
if ( first ) {
cout << setw( 5 ) << "Car" << setw( 15 ) << "Hours"
<< setw( 15 ) << "Charge\n";
first = 0; // prevents this from printing again
}
totalCharges += ( currentCharge = calculateCharges( hour ) );
cout << setiosflags( ios::fixed | ios::showpoint )
<< setw( 3 ) << i << setw( 17 ) << setprecision( 1 ) << hour
<< setw( 15 ) << setprecision( 2 ) << currentCharge << "\n";
}
cout << setw( 7 ) << "TOTAL" << setw( 13 ) << setprecision( 1 )
<< totalHours << setw( 15 ) << setprecision( 2 )
<< totalCharges << endl;
return 0;
}
double calculateCharges( double hours )
{
double charge;
if ( hours < 3.0 )
charge = 2.0;
elseif ( hours < 19.0 )
charge = 2.0 + .5 * ceil( hours - 3.0 );
else
charge = 10.0;
return charge;
}
But the assignment says that I need to use classes, and classes don't really make sense to me. I don't really understand why i need to use them, they just seem like extra work to me. Create a class called 'parking' that includes two pieces of information as private data members hour(type float), charge(type float).Your class should have a constructor that initializes all data members to zero.Provide a set_hour() function to initialize the data member 'hour' with the customer specified time duration of parking.Provide get_hour() and get_charge() functions. These function will just return the parking hour and associate charge. They will not do any other calculation or initialization.
Provide a member function calculate_charge() that calculates the total charge for parking a car in that garage.
Where you declare hour and charge now make them private members of a class called customer or something instead. Call your charge function get_charge() and make it a private function of the customer class etc...