Solutions

Imagine a tollbooth at a bridge | [Solved] | Classes | OOP | C++

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.
Include 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 nonpaying car. Pushing the Esc key should cause the program to print out the total cars and total cash and then exit.
[junkie-alert style=”white”] [/junkie-alert]

Note:

#include
using namespace std;
class TollBooth
{   private:
                unsigned int car;
                double money;
                public:
                                TollBooth(): car(0), money(0) {}
                                void payingCar()
                                {
                                        car=car+1;
                                        money=money+1;
                                }
                                void nopayCar()
                                {              car=car+1;
                                                money=money+0;
                                }
                                void display()
                                {
                                cout<<"\n Total Money Collected On tooBooth = "<<money<<endl;
                                }
                                void get()
                                {
                                      cout<<" Total cars On tooBooth = "<<car<<endl;
                                }
};
 
int main()
{              char x;
                TollBooth d;
                cout<<"Enter 1 For Toll Paying Cars and 2 For Non-Toll Paying Cars = ";
                cout<<"";
cin>>x;
                switch (x)
                {
                                case '1':
                                                d.payingCar();
                                                d.display();
                                                d.get();
                                                break;
                                case '2':
                                                d.nopayCar();
                                                d.display();
                                                d.get();
                                                break;
                }
return 0;
}

 

Output

tollbooth class

About the author

Syed Moin Ali

Hello My Name is Syed Moin Ali and I am the founder of PC Learnings Here on this blog I write about Blogging, SEO, Internet Tricks, Social Networking Site, and Make Money

Add Comment

Click here to post a comment