Answer to Question #178388 in C++ for Zain

Question #178388

Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a 50 cent toll. Mostly they do, but sometimes a car goes by without paying. The tollbooth keeps track of the number of cars that have gone by, and of the total amount of money collected. Model this 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 of 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. Make appropriate member functions const.


1
Expert's answer
2021-04-05T15:38:11-0400
#include <iostream>
using namespace std;

class tollBooth {
private:
    unsigned int carTotal;
    double cashTotal;

public:
    tollBooth();
    ~tollBooth();

    void payingCar();
    void nopayCar();
    void display() const;
};

tollBooth::tollBooth() {
    this->carTotal = 0;
    this->cashTotal = 0;
}

tollBooth::~tollBooth() {}

void tollBooth::payingCar() {
    carTotal += 1;
    cashTotal += 0.5;
}

void tollBooth::nopayCar() {
    carTotal += 1;
}

void tollBooth::display() const {
    cout << "Number of cars passed by: " << carTotal << "\nCash collected: $" << cashTotal << endl;
}

int main() {
    tollBooth mainBridge;

    mainBridge.payingCar();
    mainBridge.payingCar();
    mainBridge.payingCar();
    mainBridge.nopayCar();
    mainBridge.nopayCar();

    mainBridge.display();

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS