Polymorphism [ C++ ]

Started by
2 comments, last by Xai 17 years, 7 months ago
Hello, Im learning about polymorphism and classes. I have some code, but im not quit sure whyd you want to do something like this. Any hels appreciated. Questions in code:

//virtual functions

#include <iostream>

using namespace std;

class Animal
{
	
public:

	virtual ~Animal()
	{
		cout << "\nAnimal deleted.\n";
	}

	void virtual Talk() const 
	{
		cout << "Animal says Hi!\n";
	}

};

class Cat : public Animal
{

public:

	virtual ~Cat()
	{
		cout << "\nCat deleted.\n";
	}

	void virtual Talk() const
	{
		cout << "Cat says Hi!\n";
	}

};

int main()
{
	// creates a pointer to animal that points to a Cat object
	Animal * pPet = new Cat(); // why woukd you want to creat something like this???

	Animal ani;
	Cat cat;

	ani.Talk();
	pPet->Talk();
	cat.Talk();

	delete pPet;

	pPet = NULL;

	return 0;
}

Advertisement
By creating a function that works on all Animal objects, you can apply this function to children of the Animal class. This means you have to write less code for the same effect, which is good.

A better example would be the istream class. You can create functions that work on an istream, and they will work on std::cin, on string streams and on file streams.
The easiest way to answer that is with a question.

Later on down the road, you write another object, lets call it AnimalKiller. You want AnimalKiller to be able to kill all kinds of animals. Things is, how would you do it?

If your animal classes were not derived from a common base class AnimalKiller would need to implement AnimalKiller->KillDog(dog) AnimalKiller->KillCat(cat) AnimalKiller->KillEmu(emu). Instead, it can have one method, AnimalKiller->KillAnimal(Animal) that accepts all kinds of animals with only a single code base.
Another example, when the library writer (like Microsoft) wants to have a standard set of funcationality and let you extend / customize this functionality.

For instance in .NET their is a 'Control' class for all the visible components, which have virtual functions for drawing themselves. Also extremely important is things like Forms and Panels have lists of all the Control objects on them. 'Button' and 'ListBox' inheirit from Control, so you can put Button and List objects on a Panel or Form and the Panel or form will know how to tell the Buttons and such to draw themselves. Also, you can further write your own "ImageButton" or "GraphicListBox" classes - adding features like showing pictures, and then you can put them into the forms and panels just like normal buttons, but pictures would show up.

Such is the point.

Or lets say you have a chess game. And you want computer controlled AI players. You might make a ComputerPlayer base class with a "MakeMove()" function. Then you can write a RandomComputerPlayer, DatabaseComputerPlayer, StandardComputerPlayer, etc ... each of which implements MakeMove differently (Random identifies legal moves then picks randomly, Database looks up common patterns in a database and picks highest value, Standard tries to pick the best move by evaluating trades up to 5 moves ahead, etc.

This topic is closed to new replies.

Advertisement