TonysOpenSource
|
|
PolyMorphic CSharp |
I was recently reading a C++ book as I like to at least once a year just to keep up to date with some the more, shall we say obscure aspects of C++ that I usually never use but it is good to know about them just incase you actually end up working for some company that has some C++ gurus around who like to use some of this stuff. ( The book was More Exceptional C++ by Herb Sutter ) and it got me wondering just how things like polymorphism would work in CSharp, mostly due to the fact that if you learn C++ there is at least a chapter in any book although I ( and I may just have forgotten here ) don't recall reading about how or even if polymorphism works in CSharp. So I decided to take a look and see what the results were just out of interest.
The polymorphism Theory goes something like this. If you take an object and declare it as a base class. You can then derive classes from that class and override some function in the child class. In the example the function I have overridden is the MakeNoise function. The MakeNoise function simply writes a line to the console window giving the text version of the sound made by the animal. For an unspecified Animal which is the base class this is just a grunt noise and for the Dog class the word bark and for the Cat class the word meow. The idea is this. You have three ( or more ) classes that inherit from or is the base class. You then have another class or some other piece of code that uses these classes in a more generic fashion. All that this code cares about is that the classes passed to it are either the type of or derived from the base class and that the code passed to it implements a function in this case the MakeNoise function. Of course we require it to call the correct function, so if a Cat is passed to the function the word meow is printed even if the function called takes the base class as it's only parameter.
class Animal
{
///
/// Legal virtual function declaration
///
public virtual void MakeNoise()
{
Console.WriteLine( "grunt" );
}
}
class Dog : Animal
{
public override void MakeNoise()
{
Console.WriteLine( "bark" );
}
}
class Cat : Animal
{
public override void MakeNoise()
{
Console.WriteLine( "meow" );
}
}
///
/// Summary description for Class1.
///
class Class1
{
///
/// Make the sound of the animal by passing the base class
///
///
public void MakeSound( Animal animal )
{
animal.MakeNoise();
}
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
Class1 class1 = new Class1();
Console.WriteLine( "Starting application" );
/// create an animal
Animal animal = new Animal();
/// create a dog
Dog dog = new Dog();
/// create a cat
Cat cat = new Cat();
/// call the animal make noise
animal.MakeNoise();
/// call the dog noise
dog.MakeNoise();
/// call the cat noise
cat.MakeNoise();
/// make an animal grunt by passing an animal
class1.MakeSound( animal );
/// pass dog as an animal and call dog makenoise
class1.MakeSound( dog );
/// pass cat as an animal and call cat makenoise
class1.MakeSound( cat );
Console.WriteLine( "Ending Application" );
}
}
The code starts with the declaration of the Animal class that has a function called MakeNoise and two child classes are declared that override the MakeNoise declaration and provide their own implementation. Then we have the Class1 Class that implements one function apart from main that is given a nice generic sounding name like MakeSound and takes the base class of Animal as its only parameter.
The Main function starts off by creating four objects ( including the Class1 object ) and then calling their individual MakeNoise functions directly. This all works as expected, so then we call the MakeSound object three times. Each time passing a different object that is of the type or derived from an Animal class object. What we are doing here is seeing if the compiler is clever enough to work out what type of object it is being passed and then call the correct function for that object. If you step through the code using the debugger you can see that it is. the output for this program is.
Starting Application grunt bark meow grunt bark meow Ending Application
If you wanted you could declare the base class in CSharp as an abstract class which would mean that the class would have no implementation of its own and would require the child classes to implement it. This would be done with
abstract class Animal
{
///
/// Legal pure virtual function declaration
///
public abstract void MakeNoise();
}
Of course if you did this you would not be able to instantiate an Animal Class object. but the code line in the MakeSound function
animal.MakeNoise();
would still work.