Last active
December 18, 2015 05:19
-
-
Save TheBuzzSaw/5731707 to your computer and use it in GitHub Desktop.
Here, I will detail the use of inheritance.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Core; | |
export Samples.Polygon; | |
// The keyword 'base' allows inheritance. | |
// It automagically marks the destructor as 'virtual'. | |
base class Polygon | |
{ | |
int32 _edgeCount; | |
public get int32 EdgeCount { return _edgeCount; } | |
public abstract get double Area; | |
public constructor(int32 edgeCount) | |
{ | |
_edgeCount = Math.Max(3, edgeCount); | |
} | |
} | |
class RightTriangle is Polygon | |
{ | |
double _width; | |
double _height; | |
public override get double Area { return _width * _height / 2.0; } | |
public constructor(double width, double height) : base(3) | |
{ | |
_width = Math.Max(width, 0.1); | |
_height = Math.Max(height, 0.1); | |
} | |
} | |
int32 Main(String[] arguments) | |
{ | |
Polygon* polygon = new RightTriangle(2.0, 3.0); | |
Console.Write(polygon.Area).Write(NewLine); | |
delete polygon; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment