Make a separate class for your shape class variables without any methods.
Example:
public class Shape { public double base {get; set;} public double height {get; set;} }
public class Square : Shape { Square(double b, double h) { base = b; height = h; } public double Area() { return base * height; } }
public class Triangle { Triangle(double b, double h) { base = b; height = h; } public double Area() { return (base * height) / 2; } }
// Running it
class Program { static void Main(string[] args) { Shape square = new Shape(10,10); Triangle triangle = new Triangle(25, 10); Circle circle = new Circle(5);
Make a separate class for your shape class variables without any methods.
ReplyDeleteExample:
public class Shape
{
public double base {get; set;}
public double height {get; set;}
}
public class Square : Shape
{
Square(double b, double h)
{
base = b;
height = h;
}
public double Area()
{
return base * height;
}
}
public class Triangle
{
Triangle(double b, double h)
{
base = b;
height = h;
}
public double Area()
{
return (base * height) / 2;
}
}
// Running it
class Program
{
static void Main(string[] args)
{
Shape square = new Shape(10,10);
Triangle triangle = new Triangle(25, 10);
Circle circle = new Circle(5);
Console.WriteLine("Square Area: {0}" , square.Area());
Console.WriteLine("Triangle Area: {0}", triangle.Area());
Console.WriteLine("Circle Area: {0}" , circle.Area());
}
}
/* Output
Square Area: 100
Triangle Area: 125
*/
Email me if you have any questions
Deletemalachias.harris23@gmail.com