Home > Articles > Programming > C#

Inheritance and Polymorphism in C++ and C#

  • Print
  • + Share This
C# is often perceived as a much higher-level language than C++. C# is a managed language, but it provides most if not all the same features as C++. Software specialist Stephen Morris shows you how to translate between the two languages without losing the benefits of inheritance and polymorphism.
Like this article? We recommend

Like this article? We recommend

Programming languages generally reflect many aspects of the eras in which they are created. In the same way as buildings are often referred to as frozen music, programming languages also embody what become frozen concepts of technology. C++ is in many ways very similar to C#, but for the most part the two languages follow divergent design philosophies. However, it is not an onerous task to translate source code between the two languages.

C++ is rooted in the early stages of PC-based object orientation. C# is a much easier language to master, but this in no way suggests that C# is a lightweight language. Rather, much of the housekeeping required in C++ is simply no longer necessary in the C# world. This derives from the fact that C# runs on the .NET platform, which automatically takes care of a lot of the housekeeping to which C++ programmers have become all too accustomed.

The three pillars of object-oriented programming (OOP) are:

  • Encapsulation
  • Inheritance
  • Polymorphism

In this article, I compare and contrast the language facilities for inheritance and polymorphism provided by C++ and C#. The encapsulation features of both languages are pretty much equivalent, so I won't talk about this aspect of OOP.

The Takeaway

If you are following this series of articles, you will not be surprised to hear that I use the SharpDevelop IDE (Integrated Development Environment) product. As with the other articles, the code for this article is available as a complete SharpDevelop project. The fact that SharpDevelop is an open source product illustrates something I've long suspected: There really is a growing global community of developers. We're seeing the fruits of this huge group of virtual teams in the form of the growing range of open source software. At the moment, open source projects are a bit like islands not yet joined up. But, I bet it's only a matter of time before open source projects start to share more code between the various projects. Once this happens, it's likely that the open source community will eventually become bigger than any of the closed source vendors. What will probably happen then is that all or nearly all vendors will open their source code—no more proprietary, hard-to-learn toolkits, no more bug-ridden installers. This might usher in a new era of easy-to-use software.

Listing 1 shows a sneak preview of the finished C# code, where the following occurs:

  1. I create a specialized document called an InvoiceDocument.
  2. I instantiate the InvoiceDocument by calling the base class constructor.
  3. I call a polymorphic method defined as abstract in the base class.

Listing 1 The Finished C# Code

InvoiceDocument anInvoiceDoc =
 new InvoiceDocument(133, "A Big Customer",
  (int)documentType.INVOICE, 3500);
Console.WriteLine("Invoice name: {0}. Price value: {1}.",
  anInvoiceDoc.DocumentName, anInvoiceDoc.getPriceValue());
// Various workflows
// Sending documents to recipients
// Signing documents, archiving, etc.
// Then finally...        

One other thing I'll do along the way is to define some simple rules for converting C++ into C#.

  • + Share This
  • 🔖 Save To Your Account