
October 19th, 2004, 04:39 PM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 24
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
inheritence run time issue
i have a problem. if you comment outthe lion class and compile it runs perfectly fine.
if you compile and run with it, it hangs after saying the zoo is open.
Code:
#include <memory>
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std; // io namespace used
volatile int isopen; // global variable that tells if the park is open
volatile int inpark; // global variable that tells how many are in the park
// people classes -- workers and visitors
class visitor{ // visitors to park
private:
int prk; // are we in the park?
int exhibit; // how many exhibits have we viewed
int wlk; // are we walking?
public:
visitor(): prk(0), exhibit(0), wlk(0){ cout << "\n A zoo visitor arrived."; }; // create the visitor
void walk(){ wlk++; cout << "\n Walking to an exhibit."; return; }; // walk between exhibits
void watch(){ exhibit++; cout << "\n Watching an exhibit."; return; }; // keep track of viewed exhibits
void enter(){ prk++; inpark++; cout << "\n I'm entering the zoo."; return; }; // let user know person entered zoo
~visitor(){ cout << "\n I'm leaving the zoo."; }; // let user know person left zoo; destroy thread
void vrun(){ // class that runs the "person" (thread)
while (exhibit<3){ //
if(prk){ // we're in the park
if(wlk){ wlk--; watch(); return; } // watch some animals
else{ walk(); return; } // walk to an exhibit
}else{ enter(); return; }
}
if(exhibit>=3){ prk--; inpark--; cout << "\n I'm ready to leave the zoo."; return; } // get ready to leave
};
};
class staff{ // zoo staff
public:
staff(){ cout << "\n A staff member has arrived"; } // initiate staffer
void cleanPark(){ cout << "\n Staff is cleaning park"; return; }; // enter work mode
void cleanExhibit(){ cout << "\n Staff is cleaning exhibit"; return; }; // enter work mode
void feedAnimals(){ cout << "\n Staff is feeding animals"; return; }; // enter work mode
~staff(){ cout << "\n Leaving for the night."; }; // deallocate staff
void srun(){ // class that "runs" the staffer
while(isopen){ // while the park is open (use global variable)
switch(rand()%3){ // randomly choose what to do
case '0': cleanPark(); break;
case '1': cleanExhibit(); break;
case '2': feedAnimals(); break;
}
return;
}
};
};
// animal classes -- attractions at the zoo
class animal{ // main animal class
protected:
const string antype; // animal type
public:
animal(){}; // overloaded constructer
animal(string Antype): antype(Antype){ // animal constructor
cout << "\n A(n) " << antype << " is restless."; }; // creates animal
void eat(){ cout << "\n this " << antype << " eating."; return; }; // doing something
void sleep(){ cout << "\n this " << antype << " sleeping."; return; }; // doing something
void play(){ cout << "\n this " << antype << " playing."; return; }; // doing something
~animal(){ cout << "\n " << antype << " is resting.";}; // destructor
void run(){ // function that runs the "animal"
while(isopen){ // while the park is open.... (use global variable)
switch(rand()%3){ // randomly choose what to do
case '0': eat(); break;
case '1': sleep(); break;
case '2': play(); break;
}
}
};
};
class lion: public animal { // lions
public:
lion(): animal("lion"){}; // let user know lion is here
};
/*
class dolphin: public animal{ // dolphins
public:
dolphin(): animal("dolphin"){}; // let user know dolphin is here
};
class monkey: public animal{ // monkeys
public:
monkey(): animal("monkey"){}; // let user know monkey is here
};
class bird: public animal{ // birds
public:
bird(): animal("bird"){}; // let user know bird is here
};
*/
// main zoo class
int main(){ // main class and actual prgram
// main class interacts with user; determines how much of each thing to make (user input)
// planned changes after getting threads working: add arrays for feeding each type of animal
// and cleaning each exhibit. add variable to expand upon animal activities: dolphin shows, etc.
int amtvis,amtstaff,amtl,amtd,amtm,amtb; // numbers to be retrieved from user
inpark=0; // use global variable? how many in park?
isopen=1; // use global variable? park is open
int i,j; // for-loop counter/switch switch
cout << "\n How many people plan to visit the zoo today?\n"; // ask user for number of visitors
cin >> amtvis; // get number of visitors from user
cout << " How many people does the zoo have on staff today?\n"; // get staff; may limit other things in add ons to the program
cin >> amtstaff; // get number of staffers from user
cout << " How many lions does the zoo have?\n"; // how many lions
cin >> amtl; // get number of lions
/* cout << " How many dolphins does the zoo have?\n"; // how many dolphins
cin >> amtd; // get number of dolphins
cout << " How many monkeys does the zoo have?\n"; // how many monkeys
cin >> amtm; // get number of monkeys
cout << " How many birds does the zoo have?\n"; // how many birds
cin >> amtb; // get number of birds
*/
vector<lion*> lions(amtl);
/* vector<dolphin*> dolphins(amtd); vector<monkey*> monkeys(amtm); // animals
vector<bird*> birds(amtb); // more animals
*/ vector<staff*> staffers(amtstaff); vector<visitor*> visitors(amtvis); // people
// create animals, staff and visitors; make them do stuff
for(i=0;i<amtl;i++){ lions[i] = new lion(); } // create each lion
/* for(i=0;i<amtd;i++){ dolphins[i] = new dolphin(); } // create each dolphin
for(i=0;i<amtm;i++){ monkeys[i] = new monkey(); } // create each monkey
for(i=0;i<amtb;i++){ birds[i] = new bird(); } // create each bird
*/ for(i=0;i<amtstaff;i++){ staffers[i] = new staff(); } // create each staff
for(i=0;i<amtvis;i++){ visitors[i] = new visitor(); } // create each visitor
cout << "\n The zoo is now open."; // open the park
visitors[(rand()%amtvis)] -> enter(); // send in the first visitor
while(inpark>0){ // random sayings from park management over a loudspeaker while park is open
switch(rand()%3){ // randomly choose which type to send into action
case 0: visitors[(rand()%amtvis)] -> vrun(); break; // make a visitor do stuff
case 1: staffers[(rand()%amtstaff)] -> srun(); break; // make a staffer do something
case 2: cout << "debug::lion"; lions[(rand()%amtl)] -> run(); break; // make a lion do something
/* case 3: cout << "debug::dolphin"; dolphins[(rand()%amtd)] -> run(); break; // make a dolphin do something
case 4: cout << "debug::monkey"; monkeys[(rand()%amtm)] -> run(); break; // make a monkey do something
case 5: cout << "debug::bird"; birds[(rand()%amtb)] -> run(); break; // make a bird do something
*/ }
}
isopen=0; // close the park (end animal and staff threads)
for(i=0;i<amtvis;i++){ delete visitors[i]; } // remove the visitors
for(i=0;i<amtstaff;i++){ delete staffers[i]; } // deallocate the staff
for(i=0;i<amtl;i++){ delete lions[i]; } // deallocate lions
/* for(i=0;i<amtd;i++){ delete dolphins[i]; } // deallocate dolphins
for(i=0;i<amtb;i++){ delete birds[i]; } // deallocate birds
for(i=0;i<amtm;i++){ delete monkeys[i]; } // deallocate monkeys
*/
cout << "\n Zoo simulation has ended. \n"; // let user know simulation is over
return 0; // successful exit
};
|