C++ Classes Vs. C# Classes - Virtual functions in C and C#
(Page 4 of 6 )
Both C++ and C# support virtual functions. Consider the following C++ sample in which we have two classes, one named Base and the other named Derived:
#include <iostream>
using namespace std;
class Base
{
public:
void doWork()
{
cout << "Base class working";
}
protected:
virtual void doWork1() = 0;
};
class Derived : public Base
{
public: void doWork2()
{
cout << "Derived class working";
}
void doWork1()
{
cout << "Dervied pure virtual function working";
}
};
void main()
{
Derived d;
d.doWork1();
}
The Base class contains two functions: one named doWork, and another pure virtual function named doWork1. doWork1 can only be used if it is implemented in a class derived from Base. In our Derived class (which is publicly inherited from Base), we have a new function, doWork2, as well as the actual implementation of the pure virtual function which was derived from Base, named doWork1.
Implementing this sort of functionality in C# is easy. Take a look at the following C# source code:
using System;
abstract class Base
{
public void doWork()
{
Console.WriteLine("Base class working");
}
public abstract void doWork1();
}
class Derived : Base
{
public void doWork2()
{
Console.WriteLine("Derived class working");
}
public override void doWork1()
{
Console.WriteLine("Dervied pure virtual function working");
}
};
class EntryPoint
{
public static void Main()
{
Derived d = new Derived();
d.doWork1();
}
}
By using the abstract access modifier for both our class declaration and the declaration of its doWork1 method, we are implementing the same sort of pure virtual function behaviour that we have in our C++ sample. This tells the C# compiler that the Base class cannot be referenced unless it is the base class of another class that contains an implementation of the doWork1 function.
To tell the C# compiler that we want to override the abstract version of the doWork1 method in our Derived class, we prefix it with the override keyword. When the C# compiler sees a function prefixed with the override keyword in a derived function, it will check its base class for a function with the same signature and always use the derived one in its place; unless we explicitly call the base classes version.
To access any inherited member or function in a base class from a derived class, C# provides us with a class alias named base. Using this class, we can explicitly reference methods and members of our base class from within a derived class, like this:
using System;
class first
{
public void writeIt()
{
Console.WriteLine("Writing from base class");
}
}
class second : first
{
public second()
{
base.writeIt();
}
}
class EntryPoint
{
public static void Main()
{
second s = new second();
}
}
In our example above we have one base class (first) and one derived class (second). When a new instance of the second class is created, its constructor automatically calls the writeIt method of its base class, which uses the Console.WriteLine method to output some text to the screen. This brings us to another feature common to both C++ and C#: polymorphism.
Next: Polymorphism in C and C# >>
More C# Articles
More By Jordan Leverington