] > Programming classes

6.3.2 Programming classes

Exercise. Fix the following program and provide the missing code.

int main () 
{ 
   pet dog( ”Dog” ), cat( ”Cat” ); 
   food cookie( ”cookies” ), 
        chocolate( ”chocolate”, 7 ); 
   person dan, danna; 
 
   dan.buy( chocolate, 10 ); 
   dan.buy( cookie, 5 ); 
   danna.buy( chocolate, 2 ); 
 
   dog.get( cookie ); 
   dog.eat(); 
 
   cat.get( chocolate ); 
   cat.eat(); 
   cat.eat(); 
 
   cookie.show(); 
   chocolate.show(); 
 
   dog.show(); 
   cat.show(); 
 
   person::show(); 
 
   return 0; 
}
4 servings of cookies were left
17 servings of chocolate were left
Dog ate 1 servings
Cat ate 2 servings
2 people took care of feeding the pets
int main () 
{ 
   pet dog(”Dog”), cat(”Cat”); 
   food cookie( ”cookies” ), 
        chocolate( ”chocolate”, 7); 
   person dan, danna; 
 
   dan.buy(&chocolate, 10); 
   dan.buy(&cookie, 5); 
   danna.buy(&chocolate, 2); 
 
   dog.get( &cookie ); 
   dog.eat(); 
 
   cat.get( &chocolate ); 
   cat.eat(); 
   cat.eat(); 
 
   cookie.show(); 
   chocolate.show(); 
 
   dog.show(); 
   cat.show(); 
 
   person::show(); 
 
   return 0; 
}
class food { 
     int count; 
     string name; 
   public
     food( string ); 
     food( string, int ); 
     void add( int ); 
     void show(); 
}
class pet { 
     food  jar; 
     int count; 
     string name; 
   public
     pet( string ); 
     void get(food ); 
     void eat(); 
     void show(); 
}
class person { 
     static int count; 
   public
     person(); 
     void buy(food  , int); 
     static void show(); 
};
          +---------------------+  
          |       +-----+       |  
          |  food |     |       |  
      dog |       +-----+ *jar  |  
          |       +-----+       |  
          |  name |     |       |  
          |       +-----+ string|  
          |       +-----+       |  
          | count |     |       |  
          |       +-----+ int   |  
          +---------------------+ pet  
 
          +---------------------+  
          |       +-----+       |  
          |  food |     |       |  
      cat |       +-----+ *jar  |  
          |       +-----+       |  
          |  name |     |       |  
          |       +-----+ string|  
          |       +-----+       |  
          | count |     |       |  
          |       +-----+ int   |  
          +---------------------+ pet  
 
          +---------------------+  
          |       +-----+       |  
   cookie |  name |     |       |  
          |       +-----+ string|  
          |       +-----+       |  
          | count |     |       |  
          |       +-----+ int   |  
          +---------------------+ food  
 
          +---------------------+  
          |       +-----+       |  
chocolate |  name |     |       |  
          |       +-----+ string|  
          |       +-----+       |  
          | count |     |       |  
          |       +-----+ int   |  
          +---------------------+ food  
 
          +-------------------+  
      dan |                   |  
          +-------------------+ person  
 
          +-------------------+  
    danna |                   |  
          +-------------------+ person  
 
          +------ person -----+  
          |     +------+      |  
          | int |      |      |  
          |     +------+      |  
          +-------------------+  
 
 

[petsfood.cpp] [pets.cpp]