Saturday 12 August 2017

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 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 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.



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 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 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.
 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 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.

SOLUTION:

#include "stdafx.h"
#include "iostream"
using namespace std;
/////////////////////////////////////////////////////////////////////////////////////////class tollboth
{
private:
                        unsigned int t_cars;
                        double t_money;
public:
                        tollboth( ) : t_cars(0), t_money(0)
{ /*Empty Body*/ }
                        void payingCar( )
                        {
                                    t_cars++;
                                    t_money+=0.50;
                        }
                        void nopayCar( )
                        {
                                    t_cars++;
}
                        void display( ) const
                        {
                                    cout << "\nTotal Cars Passed by = " << t_cars << endl;
                                    cout << "Total Money Collected = " << t_money << endl;
                        }
};
/////////////////////////////////////////////////////////////////////////////////////////void main( )
{
tollboth booth;
char ch;
cout << "Press ‘p’ for a Paying Car" << endl;
cout << "Press ‘n’ for a Non-Paying Car" << endl;
cout << "Press ‘ESC’ Key to see Total Cars Passed by & Total Money Collected" << endl;
                        ch=getche( );
        a: if(ch!=27)
            {
                        switch(ch)
                        {
                                    case 'p':           booth.payingCar( );     break;
                                    case 'n':           booth.nopayCar( );      break;
                                    default:
                                                cout << "\nPress 'p' for a Paying Car" << endl;
                                                cout << "Press  'n' for a Non-Paying Car" << endl;
cout << "Press ‘ESC’ Key to see Total Cars Passed by & Total Money Collected"; cout << endl;
                                                break;
                        }
                        ch=getche( );
                        goto a;
            }
booth.display( );
system("pause");
}

OUTPUT:



0 comments: