Saturday, 23 March 2019

CPP-EX58

Imagine a tollbooth at a bridge. A Car passing by the booth is expected to pay a toll. The tollbooth keeps the track of the number of cars  that gone by and the total amount of cash collected.
Create a class tollbooth with the data members as:--total number of cars passed.    -total toll collected.
Write necessary member functions:
    1. a constructors that initializes both data members to zero.
    2. paying car(): when any car passes through  the tollbooth, that much toll gets added
        into  total toll collected and total number of cars passed is incremented by one.
    3. nonpaying car(): increments the car  total but adds nothing to cash total.
    4. display (): displays total no. of cars  passed and the total amount collected.


#include <iostream.h>
#include <iomanip.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

class Tollbooth
{
    public:

    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

    int cartotal;
    float cash;
    Tollbooth() // constructor
    {
       cartotal=0;
       cash=0;
    }
};
    void Tollbooth::payingCar()
    {
        cartotal = 1 + cartotal;
        cash = 5.0 + cash;
        return;
    }

    void Tollbooth::nopayCar()
    {
        cartotal = 1 + cartotal;
        return;
    }

    void Tollbooth::display()
    {
        cout << "\n Car Total =" << cartotal << endl;
        cout << "\n Cash Total =" << cash << endl;
        return;
    }

    int main ( )
    {
        Tollbooth toll;
        char ch;
        int choice;
        char isContinue;
        clrscr();
        cout<< "\n press 1 for non-paying cars /
 press 2 for paying cars/
 press 3 for display";
        cout<<"\n\t\t4. press 4 to exit program";

        do{

            cout<<"\n\nEnter your choice: ";
            cin>>choice;
            switch(choice)
            {
                case 1:
                    toll.nopayCar();
                    break;
                case 2:
                    toll.payingCar();
                    break;
                case 3:
                    toll.display();
                    break;
                case 4:
                    exit(0);
            default:
                cout<<"You entered wrong choice!";
        }
        cout<<"\nDo you want to continue?[y/n]: ";
        cin>>isContinue;
        }while(isContinue=='y' || isContinue=='Y');

        return 0;
    }