Consider a toll plaza on a bridge. Cars passing through the toll plaza must pay a toll of Rs.30/-. Most of the time, they do, but every now and then, a car drives by without paying. The toll plaza records the number of cars that have passed through as well as the total amount of money collected. Model this toll-plaza with the help of the toll-plaza class. The classs two data items are to hold the total number of cars and to hold the total amount of money collected. For the c++ class, implement:
A constructor that initializes both data items to 0.
A member function called payingCar() increments the car total and adds 30 to the cash total.
Another function, called nopayCar() that increments the car total but adds nothing to the
cash total.
Finally, a member function called display() should display the total cars and the total cash collected.
#include <iostream>
using namespace std;
class TollPlaza {
int cars;
int money;
public:
TollPlaza() : cars(0), money(0) {}
void payingCar() {
cars++;
money += 30;
}
void nonpayCar() {
cars++;
}
void display() {
cout << "Total cars: " << cars << endl;
cout << "Cash: Rs." << money << "/-" << endl;
}
};
int main() {
TollPlaza plaza;
plaza.payingCar();
plaza.payingCar();
plaza.nonpayCar();
plaza.payingCar();
plaza.display();
return 0;
}
Comments
Leave a comment