Practice

Ticket



What is the exact output of the following program?  Show your intermediate work.


 #include <iostream>
 using namespace std;
 #define NTYPES 4

 class Ticket {
     double f;
   public:
     Ticket() {
         cout << "T";
         f = 0.0;
     }
     Ticket(double b) {
         f = b;
     }
     ~Ticket() {
         cout << "D" << '|' << f << endl;
     }
     double fare() const { return f; }
 };

 class Collector {
     int no;
     double amount;
   public:
     Collector() {
         no = 0;
         amount = 0.0;
     }
     ~Collector() {
         cout << "Collected " << amount << endl;
     }
     void accept(const Ticket& t) {
         no++;
         amount += t.fare();
     }
     void display() const {
         cout << no << ',' << amount << endl;
     }
 };

 int main() {
     Collector collector;
     Ticket* t;
     double fare[] = { 2.75, 2.25, 1.40};

     t = new Ticket[NTYPES];

     for (int i = 0; i < 3; i++) {
         Ticket tt(fare[i]);
         t[i] = tt;
     }

     collector.accept(t[2]);
     collector.display();
     collector.accept(t[0]);
     collector.display();
     collector.accept(t[1]);
     collector.display();

     delete [] t;

     return 0;
 }

Show your output here:























Show your rough work on a separate page.


   Printer Friendly Version of this Page print this page     Top  Go Back to the Top of this Page



  Designed by Chris Szalwinski   Copying From This Site