Home > Articles > Programming > .NET and Windows Programming

Inheritance and Polymorphism in C++ and C#

  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss

Like this article? We recommend

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.

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());
anInvoiceDoc.StoreDocument();
// Various workflows
// Sending documents to recipients
// Signing documents, archiving, etc.
// Then finally...        
Console.ReadLine();

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

  • Share ThisShare This
  • Save To Your Account
Essential C# 2.0

Like this article? We recommend

Essential C# 2.0

Learn MoreAdd To Cart

Discussions

comments powered by Disqus

Related Resources

#TuesdayTrivia: Spotlight on WP7 (Win a copy of Sams Teach Yourself Windows Phone 7 Application Development)
By on May 2, 2012Comments
These days, what CAN'T a smartphone do? Microsoft is putting their own spin on things to help you experience "life in motion" when using your device. Instead of containing static application icons, the re-imagined Start screen features live Tiles showing real-time content updates.

March Trivia #1: Let there be light! (Win Microsoft Visual Studio LightSwitch Unleashed)
By on March 13, 2012Comments
Want a simplified self-service tool to help you build business applications for the desktop and beyond? Microsoft programmers… meet Visual Studio LightSwitch.

February Trivia #2: There's an App for That (Win Sams Teach Yourself iOS 5 Application Development in 24 Hours)
By on February 28, 2012Comments
In less than a decade, the iOS platform has changed the way we think about mobile communication.

See All Related Blogs