OldSchoolCode

.com



Classes Part 2
So, as I mentioned before, classes are the foundation to C++. If you don't learn classes then there is no point to C++. The best way to learn is by doing, I'll start you off with the same basic tutorial given to me. It is very important you are able to complete and fully understand this excercise before you move on - it only gets harder after here! As always, you can ask a question if you get stuck or do not understand part of the lesson.

Imagine you are writing a program which simulates a farm. This farm has animals, a barn, a farmer and food. All these items should be represented as classes. I'm going to take you through writing classes for each of the following:
  1. A building class to represent the barn
  2. A person class to represent the farmer
  3. An animal class to represent the animals on the farm
  4. A food class to represent the food
We are going to pretend these objects are placed on a map. So we're also going to include the X, Y position for each of the objects.

Type these classes up yourself and then write a basic program which creates a few animals, a barn and a farmer. Be sure to have your program call the Speak function in the animal and the person class so we can see their output in your console window.

Building
This should be one of the easier ones. It's a simple class which should hold the color of the building, and the x, y coordinates on the map.
   class Building
   {
   protected:
      char m_color[ 32 ];
      int  m_x;
      int  m_y;
      
   public:
      Building( )
      {
         m_x = 0;
         m_y = 0;
         m_color[ 0 ] = NULL;
      }
      
      void SetPosition( int x, int y )
      {
         m_x = x;
         m_y = y;
      }
      
      void GetPosition( int *pX, int *pY )
      {
         *pX = m_x;
         *pY = m_y;
      }

      void SetColor( const char *pColor )
      {
         strcpy( m_color, pColor, sizeof(m_color) );
      }
      
      void GetColor( char *pColor, int length )
      {
         strcpy( pColor, m_color, length );      
      }
   };

Person
Another simple class which should hold the name of the person, the x, y coordinates on the map, and allows the person to speak.
   class Person
   {
   protected:
      char m_name[ 32 ];
      int  m_x;
      int  m_y;
      
   public:
      Person( )
      {
         m_x = 0;
         m_y = 0;
         m_name[ 0 ] = NULL;
      }
      
      void SetPosition( int x, int y )
      {
         m_x = x;
         m_y = y;
      }
      
      void GetPosition( int *pX, int *pY )
      {
         *pX = m_x;
         *pY = m_y;
      }

      void SetName( const char *pName )
      {
         strcpy( m_name, pName, sizeof(m_name) );
      }
      
      void GetName( char *pName, int length )
      {
         strcpy( pName, m_name, length );      
      }
  
      void Speak( void )
      {
         printf( "The person is speaking!\n" );
      }
   };

Animal
This should hold the name of the animal, a function which lets the animal speak, and the x, y coordinates on the map.
   class Animal
   {
   protected:
      char m_name[ 32 ];
      int  m_x;
      int  m_y;
      
   public:
      Animal( )
      {
         m_x = 0;
         m_y = 0;
         m_name[ 0 ] = NULL;
      }
      
      void SetPosition( int x, int y )
      {
         m_x = x;
         m_y = y;
      }
      
      void GetPosition( int *pX, int *pY )
      {
         *pX = m_x;
         *pY = m_y;
      }

      void SetName( const char *pName )
      {
         strcpy( m_name, pName, sizeof(m_name) );
      }
      
      void GetName( char *pName, int length )
      {
         strcpy( pName, m_name, length );      
      }
      
      void Speak( void )
      {
         printf( "The animal is speaking!\n" );
      }
   };

Food
This should hold the name of the food, the amount of food available and the x, y coordinates on the map.
   class Food
   {
   protected:
      char m_type[ 32 ];
      int  m_amount;
      int  m_x;
      int  m_y;
      
   public:
      Animal( )
      {
         m_x = 0;
         m_y = 0;
         m_type[ 0 ] = NULL;
      }
      
      void SetPosition( int x, int y )
      {
         m_x = x;
         m_y = y;
      }
      
      void GetPosition( int *pX, int *pY )
      {
         *pX = m_x;
         *pY = m_y;
      }

      void SetAmount( int amount )
      {
         m_amount = amount;
      }
      
      void GetAmount( int *pAmount )
      {
         *pAmount = m_amount;
      }

      void SetType( const char *pType )
      {
         strcpy( m_type, pType, sizeof(m_type) );
      }
      
      void GetType( char *pType, int length )
      {
         strcpy( pType, m_type, length );      
      }
      
      void Speak( void )
      {
         printf( "The %s is speaking!\n", m_type );
      }
   };