In: Computer Science
code for the above problem given below:
#include<iostream>
using namespace std;
class tollBooth
{
private: // data items declared as required
unsigned int car;
double money;
public:
tollBooth(): car(0), money(0) {} // constructor calling to initialize car and money to 0
void payingCar() // payingCar add 0.50 to money and add 1 to car inorder to count the cars passes by
{
car=car+1;
money=money+0.50;
}
void nopayCar() // car passes but do not pay . therefore no money added
{
car=car+1;
money=money+0;
}
void display()
{
cout<<"\n Total Money Collected On TollBooth = "<<money<<endl;
cout<<" Total cars On TollBooth = "<<car<<endl;
}
};
int main()
{
char n;
tollBooth d;
cout<<"Enter 1 For Toll Paying Cars and 2 For Non-Toll Paying Cars = ";
cin>>n;
switch (n) // if entered 1 than payingCar information display else nopaying car info display
{
case '1':
d.payingCar(); // call to member functoin of tollBooth class
d.display();
break;
case '2':
d.nopayCar();
d.display();
break;
}
return 0;
}
sample output: