Answer to Question #188604 in C# for santhosh asrith

Question #188604

 Mahesh has created the following code. The purpose is to create Circle and Triangle class by inheriting the Shape Class. Both the inherited classes should override the WhoamI() method of the Shape Class. The code has some bugs. Identify the Bugs and fix them.

public class Shape

{

private void WhoamI()

{

Console.WriteLine("I m Shape");

}

}

class Triangle : public Shape

{

public virtual void WhoamI()

{

Console.WriteLine("I m Triangle");

}

}


public class Circle : public Shape

{

void WhoamI()

{

Console.WriteLine("I m Circle");

}

}

class Program

{

static void Main(string[] args)

{

Shape s;

s = new Triangle();

s.WhoamI();

s = new Circle();

s.WhoamI();

Console.ReadKey();

}

}


1
Expert's answer
2021-05-05T00:04:38-0400
public class Shape
    {


        public virtual void WhoamI()
        {
            Console.WriteLine("I m Shape");
        }


    }


    class Triangle: Shape
    {


        public override void WhoamI()
        {
            Console.WriteLine("I m Triangle");
        }
    }


    public class Circle : Shape
    {


        public override void WhoamI()
        {
            Console.WriteLine("I m Circle");
        }


    }


    class Program
    {


        static void Main(string[] args)
        {


            Shape s;
            s = new Triangle();
            s.WhoamI();
            s = new Circle();
            s.WhoamI();
            Console.ReadKey();


        }


    }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS