Jump to content

ShapeCalc - A simple C# OOP demo of how to use abstract base classes with sub-classes and explain...


G+_Darryl Medley
 Share

Recommended Posts

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

 Share

×
×
  • Create New...