Model this tollbooth with a class called tollBooth. The two data items are a type unsigned int to hold the total number of cars, and a type double to hold the total amount of money collected. A constructor initializes both of these to 0.
A member function called payingCar() increments the car total and adds 0.50 to the cash total.
Another function, called nopayCar(), increments the car total but adds nothing to the cash total.
Finally, a member function called display() displays the two totals. Make appropriate member functions const.
Include a program to test this class. This program should allow the user to push one key to count a paying car, and another to count a nonpaying car. Pushing the Esc key should cause the program to print out the total cars and total cash and then exit.
#include <iostream>
using namespace std;
int getche();
class Tollbooth
{
public:
Tollbooth (int = 0, double = 0); // constructor
void payingCar (); // Function to increment the car total and cash total by 0.50
void nopayCar(); //Function to increment the car total, but not cash total.
void display(); //Function to display the two member variables
private:
int cartotal;
double cash;
};
void Tollbooth::payingCar()
{
cartotal = 1 + cartotal;
cash = .50 + cash;
return;
}
void Tollbooth::nopayCar()
{
cartotal = 1 + cartotal;
return;
}
void Tollbooth::display()
{
cout << "Car Total =" << cartotal << endl;
cout << "Cash Total =" << cash << endl;
return;
}
int main()
{
Tollbooth toll;
char ch;
cout<< "press 0 for non-paying cars press 1 for paying cars press Esc to exit program"; // ASCI for ESC is 27;
do
{
ch=getche();
if(ch=='0')
toll.payingCar();
if(ch=='1')
toll.nopayCar();
} while(ch != ESC);
toll.display();
return 0;
}
int getche()
{
int num;
cin >> num;
return num;
}
MOD EDIT: Added text from "Gimme The Codez" original post.
This post has been edited by JackOfAllTrades: 04 October 2012 - 06:06 AM
Reason for edit:: added [code][/code] tags - learn to use them yourself

New Topic/Question
Reply



MultiQuote





|