Exp 10
Exp 10
10
Aim: Imagine a tollbooth with a class called TollBooth. The two data
items of 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.
Source Code:
#include <iostream>
class TollBooth {
private:
unsigned int totalCars;
double totalAmount;
public:
void payingCar() {
totalCars++;
totalAmount += 0.50;
}
void nopayCar() {
totalCars++;
}
Garima Sharma
22001001032
}
do {
std::cout << "Enter 'p' for a paying car or 'n' for a non-paying car (or 'q' to quit): ";
std::cin >> choice;
if (choice == 'p') {
booth.payingCar();
std::cout << "A paying car has passed.\n";
} else if (choice == 'n') {
booth.nopayCar();
std::cout << "A non-paying car has passed.\n";
} else if (choice != 'q') {
std::cout << "Invalid input. Please try again.\n";
}
return 0;
}
Garima Sharma
22001001032
Output:
Garima Sharma
22001001032
Experiment – 10
Flowchart
Start
Initialise class
TollBooth.Set
totalCars = 0,
totalAmount = 0.0
true Increment
Call Print "A
Input totalCars by 1 and paying car
booth.payingCar()
== p totalAmount by has passed."
0.50
false
true Call
Input Increment Print "A non-
booth.nopayCar() totalCars by 1 paying car
== n
has passed."
false
false
Print
"Invalid
Garima Sharma choice."
22001001032