0% found this document useful (0 votes)
4 views4 pages

Exp 10

The document describes the implementation of a TollBooth class in C++ that tracks the number of cars and the total money collected from paying cars. It includes methods for handling both paying and non-paying cars, as well as a main function that interacts with the user to record car entries. The flowchart illustrates the process of initializing the class, receiving user input, and displaying the totals upon quitting.

Uploaded by

garisharmaa13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Exp 10

The document describes the implementation of a TollBooth class in C++ that tracks the number of cars and the total money collected from paying cars. It includes methods for handling both paying and non-paying cars, as well as a main function that interacts with the user to record car entries. The flowchart illustrates the process of initializing the class, receiving user input, and displaying the totals upon quitting.

Uploaded by

garisharmaa13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment No.

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:

TollBooth() : totalCars(0), totalAmount(0.0) {}

void payingCar() {
totalCars++;
totalAmount += 0.50;
}

void nopayCar() {
totalCars++;
}

unsigned int getTotalCars() const {


return totalCars;

Garima Sharma
22001001032
}

double getTotalAmount() const {


return totalAmount;
}
};
int main() {
TollBooth booth;
char choice;

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";
}

} while (choice != 'q');


std::cout << "Total cars: " << booth.getTotalCars() << std::endl;
std::cout << "Total amount collected: $" << booth.getTotalAmount() << std::endl;

return 0;
}

Garima Sharma
22001001032
Output:

Garima Sharma
22001001032
Experiment – 10
Flowchart

Start

Initialise class
TollBooth.Set
totalCars = 0,
totalAmount = 0.0

Enter 'p' for a


paying car or 'n'
for a non-paying
car (or 'q' to quit)

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

true Display total cars and


Input
Total amount End
== q
collected.

false
Print
"Invalid
Garima Sharma choice."
22001001032

You might also like