c++ - Creating a function that is a friend to multiple classes -


in code below, trying create function "patient_count" friend classes "horse" , "pig" , , "dog". can function friend 1 class not 3. can tell me mistake is?

/*******************************************************\ * veternarian class problem - need class each   * * of 3 animals. horse, pig , dogs                     * \*******************************************************/  #include <cstdlib> #include <iostream> #include <string>  const int horse_kennel = 100; // number of horses can store const int pig_kennel = 100; // number of pigs can store const int dog_kennel = 100; // number of dogs can store  /*******************************************************\ * class horse                                           * *                                                       * * member functions                                      * * horse_count -- keeps track of number of horses    * * add_horse -- sends data object               * * next_horse -- returns data object            * \*******************************************************/  // definition of class class horse {     private:       int horse_count;              // variable keep track of data       std::string horse_data[horse_kennel]; // place put data        // declarations method prototypes    public:                   // initialize        horse( );        // function accepts argument returns nothing       void add_horse(const std::string new_horse_data);        // method returns next horse in queue       std::string next_horse( );        friend int patient_count(horse);  };   /*******************************************************\ * method definition - here flush out prototypes  * * outlined in last section                          * \*******************************************************/ inline horse::horse( ) {    for(int = 0; < horse_kennel; ++i){       horse_data[i] = "empty spot";    }    horse_count = 0; // 0 data count }  /*******************************************************\ * horse::add_horse -- send data object               * \*******************************************************/ inline void horse::add_horse(const std::string new_horse_data) {    horse_data[horse_count] = new_horse_data;    ++horse_count; }  /*******************************************************\ * horse::next_horse - data object              * \*******************************************************/  inline std::string horse::next_horse( ) {    // implementing queue    std::string current_horse = " ";    int target_horse = 0;    for(int = 0;i < horse_kennel; ++i){       if(horse_data[i] != "empty spot"){          std::cout << "horse number " << << " " << horse_data[i] << std::endl;        }    }    std::cout << "select horse want: ";     std::cin >> target_horse;     return (horse_data[target_horse]); }  /*******************************************************\ * class pig                                             * *                                                       * * member functions                                      * * pig_count -- keeps track of number of pigs        * * add_pig -- sends data object                 * * next_pig -- returns data object              * \*******************************************************/  // definition of class class pig {     private:       int pig_count;              // variable keep track of data       std::string pig_data[pig_kennel]; // place put data        // declarations method prototypes    public:                   // initialize        pig( );        // function accepts argument returns nothing       void add_pig(const std::string new_pig_data);        // method returns next pig in queue       std::string next_pig( );        friend pig patient_count(pig); };   /*******************************************************\ * method definition - here flush out prototypes  * * outlined in last section                          * \*******************************************************/ inline pig::pig( ) {    for(int = 0; < pig_kennel; ++i){       pig_data[i] = "empty spot";    }    pig_count = 0; // 0 data count }  /*******************************************************\ * pig::add_pig -- send data object                   * \*******************************************************/ inline void pig::add_pig(const std::string new_pig_data) {    pig_data[pig_count] = new_pig_data;    ++pig_count; }  /*******************************************************\ * pig::next_pig - data object                  * \*******************************************************/  inline std::string pig::next_pig( ) {    // implementing queue    std::string current_pig = " ";    int target_pig = 0;    for(int = 0;i < pig_kennel; ++i){       if(pig_data[i] != "empty spot"){          std::cout << "pig number " << << " " << pig_data[i] << std::endl;        }    }    std::cout << "select pig want: ";     std::cin >> target_pig;     return (pig_data[target_pig]); }  /*******************************************************\ * class dog                                             * *                                                       * * member functions                                      * * dog_count -- keeps track of number of dogs        * * data_to_object -- sends data object          * * data_from_object -- returns data object      * \*******************************************************/  // definition of class class dog {    private:     int dog_count;              // variable keep track of data     std::string dog_data[dog_kennel]; // place put data    // declarations method prototypes   public:                 // initialize      dog( );      // function accepts argument returns nothing     void add_dog(const std::string new_dog_data);      // method returns next dog in queue     std::string next_dog( );    friend dog patient_count(dog); };   /*******************************************************\ * method definition - here flush out prototypes  * * outlined in last section                          * \*******************************************************/ inline dog::dog( ) {    for(int = 0; < dog_kennel; ++i){       dog_data[i] = "empty spot";    }    dog_count = 0; // 0 data count }  /*******************************************************\ * dog::add_dog -- send data object                   * \*******************************************************/ inline void dog::add_dog(const std::string new_dog_data) {    dog_data[dog_count] = new_dog_data;    ++dog_count; }  /*******************************************************\ * dog::next_dog - data object                  * \*******************************************************/  inline std::string dog::next_dog( ) {    // implementing queue    std::string current_dog = " ";    int target_dog = 0;    for(int = 0;i < dog_kennel; ++i){       if(dog_data[i] != "empty spot"){          std::cout << "dog number " << << " " << dog_data[i] << std::endl;        }    }    std::cout << "select dog want: ";     std::cin >> target_dog;     return (dog_data[target_dog]); }  /**************************************************\ * function friend of animal      * * classes , returns total of animals     * * problem ******* problem *********problem ********* * when add other 2 classes on next line  * * program stops working                        * \**************************************************/ // int patient_count(horse target_horse) //works int patient_count(horse target_horse, pig target_pig, dog target_dog) // nova {    //  int all_animals = target_horse.horse_count; //works    int all_animals = target_horse.horse_count + target_pig.pig_count + target_dog.dog_count; // nova     return (all_animals); } /**************************************************\ * class defined above, section    * * small testing harness verify class   * * doing designed              * \**************************************************/ int main( ) {    int total_animals;    horse current_horse; // create instance     // send 3 values object    current_horse.add_horse("mr ed, 10, male");    current_horse.add_horse("lightning, 4, female");    current_horse.add_horse("blitz, 7, male");     // call return of 3 values    std::cout << "selected horse  ->" << current_horse.next_horse( ) << '\n';     pig current_pig; // create instance     // send 3 values object    current_pig.add_pig("arnold, 4, male");    current_pig.add_pig("babe, 2, female");    current_pig.add_pig("killer, 7, male");     // call return of 3 values    std::cout << "selected pig  ->" << current_pig.next_pig( ) << '\n';     dog current_dog; // create instance     // send 3 values object    current_dog.add_dog("misty, 15, female");    current_dog.add_dog("tristian, 12, male");    current_dog.add_dog("tempest, 11, female");     // call return of 3 values    std::cout << "selected dog  ->" << current_dog.next_dog( ) << '\n';      // results friend function    //  total_animals = patient_count(current_horse); // works    total_animals = patient_count(current_horse, current_pig, current_dog); // nova    std::cout << "total animals: " << total_animals << std::endl;    return (0); } 

hmm...i think there's lot easier way handle this:

class animal {      static int count;      animal() { ++count; }     ~animal() { --count; } };  class horse : public animal {      // horse stuff };  class pig : public animal {      // pig stuff here };  class dog : public animal {      // dog stuff here };  int patient_count() { return animal::count; } 

other that, code seems have basic problem: it's confusing (for example) animal collection of animals. have number of things like:

dog current_dog; // create instance  // send 3 values object current_dog.add_dog("misty, 15, female"); current_dog.add_dog("tristian, 12, male"); current_dog.add_dog("tempest, 11, female"); 

this makes no sense. dog should represent that: 1 dog. has 1 name, 1 age, 1 sex, , on. have above 3 dogs, not one. represent them, should have collection of dogs -- preferably standard collection std::vector, if you're not allowed use (which may semi-reasonable, since sounds/seems homework) @ least array.

dog dogs[10];   // array of 10 dogs (none yet initialized though)  dogs[0] = dog("misty, 15, female"); dogs[1] = dog("tristian, 12, male"); dogs[2] = dog("tempest, 11, female"); 

pigs, cows, horses, etc., pretty same: 1 animal object should represent 1 actual animal. collection of animals different thing single animal. note, however, comment above: array of 10 dogs -- 10 dogs (even though none of them has name, age or sex yet, we've defined them officially exist). means patient_count report existence of 10 dogs when define array, regardless of number contain meaningful data. 1 way in std::vector better choice. if like:

std::vector<dog> dogs;  dogs.push_back("misty, 15, female"); dogs.push_back("tristian, 12, male"); dogs.push_back("tempest, 11, female"); 

at point, you've created , stored 3 dogs, if print out patient_count @ point, should show 3 (representing actual dogs created/defined) not 10 (or whatever) represent number of potential animals, while ignoring number contain meaningful data.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -