Comments

Wednesday, November 21, 2012

OOP Toll both Add number of cars play program until user press no

Posted by at 9:32 PM Read our previous post
Object 
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.


Source code:


#include <iostream>
using namespace std;

class tollboth
{ private:
       int null,pcar,wpcar;
       float tax;
public:
       tollboth()
       {
              tax=0;
              pcar=0;
              wpcar=0;
              null=0;
       }
       void paycar(int a)
       {
       /*pcar=a;*/
       pcar=pcar+a;
       for(null;null<=pcar;null++)
       {
              tax=tax+0.50;
       }
       }
       void withoutpay(int b)
       {
       wpcar=wpcar+b;
       }
       void showdata()
       {
       cout<<"Total no of payed cars are : "<<pcar<<endl;
       cout<<"Total tax is : "<<tax<<endl;
       cout<<"Total no of not payed cars are : "<<wpcar<<endl;
    
       }
};
int main()
{
       tollboth tb;
       char press,input;
       int a,b;
 do{
    
       cout<<"Press 1 for car pay tax"<<endl;
       cout<<"Press 2 for car not pay tax"<<endl;
       cout<<"Press 3 for total tax and Exit"<<endl;
       cin>>press;

       switch(press)
       {
       case '1':
              {
                     cout<<"Enter No of the cars pay tax"<<endl;
                     cin>>a;
                     tb.paycar(a);
                     break;
              }
       case '2':
              {
                     cout<<"Enter No of cars not pay tax"<<endl;
                     cin>>b;
                     tb.withoutpay(b);
                     break;
              }    
       }
       cout<<"Press y to continue and n for terminate"<<endl;
       cin>>input;
       }
       while(input=='y');
       tb.showdata();
    system("pause");
    return 0;
}
Output:

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Popular Posts

Labels

© Codepirate is powered by Blogger - Template designed by Stramaxon - Best SEO Template