>3.2 Toollbooth at a bridge programme
>
/*imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a five-rupee 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 (say TollBooth_. The two data members may be a type of usnsigned int (to hold the number of cars) and a type double to hold the amount of money collected. A constructor may initialize both these to 0. A member function (say payingCar() increments the car total and adds 5 to the cash total, while another member function (say noPayCar()) increments the car total but adds nothing to the cash total. Finally a member function display () displays the two total.
[Hint: Allow user to press a particular key to indicate a paying car and another key to indicate non paying car]
Programmed by: Jitendra Yadav
Roll: 10116
Date: 27 July 2011*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class Tollbooth
{
int count;
double amt;
public:
Tollbooth()
{
count=0;
amt=0;
}
void payingcar()
{
amt=amt+5;
count++;
}
void nopaycar()
{
count++;
}
void display()
{
cout<<“No of car:”<<count<<endl;
cout<<“Sum collected:”<<amt<<endl;
}
};
int main()
{
int x;
Tollbooth t;
while(1)
{
cout<<“1.for paying car”<<endl<<“2. for non paying car”<<endl;
cout<<“3.for display”<<endl<<“4. for exit”<<endl;
cin>>x;
switch(x)
{
case 1:
t.payingcar();break;
case 2:
t.nopaycar();break;
case 3:
t.display();break;
case 4:
exit(0);
default:
cout<<“wrong entry”<<endl;
}
}
}
Output Sample:
1.for paying car
2. for non paying car
3. for display
4. for exit
1
1. For paying car
2. For non paying car
3. For display
4. For exit
3
No of cars: 1
Sum collected: 5
1. For paying car
2. For non paying car
3. For display
4. For exit