trouble with using classes

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#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;
 else if ( 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.

Can someone help me with this?
You're more then 3/4 done what's the trouble? If you don't know how to make a class then this link will tell you in like 5 mins.

http://www.cplusplus.com/doc/tutorial/classes/

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...
Last edited on
Topic archived. No new replies allowed.