Aim :Imagine a tollbooth with a class called tollbooth. The two data items are a type 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 payingCar
() increments the car total and adds 0.50 to the cash total. Another function called
nopayCar (), increments the car total but adds nothing to the cash total. Finally, a member
function called display () displays the two totals i.e., total cars and total cash. Write 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 total cash and then exit
Theory
1. Encapsulation
The class hides its data members (totalCars and totalCash) and provides controlled access via public functions (payingCar(), nopayCar(), display()).
This ensures data security and prevents direct modification of values.
2. Abstraction
The user does not need to know the internal implementation of how cars and cash are counted.
The class provides simple interfaces (payingCar() and nopayCar()) to interact with the system.
3.Constructor
A constructor initializes totalCars and totalCash to 0, ensuring proper setup when the object is created.
Syntax:
class ClassName {
public:
// Default constructor (no parameters)
ClassName() {
// Initialization code (optional)
}
4. Dynamic Behavior (Runtime Execution)
The program continuously accepts user input to increment the car count based on whether the car is paying or not.
The ESC key triggers the display function, showing real-time updates of collected cash and total cars.
5. Message Passing (Interaction via Methods)
Objects interact using member functions like payingCar(), nopayCar(), and display().
Instead of modifying variables directly, function calls handle all interactions with the class.
Program :
Conclusion : Hence we have performed our
practical successfully