I'm following a YouTube video and am wondering if there are any other efficient ways of doing this. Is it possible to do it with an interface too?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Learning
{
class Program
{
static void Main(string[] args)
{
Shape Rect = new Rectangle(5, 3);
Shape Tri = new Triangle(8, 12);
Rectangle CombinedRectangle = new Rectangle(3, 3) + new Rectangle(6, 6);
Console.WriteLine(CombinedRectangle.Area());
}
abstract class Shape
{
public abstract double Area();
}
class Triangle : Shape
{
private double theBase { get; set; }
private double theHeight { get; set; }
public Triangle(double theBase, double theHeight)
{
this.theBase = theBase;
this.theHeight = theHeight;
}
public override double Area()
{
return (theBase * theHeight) / 2;
}
}
class Rectangle : Shape
{
private double Length { get; set; }
private double Width { get; set; }
public Rectangle(double Length, double Width)
{
this.Length = Length;
this.Width = Width;
}
public override double Area()
{
return Length * Width;
}
public static Rectangle operator+ (Rectangle r1, Rectangle r2)
{
double combinedWidth = r1.Width + r2.Width;
double combinedLength = r1.Length + r2.Length;
return new Rectangle(combinedLength, combinedWidth);
}
}
}
}
+operator. Most people wouldn't think that a 1x1 rectangle "plus" a 2x2 rectangle makes a 3x3 rectangle. \$\endgroup\$