computer science II
c m s c 214  
f a l l   2 0 0 1  

Inheritance

© by Charles Lin. All rights reserved. You must receive written permission from Charles Lin to reproduce this webpage in any form.

The following is code for inheritance. TurboCat is a class that inherits from Cat.

// -----------------------------------------------------
// Cat.h
// -----------------------------------------------------

#ifndef CAT_H
#define CAT_H

#include <iostream>
#include <string>

class Cat
{
   std::string name;   // cat's name
   std::string breed;  // cat's breed
   std::string *buddy;  // array of buddies
   int numBuddies;
public:
   Cat( std::string nameIn = "NONE",
        std::string breedIn = "NONE" );
   Cat( const Cat &other );
   ~Cat();
   Cat &operator=( const Cat &other );
   std::string getName() const;
   std::string getBreed() const;
   void setName( std::string nameIn );
   void setBreed( std::string breedIn );
   bool addBuddy( std::string buddyIn );
   bool hasBuddy( std::string buddyIn );
   void print( std::ostream &out ) const;
private:
   void init();
   void copy( const Cat & other );
   void freeMem();
};

ostream & operator<<( ostream &out, const Cat &cat );

#endif

// -----------------------------------------------------
//   Cat.cpp
// -----------------------------------------------------

#include <iostream>
#include <string>

#include "Cat.h"

using namespace std;

// default cons
Cat::Cat( string nameIn, string breedIn )
  : name( nameIn ), breed( breedIn ),
     buddy( NULL ), numBuddies( 0 )
{
}

// copy cons
Cat::Cat( const Cat &other )
{
   copy( other );
}

// assignment operator
Cat & Cat::operator=( const Cat &other )
{
  if ( this != &other )
    {
      freeMem();
      copy( other ); // init in copy
    } 
  return *this;
}

// destructor
Cat::~Cat()
{
  freeMem();
}

string Cat::getName() const
{
  return name;
}

string Cat::getBreed() const
{
  return breed;
}

void Cat::setName( string nameIn )
{
  name = nameIn;
}

void Cat::setBreed( string breedIn )
{
  breed = breedIn;
}

bool Cat::addBuddy( string buddyIn )
{
  if ( ! hasBuddy( buddyIn ) )
    {
      string *newArr = new string[ numBuddies + 1 ];
      for ( int i = 0; i < numBuddies; i++ )
	newArr[ i ] = buddy[ i ];
      newArr[ numBuddies ] = buddyIn;

      delete [] buddy;
      buddy = newArr;

      numBuddies++;

      return true;
    }
  return false;
}

bool Cat::hasBuddy( string buddyIn )
{
  for ( int i = 0; i < numBuddies; i++ )
    if ( buddy[ i ] == buddyIn )
      return true;
  return false;
}

void Cat::init()
{
  buddy = NULL;
  numBuddies = 0;
}

void Cat::copy( const Cat & other )
{
  init();
  numBuddies = other.numBuddies;

  if ( numBuddies > 0 )
    {
      buddy = new string[ numBuddies ];
      for ( int i = 0; i < numBuddies; i++ )
	buddy[ i ] = other.buddy[ i ];
    }
}

void Cat::freeMem()
{
  delete [] buddy;
  buddy = NULL;
}

void Cat::print( std::ostream &out ) const
{
  out << "Cat (" << name << ") has buddies: ";
  if ( numBuddies == 0 )
    cout << "NO BUDDIES!";
  else
    {
      for ( int i = 0; i < numBuddies; i++ )
	cout << buddy[ i ] << " ";
    }
}

ostream & operator<<( ostream &out, const Cat &cat )
{
  cat.print( out );
  return out;
}

// -----------------------------------------------------
//  TurboCat.h
// -----------------------------------------------------

#ifndef TURBOCAT_H
#define TURBOCAT_H

#include <iostream>
#include <string>

#include "Cat.h"

class TurboCat : public Cat
{
// inherits name, breed, buddy, numBuddies

  int speed, meowPower;

public:
   TurboCat( std::string nameIn = "NONE",
	     std::string breedIn = "NONE",
	     int speedIn = 5, int mewowPowerIn = 5 );
  // No dynamic memory, so these aren't required.
  //   TurboCat( const Cat &other );
  //   ~TurboCat();
  //   TurboCat &operator=( const TurboCat &other );
  int getSpeed() const;
  int getMeowPower() const;
  string getBreed() const;  // overrides Cat's getBreed()
  void setSpeed( int speedIn );
  void setMeowPower( int meowPowerIn );
  void print( std::ostream & out ) const;
};

ostream & operator<<( std::ostream &out, const TurboCat &cat );

#endif

// -----------------------------------------------------
//    TurboCat.cpp (inherited class)
// -----------------------------------------------------

#include <iostream>
#include <string>

#include "Cat.h"
#include "TurboCat.h"

using namespace std;

TurboCat::TurboCat( string nameIn, string breedIn,
		    int speedIn, int meowPowerIn )
  : Cat( nameIn, breedIn ), speed( speedIn ),
    meowPower( meowPowerIn )
{
  
}

int TurboCat::getSpeed() const
{
  return speed;
}

int TurboCat::getMeowPower() const
{
  return meowPower;
}

void TurboCat::setSpeed( int speedIn )
{
  if ( speedIn > 0 )
    speed = speedIn;
}

void TurboCat::setMeowPower( int meowPowerIn )
{
  if ( meowPowerIn > 0 )
    meowPower = meowPowerIn;
}

string TurboCat::getBreed() const
{
  return string( "TURBO-" ) + Cat::getBreed();
}

void TurboCat::print( ostream & out ) const
{
  Cat::print( out );  // calls base class
  out << endl;
  out << "(" << getName() << ") is a groovy TURBO-CAT";
}

ostream & operator<<( ostream &out, const TurboCat &cat )
{
  cat.print( out );
  return out;
}

// -----------------------------------------------------
// main.cpp
// -----------------------------------------------------

#include <iostream>
#include <string>

#include "Cat.h"
#include "TurboCat.h"

using namespace std;

int main()
{
  Cat fluffy( "Fluffy", "Manx" );

  fluffy.addBuddy( "Tiger" );
  fluffy.addBuddy( "Rex" );
  fluffy.addBuddy( "Mousie" );

  cout << fluffy << endl;
  cout << endl;
  cout << fluffy.getName() << " is a " << fluffy.getBreed() << endl;

  cout << "------" << endl;
  TurboCat whiskers( "Whiskers", "Manx" );

  whiskers.addBuddy( "Moe" );
  whiskers.addBuddy( "Barney" );
  whiskers.addBuddy( "Apu" );

  cout << whiskers << endl;
  cout << endl;
  cout << whiskers.getName() << " is a " << whiskers.getBreed() << endl;

  return 0;
}

// -----------------------------------------------------
//   OUTPUT
// -----------------------------------------------------
Cat (Fluffy) has buddies: Tiger Rex Mousie 

Fluffy is a Manx
------
Cat (Whiskers) has buddies: Moe Barney Apu 
(Whiskers) is a groovy TURBO-CAT

Whiskers is a TURBO-Manx

Web Accessibility