CFanatic

Go Back   CFanatic > Programming > C Programming

Join CFanatic Forum Now

[ask]Static Function in Base Class? topic posted under C Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 12-18-2008, 11:31 PM
Junior Member
 
Join Date: Dec 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
bluerose is on a distinguished road
| More
[ask]Static Function in Base Class?

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
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 2:
                 dino[ctr++]=new Velociraptor();
                 break;                 
            case 3:
                 dino[ctr++]=new Pterodactyl();
                 break;
            case 4:
                 dino[ctr++]=new Plesiosaur();
                 break;
            case 5:                 
                 dino[ctr++]=new Brachiosaurus();
                 break;                 
            case 6:
                 dino[ctr++]=new Triceratop;
                 break;
            case 7:                 
                 dino[ctr++]=new Mammoth();
                 break;                 
            case 8:
                 dino[ctr++]=new Stegosaurus();
                 break;                 
            case 9:
                 T_Rex::statistics();
                 Velociraptor::statistics();
                 Pterodactyl::statistics();
                 Plesiosaur::statistics();
                 Brachiosaurus::statistics();
                 Triceratop::statistics();
                 Mammoth::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;
}
Thank u for the help in advance :-)
Reply With Quote
  #2  
Old 01-18-2009, 02:34 AM
Junior Member
 
Join Date: Jan 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
crazy_awper is on a distinguished road
| More
Re: [ask]Static Function in Base Class?

I agree with you,I think you are right,good luck to you guy!
Reply With Quote
  #3  
Old 01-19-2009, 12:14 AM
Junior Member
 
Join Date: Dec 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
bluerose is on a distinguished road
| More
Re: [ask]Static Function in Base Class?

I don't think that answer is relevant to my question. I think you mis post the reply :-)
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
get Class johny10151981 Win32 Programming 1 10-07-2008 03:58 AM
Displaying Icons in Static controls Spogliani Win32 Programming 1 06-24-2008 11:03 PM
linking errors with DLL linked to static libraries mijal C++ Programming 3 08-23-2007 10:22 PM
changing variable types of a function of a derived class mrclash C# Programming 0 09-20-2006 04:56 AM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 01:12 AM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board