G+_Darryl Medley Posted September 17, 2014 Share Posted September 17, 2014 ShapeCalc - A simple C# OOP demo of how to use abstract base classes with sub-classes and explains polymorphism: https://github.com/Darryl-Medley/Coding-101/blob/master/ShapeCalc.cs The user selects a shape from a menu, enters data such as the length of a side, and the program calculates the shape's area. (I swear I didn't see the similar examples on MSDN until after I started this.) All of the code is in the linked ShapeCalc.cs file so to run it, just create a new C# project and copy/paste the code in. My main purpose for writing this is to show how you can write code that operates on the base class (such as calling the abstract methods) but then at run time pass an object of a sub-class and the code for the sub-class is executed. What's cool about this is that you can easily add more sub-classes for more shapes and the only code that has to be changed in the main program is the menu method. This is polymorphism in action and the main reason to use abstract classes / methods but I don't think it was really explained on the show. This is my first C# program so any suggestions are appreciated. https://github.com/Darryl-Medley/Coding-101/blob/master/ShapeCalc.cs Link to comment Share on other sites More sharing options...
G+_Louis Maresca Posted September 17, 2014 Share Posted September 17, 2014 Darryl, this is a great example! It doesn't matter MSDN has something similar, you put your own spin on it. Great! Link to comment Share on other sites More sharing options...
G+_Darryl Medley Posted September 18, 2014 Author Share Posted September 18, 2014 At the risk of dating myself I think the subconscious reason I chose shapes is that the first OOP language I had, Turbo Pascal 5.5, used shapes in the OOP Guide book it came with (which I still have of course). Link to comment Share on other sites More sharing options...
Recommended Posts