[ask] Static Function in Base Class

Discussion in 'C++' started by bluerose, Jan 2, 2009.

  1. bluerose

    bluerose New Member

    Joined:
    Dec 10, 2008
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    I have a base class of Dinosaur that has 2 other child class (Carnivore and Herbivore). I tried to calculate the total price of all ticket sold for Carnivore, Herbivore and Dinosaur but I dun have any idea how to make it. I can make it for the child class of T-Rex until Stegosaurus. But i duno how to make it for the Base classes. Does anybody in here could help me pls? This is the code. Realy expecting ur help.

    Code:
    #include<iostream>
    using namespace std;
    class Dinosaur
    {
          protected:
                    int weight;
                    static int total_sold;
                    static double total_ticket;
          public:
                 Dinosaur()
                 {
                      total_sold++;
                 }
                 virtual double rate()
                 {
                         return (weight*0.07);
                 }
                 static void statistics()
                 {
                        cout << "Total Ticket Sold For All Dinosaurs         : " << total_sold
                             << " -----> Total : $ " << total_ticket << endl << endl;
                 }
                 virtual void voice()
                 {
                 }
    };
    
    class Carnivore:public Dinosaur
    {
          protected:
                    static int total_carnivore_sold;
                    static double total_carnivore_ticket;
          public:
                 Carnivore()
                 {
                       total_carnivore_sold++;
                 }
                 void voice(char *v)
                 {
                     cout << "I am carnivorous...";
                     cout << v << endl;
                     cout << "I weigh : " << weight << " kgs" << endl;
                     cout << "My ticket price is : ";                 
                 }
                 double rate()
                 {
                       return Dinosaur::rate()*1.25;
                       
                 }
                 static void statistics()
                 {
                        cout << "Total Ticket Sold For Carnivorous Dinosaur : " << total_carnivore_sold
                             << " -----> Total : $ " << total_carnivore_ticket << endl << endl;
                 }
    };
    
    class Herbivore:public Dinosaur
    {
          protected:
                    static int total_herbivore_sold;
                    static double total_herbivore_ticket;
          public:
                 Herbivore()
                 {
                     total_herbivore_sold++;
                     total_herbivore_ticket+=Herbivore::rate();
                 }
                 void voice(char *v)
                 {
                      cout << "I am herbivorous...";
                      cout << v << endl;
                      cout << "I weigh : " << weight << " kgs" << endl;
                      cout << "My ticket price is : ";                  
                 }
                 double rate()
                 {
                       return Dinosaur::rate();
                 }
                 static void statistics()
                 {
                        cout << "Total Ticket Sold For Herbivorous Dinosaur : " << total_herbivore_sold
                             << " -----> Total : $ " << total_herbivore_ticket << endl << endl;
                 }
    };
    
    class T_Rex:public Carnivore
    {
          protected:
                    static int total_trex_sold;
                    static double total_trex_ticket;
          public:
                 T_Rex()
                 {
                        weight=2000;
                        total_trex_sold++;
                        total_trex_ticket+=Carnivore::rate();
                        Carnivore::voice("T-Rex is the King. Roaaaaaaaaaarrrrrrrrrrrrrrghh");
                        cout << Carnivore::rate();
                 }
                 static void statistics()
                 {
                        cout << "Total Ticket Sold For Tyrannosaurus Rex  : " << total_trex_sold 
                             << " -----> $ " << total_trex_ticket << endl << endl;
                 }
    };
    
    
    
    class Plesiosaur:public Carnivore
    {
          protected:
                    static int total_plesiosaur_sold;
                    static double total_plesiosaur_ticket;
          public:
                 Plesiosaur()
                 {
                     weight=1350;
                     total_plesiosaur_sold++;
                     total_plesiosaur_ticket+=Carnivore::rate();
                     Carnivore::voice("Plesiosaur...here to please u :-)");
                     cout << Carnivore::rate();               
                 }
                 static void statistics()
                 {
                        cout << "Total Ticket Sold For Plesiosaur         : " << total_plesiosaur_sold
                             << " -----> $ " << total_plesiosaur_ticket << endl << endl;
                 }
    };
    
    class Brachiosaurus:public Herbivore
    {
          protected:
                    static int total_brachiosaurus_sold;
                    static double total_brachiosaurus_ticket;
          public:
                 Brachiosaurus()
                 {
                     weight=3500;
                     total_brachiosaurus_sold++;
                     total_brachiosaurus_ticket+=Herbivore::rate();
                     Herbivore::voice("Brachiosaurus...nyam nyam nyam");
                     cout << Herbivore::rate();               
                 }
                 static void statistics()
                 {
                        cout << "Total Ticket Sold For Brachiosaurus      : " << total_brachiosaurus_sold
                             << " -----> $ " << total_brachiosaurus_ticket << endl << endl;
                 }
    };
    
    
    
    class Stegosaurus:public Herbivore
    {
          protected:
                    static int total_stegosaurus_sold;
                    static double total_stegosaurus_ticket;
          public:
                 Stegosaurus()
                 {
                     weight=2100;
                     total_stegosaurus_sold++;
                     total_stegosaurus_ticket+=Herbivore::rate();
                     Herbivore::voice("Stegosaurus....cool man!" );
                     cout << Herbivore::rate();               
                 }
                 static void statistics()
                 {
                        cout << "Total Ticket Sold For Stegosaurus        : " << total_stegosaurus_sold
                             << " -----> $ " << total_stegosaurus_ticket << endl << endl;
                 }
    };
    
    int Dinosaur::total_sold=0;
    int Carnivore::total_carnivore_sold=0;
    int Herbivore::total_herbivore_sold=0;
    int T_Rex::total_trex_sold=0;
    int Plesiosaur::total_plesiosaur_sold=0;
    int Brachiosaurus::total_brachiosaurus_sold=0;
    int Stegosaurus::total_stegosaurus_sold=0;
    
    double Dinosaur::total_ticket=0;
    double Carnivore::total_carnivore_ticket=0;
    double Herbivore::total_herbivore_ticket=0;
    double T_Rex::total_trex_ticket=0;
    double Plesiosaur::total_plesiosaur_ticket=0;
    double Brachiosaurus::total_brachiosaurus_ticket=0;
    double Stegosaurus::total_stegosaurus_ticket=0;
    
    int main()
    {
        //Carnivore *karnivora[100];
        //Herbivore *herbivora[100];
        Dinosaur *dino[100];
        int n, choice;
        int ctr=0;
        do
        {
            cout << endl;
            cout << "-----------------------------------------------------------------------------" << endl;
            cout << "JURASSIC PARK TICKETING SIMULATION" << endl
                 << "1. Tyrannosaurus Rex" << endl
                 //<< "2. Velociraptor" << endl
                 //<< "3. Pterodactyl" << endl
                 << "4. Plesiosaur" << endl
                 << "5. Brachiosaurus" << endl
                 //<< "6. Triceratop" << endl
                 //<< "7. Mammoth" << endl
                 << "8. Stegosaurus" << endl
                 << "9. Managerial Report" << endl
                 << "0. Exit" << endl;
            cout << "-------------------------------------------------------------------------------" << endl;             
            cout << "Enter your choice : " ; cin >> choice;
    
    
            switch(choice)
            {
            if (ctr <= 100) 
            {
                case 1: 
                     dino[ctr++]=new T_Rex();
                     break;
                case 4:
                     dino[ctr++]=new Plesiosaur();
                     break;
                case 5:                 
                     dino[ctr++]=new Brachiosaurus();
                     break;                             
                case 8:
                     dino[ctr++]=new Stegosaurus();
                     break;                 
                case 9:
                     T_Rex::statistics();
                     Plesiosaur::statistics();
                     Brachiosaurus::statistics();
                     Stegosaurus::statistics();
                     Carnivore::statistics();
                     Herbivore::statistics();
                     Dinosaur::statistics();
                     break;
                case 0 : break;
                default: cout << "YOu enter the wrong choice. Pls try again. " << endl; break;
            }
            }
        }while (choice != 0);
        cin >> n;
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice