Pages

Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a 50 rupees 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 toll Booth. 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 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. [Updated]

Friday 3 May 2013

#include <iostream.h>
class Tollboth   //class declaretion
{
      //attributes
      int cars;
      double collect;
      public:
             //contstructor
      Tollboth()
      {
           cars=0;
           collect=0;
      }
      //methods
      void payingcar() //paying car method
      {
           cars++;
           collect+=50;
           cout<<"Entry Succesfully added."<<endl;
      }
    
      void nopayingcar() //nonpaying  car method
      {
           cars++;
           cout<<"Entry Succesfully added."<<endl;
      }
    
      void display() //methods to display
      {
           cout<<"Total numbers of cars: "<<cars<<endl;
           cout<<"Total payment: "<<collect<<endl;
      }
};
main()
{
      int ask;
      char ag;
      Tollboth a; // create objects
      again:   //lable
      cout<<"Press 1 if payment is delievered by car owner.\n";
      cout<<"Press 2 if payment is not delievered by car owner.\nPress 3 to display total payment and cars.\nPress 1 or 2 or 3: "<<endl;
      //ask paying cara or nonpaying car.
      cin>>ask;
      if(ask==1)
      {
            a.payingcar();
      }
      else if(ask==2)
      {
            a.nopayingcar();
      }
      else if(ask==3)
      {
           a.display();
      }
      else
      {
           cout<<"Enter Valid Data."<<endl;
           goto again;
      }
      //ask to calculation again.
      cout<<"You like to enter data again? Y/N ";
      cin>>ag;
      if(ag=='Y' || ag=='y')
      {
           goto again; //goto lable again at above.
      }
}
 

Search Box

Most Reading

Contact Form

Name

Email *

Message *