Shan - 1 year ago 89
C# Question

CalculateArea using poymorphism and virtual method - not getting proper result.

I have write a code to calculate the area of different shapes using polymorphism (Virtua & Override Method) but not getting correct result. :(

Below is my code:

``````class Program
{
static void Main(string[] args)
{
double side = 0;
double length = 0;
double width = 0;
double height = 0;
double baseoftriangle = 0;

UserChoice:
Console.WriteLine("For what shape you want to calculate the Area:\n1. Sqaure\n2. Rectangle\n3. Triangle\n4. Circle");
Console.Write("Please Select the number from above options: ");

switch (choice)
{
case 1:
Console.Write("Please enter the side of square: ");
break;
case 2:
Console.Write("Please enter the length of rectangle: ");
Console.Write("Please enter the width of rectangle: ");
break;
case 3:
Console.Write("Please enter the height of triangle: ");
Console.Write("Please enter the base of triangle: ");
break;
case 4:
break;
default:
goto UserChoice;
}

CalculateArea Sqa = new Square();
Sqa = new Rectangle();
Sqa = new Triangle();
Sqa = new Circle();
if (choice == 1)
{
Sqa.Area(side);
Sqa.ShowResult();
}
else if(choice==2)
{
Sqa.Area(length,width);
Sqa.ShowResult();
}
else if(choice==3)
{
Sqa.Area(height, baseoftriangle);
Sqa.ShowResult();
}
else
{
Sqa.ShowResult();
}

ChoiceOfAnotherCalculation:
Console.Write("\nDo you want to calculate area of any other shape? Give input in Yes or NO: ");

switch (choice1.ToUpper())
{
case "YES":
goto UserChoice;
case "NO":
break;
default:
goto ChoiceOfAnotherCalculation;
}
}
}
class CalculateArea
{
public double result;
public virtual void Area(double side)
{
}
public virtual void Area(double length, double width)
{
}
public void ShowResult()
{
}
}
class Square: CalculateArea
{

public override void Area(double side)
{
result = side * side;
}

}
class Rectangle:CalculateArea
{
public override void Area(double length, double width)
{
result = length * width;
}
}
class Triangle:CalculateArea
{
public override void Area(double height, double baseoftriangle)
{
result = (height * baseoftriangle)/2;
}
}
class Circle:CalculateArea
{
{
}
}
``````

For Rectangle & Triangle, I am always getting 0 as a result and for square I am getting result from circle. I think this issue is causing because of the way I have created objects for class (May be because of lack of understanding in polymorphism).

Can someone please have a look and let me know what is the actual problem here.

Apart from this also I would appreciate your efforts if you can provide me the solution with get & set properties for each class. I have tried to write the code (Where I have tried to define the property in each derive class as well as in base class) but was getting various errors that's why I have jump on above solution.

I will also request please do not downvote the question as system won't allow me ask question for a long time. As this is the only platform where I can understood the things properly.

Many Thanks.

Inside the `switch` statement, after case 1, your program will exit. This is because you have used `return;`, you might have intended to use `break;` instead. Break statement ensures that your switch wont fall through to the next statement, and a return statement would terminate the execution immediately.
``````case 1: