Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a
ID: 3816803 • Letter: I
Question
Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a 50 cent toll. Mostly they do. but sometimes a car goes by without paying. The tollbooth keeps track of the number of cars that is gone by. and of the total amount of money collected. Model this tollbooth with a class called tollbooth. The two data member items are type int to hold the total number of cars, and type double to hold the total amount of money collected. A constructor initializes both of these to 0. A member function called payinCar () 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 (ostream& fileout) to displays the two totals to the screen. (Look at BankAccount class - pg 571-573) Make the stream parameters versatile (pg 346 - 347) Include a main function to test this class. The program should allow the user to enter 'p' to count a paying car. and 'n' to count a nonpaying car and 'q' should cause the program to print out the total number of cars and total amount collected. Save this program as label 2b. cc Sample output of this program can be as follows (user input in italics): Submit the programs electronically as labl2. Also print your answer sheet. Don't forget to do the documentation.Explanation / Answer
Answer:
TollBooth programme:
A tollbooth at a bridge. Cars passing by the booth are expected to pay a 50 pesewas toll.Mostly they do,but sometimes a car goes by without paying.The tollbooth keeps track of the number of cars that have gone by, and of the total amount of money collected.
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 f money collected. A constructor initializes both of these to 0.
Amember function called payingCar() increments the car total and adds 0.50 to the cash total.
Anther function,called nopayCar(),increments the car total but adds nothing to the cash total.
Finally,a member function called display() display the two totals.Make appropriate member functions const.
include a programe to test this class.This program should allow the user to push ne key to count a paying car, and another to count a nonpaying car. pushing the Q key should cause the program to print ut the total cars and ttal cash and then exit.
#include<iostream>
using namespace std;
int getche();
class Tollbooth
{
public:
Tollbooth(int=0,double=0);//constructor
void payinhCar(); //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;
duble cash;
};
void Tollbooth::payingCar()
{
cartotal=1+cartotal;
cash=.50+cash;
return;
}
void Tollbooth::nopayCar()
{
cartotal=1+cartotal;
return;
}
void Tollbooth::display()
{
cout<<"Car Total="carttal<<endl;
cout<<"Car Ttal="<<cash<<endl;
return;
}
int main()
{
Tollbooth toll;
char ch;
cout<<"press N for non-payingcars press P for paying cars press Q to exit programe";
do
{
ch=getche();
if(ch=='P')
toll.payingCar();
if(ch=='N')
toll.nopayCar();
}
while(ch!=Q);
toll.display();
return 0;
}
int getche()
{
char alpha;
cin>>alpha;
return alpha;
}