Program 9 – TollBooth Class

Imagine a tollbooth with a class called tollbooth. The two data items are: a toe 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 these to 0. A member function called paying car() increments the car total and adds 0.50 to the cash total. Another function, called nonpay car(), increments the car total but adds nothing to the cash total. Finally a member function called displays the two totals.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 non-paying car. Pushing the ESC key should cause the program to print out the total cars and the total cash and then exit.null

#include<iostream>
using namespace std;


class tollbooth
{
    unsigned int car;
    double amt;
    public:
        tollbooth()
        {
            this->car = 0;
            this->amt = 0;
        }
        void payingcar()
        {
            this->car++;
            this->amt+=0.50;
        }
        void nonpayingcar()
        {
            this->car++;
        }
        void display()
        {
            cout<<"Number of cars: "<<car<<endl;
            cout<<"Amount: "<<amt<<endl;
        }
};

int main()
{
    char c='y';
    int ch;
    tollbooth t;
    do{
    cout<<" 1 for paying \n 2 for nopaying \n 3 Display/Exit \n";
    cout<<"Enter choice \n";
    cin>>ch;
    switch(ch)
    {
    case 1:
    t.payingcar();
    cout<<"car added";
    break;
    case 2:
    t.nonpayingcar();
    cout<<"car added";
    break;
    case 3:
    t.display();
    c='n';
    break;
    
    }
   // t.display();
   if(c=='y'||c=='Y'){
    cout<<"\nDo you want to continue";
    cin>>c;
   }
    }
    while(c=='y'||c=='Y');
    return 0;
}

LEAVE A REPLY