0% found this document useful (0 votes)
27 views4 pages

CSH

The document defines classes for basic shapes - Circle, Rectangle, Square - that inherit from a base Shapes class. The Shapes class defines properties for length, width, height and methods to calculate area and perimeter that are overridden by the subclasses. The Circle class calculates area as PI * radius^2 and perimeter as 2 * PI * radius. The Rectangle class calculates area as length * width and perimeter as 2 * (length + width). A Square class inherits from Rectangle. A Program class demonstrates creating shape objects and calling their calculation methods.

Uploaded by

n02021908g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

CSH

The document defines classes for basic shapes - Circle, Rectangle, Square - that inherit from a base Shapes class. The Shapes class defines properties for length, width, height and methods to calculate area and perimeter that are overridden by the subclasses. The Circle class calculates area as PI * radius^2 and perimeter as 2 * PI * radius. The Rectangle class calculates area as length * width and perimeter as 2 * (length + width). A Square class inherits from Rectangle. A Program class demonstrates creating shape objects and calling their calculation methods.

Uploaded by

n02021908g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

using System;

class Shapes
{
protected double length;
protected double width;
protected double height;

public Shapes(double length, double width, double height)


{
this.length = length;
this.width = width;
this.height = height;
}

public virtual double CalculateArea()


{
return 0;
}

public virtual double CalculatePerimeter()


{
return 0;
}
}

class Circle : Shapes


{
public Circle(double radius) : base(radius, 0, 0)
{
}

public override double CalculateArea()


{
return Math.PI * length * length;
}

public override double CalculatePerimeter()


{
return 2 * Math.PI * length;
}
}

class Rectangle : Shapes


{
public Rectangle(double length, double width) : base(length, width, 0)
{
}

public override double CalculateArea()


{
return length * width;
}

public override double CalculatePerimeter()


{
return 2 * (length + width);
}
}

class Square : Rectangle


{
public Square(double side) : base(side, side)
{
}
}

class Program
{
static void Main(string[] args)
{
Circle circle = new Circle(5);
Rectangle rectangle = new Rectangle(4, 6);
Square square = new Square(5);

Console.WriteLine("Circle:");
Console.WriteLine("Area: " + circle.CalculateArea());
Console.WriteLine("Perimeter: " + circle.CalculatePerimeter());
Console.WriteLine();

Console.WriteLine("Rectangle:");
Console.WriteLine("Area: " + rectangle.CalculateArea());
Console.WriteLine("Perimeter: " + rectangle.CalculatePerimeter());
Console.WriteLine();

Console.WriteLine("Square:");
Console.WriteLine("Area: " + square.CalculateArea());
Console.WriteLine("Perimeter: " + square.CalculatePerimeter());
Console.WriteLine();
}
}

Number 2

using System;

class Shapes
{
public virtual double CalculateArea()
{
return 0;
}

public virtual double CalculatePerimeter()


{
return 0;
}
}

class Triangle : Shapes


{
private double baseLength;
private double height;

public Triangle(double baseLength, double height)


{
this.baseLength = baseLength;
this.height = height;
}

public override double CalculateArea()


{
return 0.5 * baseLength * height;
}

public override double CalculatePerimeter()


{
// Assuming it's an equilateral triangle for simplicity
return 3 * baseLength;
}
}

class Program
{
static void Main(string[] args)
{
Triangle triangle = new Triangle(4, 5);
Rectangle rectangle = new Rectangle(4, 6);
Circle circle = new Circle(5);

Console.WriteLine("Triangle:");
Console.WriteLine("Area: " +
triangle.CalculateArea());
Console.WriteLine("Perimeter: " +
triangle.CalculatePerimeter());
Console.WriteLine();

Console.WriteLine("Rectangle:");
Console.WriteLine("Area: " +
rectangle.CalculateArea());
Console.WriteLine("Perimeter: " +
rectangle.CalculatePerimeter());
Console.WriteLine();

Console.WriteLine("Circle:");
Console.WriteLine("Area: " + circle.CalculateArea());
Console.WriteLine("Perimeter: " +
circle.CalculatePerimeter());
Console.WriteLine();
}
}

You might also like