C# Exercise 8

Mannan Ul Haq
0

Objective:

  • Concept of Polymorphism
  • Declare Virtual Methods

Activity:

Task 1:

  • Create a class shape and also create classes circle, square, triangle and rectangle as sub classes of class shape
  • Each sub class of shape should override a calculate area of class shape
  • Your classes should have overloaded constructors that will take the member variables as input

Task 2:

What if we wanted to use the definition of the derived class function? To accomplish this, we can add the keyword virtual to the declaration of the area() method in the shape class.

Task 3:

Another advantage of polymorphism is to keep the object of different sub types of same parent class in one array. The array will be on parent class type. As an exercise create take 5 shapes as input from user. Store these in one array. In one loop print the area of these shapes.


Solution:

using System;

class Shape
{
    public string Color;

    public Shape(string color)
    {
        Color = color;
    }

    public virtual double Area()
    {
        return 0;
    }
}

class Circle : Shape
{
    private double Radius;

    public Circle(double radius, string color) : base(color)
    {
        Radius = radius;
    }

    public override double Area()
    {
        return Math.PI * Math.Pow(Radius, 2);
    }
}

class Square : Shape
{
    private double Side;

    public Square(double side, string color) : base(color)
    {
        Side = side;
    }

    public override double Area()
    {
        return Math.Pow(Side, 2);
    }
}

class Rectangle : Shape
{
    private double Length;
    private double Width;

    public Rectangle(double length, double width, string color) : base(color)
    {
        Length = length;
        Width = width;
    }

    public override double Area()
    {
        return Length * Width;
    }
}

class Triangle : Shape
{
    private double Base;
    private double Height;

    public Triangle(double basee, double height, string color) : base(color)
    {
        Base = basee;
        Height = height;
    }

    public override double Area()
    {
        return 0.5 * Base * Height;
    }
}

class Program
{
    static void Main()
    {
        Triangle t1 = new Triangle(1.0, 9.0, "Red");
        Circle c1 = new Circle(2, "Blue");
        Rectangle r1 = new Rectangle(6, 2, "Orange");

        Console.WriteLine(t1.Area());
        Console.WriteLine(t1.Color);
        Console.WriteLine(c1.Area());
        Console.WriteLine(r1.Area());

        int count = 5;
        Shape[] shapesArray = new Shape[count];
        string color;

        for (int i = 0; i < count; i++)
        {
            Console.WriteLine("Press 1 for a triangle, 2 for rectangle, 3 for a circle.");
            switch (Console.ReadKey().KeyChar)
            {
                case '1':
                    Console.WriteLine();
                    Console.WriteLine("Enter base: ");
                    double baseVal = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine("Enter height: ");
                    double height = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine("Enter color: ");
                    color = Console.ReadLine();
                    shapesArray[i] = new Triangle(baseVal, height, color);
                    break;
                case '2':
                    Console.WriteLine();
                    Console.WriteLine("Enter length: ");
                    double length = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine("Enter width: ");
                    double width = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine("Enter color: ");
                    color = Console.ReadLine();
                    shapesArray[i] = new Rectangle(length, width, color);
                    break;
                case '3':
                    Console.WriteLine();
                    Console.WriteLine("Enter radius: ");
                    double radius = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine("Enter color: ");
                    color = Console.ReadLine();
                    shapesArray[i] = new Circle(radius, color);
                    break;
                default:
                    Console.WriteLine();
                    Console.WriteLine("Invalid input. Enter again.");
                    i--;
                    break;
            }
        }

        Console.WriteLine("\nPrinting areas of shapes:");
        for (int i = 0; i < count; i++)
        {
            Console.WriteLine("Shape " + (i + 1) + " has area: " + shapesArray[i].Area());
        }
    }
}


Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !