Return to the C++ Demos Home Page

SAMS Teach Yourself C++ in 21 Days

Chapter Eleven

Prog 1 :  Simple Inheritance

// STYC++ in 21 chapter 11 prog 1
//
//   Example of simple Inheritance
//
//

#include <iostream.h>

enum BREED {GOLDEN, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB};

class Mammal
{
	public:
	// constructors
	Mammal():itsAge(2),itsWeight(5){}
	~Mammal(){}

	// accessors
	int getAge() const { return itsAge; }
	int getWeight() const { return itsWeight; }
	void setAge (int age) { itsAge = age; }
	void setWeight (int weight) { itsWeight = weight; }

	// Other stuff
	void speak() const { cout << "Mammal sound! \n"; }
	void sleep() const { cout << "shhh. I'm Sleeping!\n"; }

	protected:               // only let me and derived classes see my data!
	int itsAge;
	int	itsWeight;

};// end of class Mammal

// class that derives from Mammal

class Dog : public Mammal
{
	public:
	// constructors
	Dog():itsBreed(GOLDEN){}
	~Dog(){}

	// accessors
	BREED getBreed() const { return itsBreed; }
	void setBreed(BREED breed) { itsBreed = breed; }

    // other stuff
	void begForFood() const { cout << "gimme, gimme, gimme...\n"; }
	void wagTail() const { cout << "I'm waggign my dogger tail!!\n"; }

	//	private:             // no derivations from here
	BREED itsBreed;

};// end of class Dog

int main()
{
   Dog fido;
   fido.speak();
   fido.wagTail();
   cout << "fido is " << fido.getAge() << " years old!\n";
   return 0;
}

// end of source

 

Prog 2: Overloading Constructors when Inheriting

// STYC++ in 21 chapter 11 prog 2
//
//   Example of overloading constructors in derived 
//  methods...
//
//

#include <iostream.h>

enum BREED {GOLDEN, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB};

class Mammal
{
	public:
	// constructors
	Mammal();
	Mammal(int age);
	~Mammal();

	// accessors
	int getAge() const { return itsAge; }
	int getWeight() const { return itsWeight; }
	void setAge (int age) { itsAge = age; }
	void setWeight (int weight) { itsWeight = weight; }

	// Other stuff
	void speak() const { cout << "Mammal sound! \n"; }
	void sleep() const { cout << "shhh. I'm Sleeping!\n"; }

	protected:               // only let me and derived classes see my data!
	int itsAge;
	int	itsWeight;

};// end of class Mammal

// class that derives from Mammal

class Dog : public Mammal
{
	public:
	// constructors
	Dog();
	Dog(int age);
	Dog(int age, int weight);
	Dog(int age, BREED breed);
	Dog(int age, int weight, BREED breed);
	~Dog();

	// accessors
	BREED getBreed() const { return itsBreed; }
	void setBreed(BREED breed) { itsBreed = breed; }

    // other stuff
	void begForFood() const { cout << "gimme, gimme, gimme...\n"; }
	void wagTail() const { cout << "I'm waggign my dogger tail!!\n"; }

	//	private:             // no derivations from here
	BREED itsBreed;

};// end of class Dog

// define member funcs

//Mammal constructors
Mammal::Mammal():
itsAge(1),itsWeight(5)
{
	cout << "Mammal Constructor\n";
}

Mammal::Mammal(int age):
itsAge(age),itsWeight(5)
{
	cout << "Mammal(int) Constructor\n";
}

Mammal::~Mammal()
{
	cout << "Mammal Destructor\n";
}

// Dog constructors
Dog::Dog():
Mammal(),
itsBreed(GOLDEN)
{
	cout << "Dog Constructor\n";
}

Dog::Dog(int age):
Mammal(age),
itsBreed(GOLDEN)
{
	cout << "Dog(int) Constructor\n";
}

Dog::Dog(int age, int weight):
Mammal(age),
itsBreed(GOLDEN)
{
	itsWeight = weight;
	cout << "Dog(int int) Constructor\n";
}

Dog::Dog(int age, int weight, BREED breed):
Mammal(age),
itsBreed(breed)
{
	itsWeight = weight;
	cout << "Dog(int int BREED) Constructor\n";
}

Dog::Dog(int age, BREED breed):
Mammal(age),
itsBreed(breed)
{
	cout << "Dog(int BREED) Constructor\n";
}

Dog::~Dog()
{
	cout << "Dog Destructor\n";
}

// the driver 

int main()
{
	Dog fido;
	Dog rover(5);
	Dog buster(6,8);
	Dog yorkie(3,GOLDEN);
	Dog dobbie(4,20,DOBERMAN);

	fido.speak();
	rover.wagTail();
	
	cout << "Yorkie is " << yorkie.getAge() << " years old!\n";
	cout << "Dobbie weighs " << dobbie.getWeight() << " pounds\n";

return 0;

}  // end of source

Prog 3 : Member Function Overriding

// STYC++ in 21 chapter 11 prog 3
//
//   Overriding a base function in derived classes
//
//

#include <iostream.h>

enum BREED {GOLDEN, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB};

class Mammal
{
	public:
	// constructors
		Mammal() { cout <<"Mammal Constructor\n"; }
		~Mammal() { cout << "Mammal Destructor\n"; }

	
	// Other stuff
	void speak() const { cout << "Mammal sound! \n"; }
	void sleep() const { cout << "shhh. I'm Sleeping!\n"; }

	protected:              
	int itsAge;
	int	itsWeight;

};// end of class Mammal

// class that derives from Mammal

class Dog : public Mammal
{
	public:
	// constructors
		Dog(){ cout << "Dog Constructor\n";}
		~Dog(){ cout << "Dog Destructor\n"; }

    // other stuff
	void begForFood() const { cout << "gimme, gimme, gimme...\n"; }
	void wagTail() const { cout << "I'm waggign my dogger tail!!\n"; }
	void speak() const { cout << "This is a Dog Speaking!\n"; }

	//	private:             // no derivations from here
	BREED itsBreed;

};// end of class Dog

// define member funcs




// the driver 

int main()
{
	
	Mammal Biggie;
	Dog Ike;
	Biggie.speak();
	Ike.speak();
	
	
return 0;

}  // end of source

Prog 4 : Calling Base Class Methods

// STYC++ in 21 chapter 11 prog 4
//
//   Example of calling the base method when it has 
//   been overidden
//    
//


#include <iostream.h>

enum BREED {GOLDEN, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB};

class Mammal
{
	public:
	// constructors
		Mammal() { cout <<"Mammal Constructor\n"; }
		~Mammal() { cout << "Mammal Destructor\n"; }

	
	// Other stuff
	void speak() const { cout << "Mammal sound! \n"; }
	void sleep() const { cout << "shhh. I'm Sleeping!\n"; }

	protected:              
	int itsAge;
	int	itsWeight;

};// end of class Mammal

// class that derives from Mammal

class Dog : public Mammal
{
	public:
	// constructors
		Dog(){ cout << "Dog Constructor\n";}
		~Dog(){ cout << "Dog Destructor\n"; }

    // other stuff
	void begForFood() const { cout << "gimme, gimme, gimme...\n"; }
	void wagTail() const { cout << "I'm waggign my dogger tail!!\n"; }
	void speak() const { cout << "This is a Dog Speaking!\n"; }

	//	private:             // no derivations from here
	BREED itsBreed;

};// end of class Dog

// define member funcs




// the driver 

int main()
{
	
	Mammal Biggie;
	Dog Ike;
	Biggie.speak();
	Ike.speak();

	// call the base method

	cout << "\nThis is Ike calling the base function\n";
	Ike.Mammal::speak();
	cout << "\n\n";

		
return 0;

}  // end of source

 Prog 5 : Polymorphism

// STYC++ in 21 chapter 11 prog 5
//
//   Polymorphism.  Plain and Simple....
//
//

#include <iostream.h>

class Mammal
{
   public:
   Mammal():itsAge(1) {}
   virtual ~Mammal() {}
   virtual void speak() const { cout <<"Mammal Speak!\n"; }
   protected:
   int itsAge;
}; //end of Mammal Class

class Dog : public Mammal
{
   public:
   void speak() const { cout << "Woof!\n"; }
};

class Cat : public Mammal
{
   public:
   void speak() const { cout << "Meow!\n"; }
};

class Pig : public Mammal
{
   public:
   void speak() const { cout << "Oink!\n"; }
};

class Horse : public Mammal
{
   public:
   void speak() const { cout << "Whinny!\n"; }
};


int main()
{

    Mammal *theArray[5];
	Mammal *ptr;
	int choice, i;

	  for (i=0; i<5; i++ )
	    {
		  cout << "(1) Dog  (2) Cat (3) Pig (4) Horse : ";
		  cin >> choice;
		  switch (choice)
		    {
				case 1 : ptr = new Dog;
				break;
				case 2 : ptr = new Cat;
				break;
				case 3 : ptr = new Pig;
				break;
				case 4 : ptr = new Horse;
				break;
				default : ptr = new Mammal;
				break;
             } // end of switch
			   
			   theArray[i] = ptr;
         
		 } // end of for loop

		for ( i=0; i<5; i++ ) 
		      theArray[i]->speak();
			  
	 return 0;

} // end of source	 		  	  

Prog 6 : A Virtual Copy Constructor using the Clone() method

// STYC++ in 21 chapter 11 prog 6
//
//   Vitual Copy constructors using a Clone() func.
//   Constructors cannot be virtual so we use this 
//   method to provide the derived classes a way to 
//   copy themselves correctly
//

#include <iostream.h>

class Mammal
{
   public:
   Mammal():itsAge(1) { cout << "Mammal Constructor\n"; }
   virtual ~Mammal() { cout << "Mammal Destructor\n"; }
   Mammal (const Mammal &rhs);	
   virtual Mammal *clone() { return new Mammal(*this); } 
   virtual void speak() const { cout <<"Mammal Speak!\n"; }
   int getAge() const { return itsAge; }
   protected:
   int itsAge;
}; //end of Mammal Class

// define copy constructor
Mammal::Mammal(const Mammal &rhs): itsAge(rhs.getAge())
{
	cout << "Mammal copy constructor\n";
}


class Dog : public Mammal
{
   public:
	   Dog() { cout << "Dog Constructor\n"; }
	   virtual ~Dog() { cout << "Dog Destructor\n"; }
	   Dog( const Dog & rhs);
	   void speak() const { cout << "Woof!\n"; }
       virtual Mammal *clone() { return new Dog(*this); }
};

// define dog copy constructor
Dog::Dog(const Dog &rhs):
Mammal(rhs)
{
	cout << "Dog copy Constructor\n";
}


class Cat : public Mammal
{
   public:
	   Cat() { cout << "Cat Constructor\n"; }
	   virtual ~Cat() { cout << "Cat Destructor\n"; }
	   Cat( const Cat & rhs);
	   void speak() const { cout << "Meow!\n"; }
       virtual Mammal *clone() { return new Cat(*this); }
};

// define Cat copy constructor
Cat::Cat(const Cat &rhs):
Mammal(rhs)
{
	cout << "Cat copy Constructor\n";
}


enum ANIMALS { MAMMAL, DOG, CAT };
const int numAnimalTypes = 3;

int main()
{

    Mammal *theArray[numAnimalTypes];
	Mammal *ptr;
	int choice, i;

	  for (i=0; i<numAnimalTypes; i++ )
	    {
		  cout << "(1) Dog  (2) Cat (3) Mammal : ";
		  cin >> choice;
		  switch (choice)
		    {
				case DOG : ptr = new Dog;
				break;
				case CAT : ptr = new Cat;
				break;
				default : ptr = new Mammal;
				break;
             } // end of switch
			   
			   theArray[i] = ptr;
         
		 } // end of for loop

	  // Now make a copy while you speak....

	  Mammal *otherArray[numAnimalTypes];
	  for (i=0; i<numAnimalTypes; i++ )
	  {
		  theArray[i]->speak();
		  otherArray[i] = theArray[i]->clone();
      }

	  // Now, did the array copy over correctly?

	  for (i=0; i<numAnimalTypes; i++ )
		     otherArray[i]->speak();


	 return 0;

} // end of source	 		  	  

Return to the C++ Demos Home Page