Lecture 10 Class Samples

 

Example – These are the classes we’re including in all the examples

 

FILE: Animal.h

#include <iostream>

#include <string>

using namespace std;

 

class Animal

{

private:

  string name;

public:

  string getName()

  {

    return this->name;

  }

  void setName( string name )

  {

    this->name = name;

  }

  virtual void makeSound()

  {

    cout << "No sound defined" << endl;

  }

};

 

FILE: Dog.h

#include <iostream>

#include <string>

 

using namespace std;

 

class Dog : public Animal

{

public:

  void makeSound()

  {

    cout << "Woof" << endl;

  }

  void scratch()

  {

    cout << "Run away!!!" << endl;

  }

};

 

FILE: Cat.h

#include <iostream>

#include <string>

 

using namespace std;

 

class Cat : public Animal

{

public:

  void makeSound()

  {

    cout << "Meow" << endl;

  }

  void scratch()

  {

    cout << "Scratch" << endl;

  }

};

 

 

Example 1 – Basic menu

#include <iostream>

#include <string>

#include <vector>

#include "animal.h"

#include "dog.h"

#include "cat.h"

 

using namespace std;

 

vector< Animal* > animals;

 

bool getAnimal();

 

void main()

{

  // let's create a menu and put it in a loop

  while( true )

  {

    string input;

    cout << "Animal Catalog" << endl;

    cout << " 1 - Enter a new animal" << endl;

    cout << " x - Exit" << endl;

    cin >> input;

 

    switch( input[0] )

    {

    case '1':

      getAnimal();

      break;

    case 'x':

      // exit

      exit(0);

    default:

      cout << "I don't understand\n" << endl;

    }

  }

}

 

bool getAnimal()

{

  string input;

  cout << "What type of animal? (Dog, Cat):" << endl;

  cin >> input;

  if ( strcmpi( input.c_str(), "dog" ) == 0 ) // dog

  {

    animals.push_back( new Dog() );

  }

  else if ( strcmpi( input.c_str(), "cat" ) == 0 ) // cat

  {

    animals.push_back( new Cat() );

  }

  else

  {

    cout << "What?" << endl;

    return false;

  }

  cout << "Enter your " << input << "'s name: ";

  cin >> input;

  animals[ animals.size() - 1 ]->setName( input );

  return true;

}

 

 

 

Example 2 – Menu with creating and listing classes

#include <iostream>

#include <string>

#include <vector>

#include "animal.h"

#include "dog.h"

#include "cat.h"

 

using namespace std;

 

vector< Animal* > animals;

 

bool getAnimal();

void listAnimals();

 

void main()

{

  // let's create a menu and put it in a loop

  while( true )

  {

    string input;

    cout << "\nAnimal Catalog" << endl;

    cout << " 1 - Enter a new animal" << endl;

    cout << " 2 - List animals" << endl;

    cout << " x - Exit" << endl;

    cin >> input;

 

    switch( input[0] )

    {

    case '1':

      getAnimal();

      break;

    case '2':

      listAnimals();

      break;

    case 'x':

      // exit

      exit(0);

    default:

      cout << "I don't understand\n" << endl;

    }

  }

}

 

bool getAnimal()

{

  string input;

  cout << "What type of animal? (Dog, Cat):" << endl;

  cin >> input;

  if ( strcmpi( input.c_str(), "dog" ) == 0 ) // dog

  {

    animals.push_back( new Dog() );

  }

  else if ( strcmpi( input.c_str(), "cat" ) == 0 ) // cat

  {

    animals.push_back( new Cat() );

  }

  else

  {

    cout << "What?" << endl;

    return false;

  }

  cout << "Enter your " << input << "'s name: ";

  cin >> input;

  animals[ animals.size() - 1 ]->setName( input );

  return true;

}

 

void listAnimals()

{

  cout << "Listing animals" << endl;

  for( int i = 0; i < animals.size(); i++ )

  {

    cout << typeid( *animals[i] ).name() << " ";

    cout << animals[i]->getName() << endl;

  }

  cout << "Total of " << animals.size() << " animals" << endl;

}

 

Example 3 – Loading and Saving our Objects

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

#include "animal.h"

#include "dog.h"

#include "cat.h"

 

using namespace std;

 

vector< Animal* > animals;

 

bool getAnimal();

void listAnimals();

void loadAnimals();

void saveAnimals();

 

void main()

{

  // let's create a menu and put it in a loop

  while( true )

  {

    string input;

    cout << "\nAnimal Catalog" << endl;

    cout << " 1 - Enter a new animal" << endl;

    cout << " 2 - List animals" << endl;

    cout << " 3 - Load animals" << endl;

    cout << " 4 - Save animals" << endl;

    cout << " x - Exit" << endl;

    cin >> input;

 

    switch( input[0] )

    {

    case '1':

      getAnimal();

      break;

    case '2':

      listAnimals();

      break;

    case '3':

      loadAnimals();

      break;

    case '4':

      saveAnimals();

      break;

    case 'x':

      // exit

      exit(0);

    default:

      cout << "I don't understand\n" << endl;

    }

  }

}

 

bool getAnimal()

{

  string input;

  cout << "What type of animal? (Dog, Cat):" << endl;

  cin >> input;

  if ( strcmpi( input.c_str(), "dog" ) == 0 ) // dog

  {

    animals.push_back( new Dog() );

  }

  else if ( strcmpi( input.c_str(), "cat" ) == 0 ) // cat

  {

    animals.push_back( new Cat() );

  }

  else

  {

    cout << "What?" << endl;

    return false;

  }

  cout << "Enter your " << input << "'s name: ";

  cin >> input;

  animals[ animals.size() - 1 ]->setName( input );

  return true;

}

 

void listAnimals()

{

  cout << "Listing animals" << endl;

  for( int i = 0; i < animals.size(); i++ )

  {

    cout << typeid( *animals[i] ).name() << " ";

    cout << animals[i]->getName() << endl;

  }

  cout << "Total of " << animals.size() << " animals" << endl;

}

 

void loadAnimals()

{

  animals.clear(); //empty our list

 

  ifstream inputFile( "animals" );

 

  string className;

  string animalName;

  char buffer[ 128 ];

  while ( inputFile.getline( buffer, sizeof buffer, ',' ) )

  {

    className = buffer;

    inputFile.getline( buffer, sizeof buffer );

    animalName = buffer;

    if ( className.compare( "class Dog" ) == 0 )

    {

      animals.push_back( new Dog() );

    }

    else

    {

      animals.push_back( new Cat() );

    }

    animals[ animals.size() - 1 ]->setName( animalName );

  }

 

  inputFile.close();

}

 

void saveAnimals()

{

  ofstream outputFile( "animals" );

  for( int i = 0; i < animals.size(); i++ )

  {

    outputFile << typeid( *animals[i] ).name() << ",";

    outputFile << animals[i]->getName() << endl;

  }

  outputFile.close();

}