Another C# example with constructors in a derived class using the repl.it online tool for C# .
Another good online tool can be found on this web page.
You can run it here with the command:
1 | mono main.exe |
The source code is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | using System; namespace InheritanceApplication { class Ship { double ship_width; double ship_length; // Default constructor for class Ship public Ship() { Width = Length = 0.0; } // Constructor for Ship get width and length of Ship public Ship(double w, double l) { Width = w; Length = l; } // Properties for Width public double Width { get { return ship_width; } set { ship_width = value < 0 ? -value : value; } } // Properties for Length public double Length { get { return ship_length; } set { ship_length = value < 0 ? -value : value; } } // display Ship status public void ShipStatus() { Console.WriteLine("The ship width and length in metters are: Width " + Width + "| Length " + Length); } } // A derived class of Ship named Interstellar_Ship. class Interstellar_Ship : Ship { string Interstellar_Engine; // A default constructor of Interstellar_Ship. public Interstellar_Ship() { Interstellar_Engine = "Velocity"; } // Constructor base public Interstellar_Ship(string s, double w, double l) : base(w, l) { Interstellar_Engine = s; } // Return area of Interstellar_Ship. public double Area() { return Width * Length; } // Display engine of Interstellar_Ship's. public void DisplayInterstellar_Engine() { Console.WriteLine("Interstellar_Ship engine for " + Interstellar_Engine); } } // Inheriting Interstellar_Ship class class Speed_Interstellar_Ship : Interstellar_Ship { int speed_ship; // Constructor public Speed_Interstellar_Ship(int speed, string s, double w, double l) : base(s, w, l) { speed_ship = speed; } // Display the speed. public void Display_Speed() { Console.WriteLine("Speed in km/s is: " + speed_ship); } } // Driver Class class GFG { // Main Method public static void Main() { Speed_Interstellar_Ship first_ship = new Speed_Interstellar_Ship(131000, "catafest Velocity Engine", 1000.33, 176.3); Speed_Interstellar_Ship second_ship = new Speed_Interstellar_Ship(176000, "mythcat Hybrid Variable-impulse Chemical", 177.0, 441.0); Console.WriteLine("Details of first ship: "); first_ship.DisplayInterstellar_Engine(); first_ship.ShipStatus(); first_ship.Display_Speed(); Console.WriteLine("Area of ship: " + first_ship.Area()); Console.WriteLine(); Console.WriteLine("Details of second_ship: "); second_ship.DisplayInterstellar_Engine(); second_ship.ShipStatus(); second_ship.Display_Speed(); Console.WriteLine("Area of ship: " + second_ship.Area()); } } } |