I'm a C# beginner (3-day experience). Today I'm practising the inheritance in C#. Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Geometry
{
public class Geometry
{
public double perimeter;
public double area;
public bool special;
}
public class Rect : Geometry
{
public void Rectangle( double length, double width )
{
perimeter = (length + width) * 2;
area = length * width;
special = (length == width) ? true : false;
Console.WriteLine("Rectangle information: ");
Console.WriteLine("Perimeter: " + perimeter);
Console.WriteLine("Area: " + area);
if (special) Console.WriteLine("This is a special rectangle. In fact, this is a square!");
Console.WriteLine();
}
}
public class Cir : Geometry
{
public void Circle(double radius)
{
perimeter = radius * 2 * Math.PI;
area = radius * radius * Math.PI;
Console.WriteLine("Perimeter: " + perimeter);
Console.WriteLine("Area: " + area);
Console.WriteLine();
}
}
public class Main
{
public static void main(String[] args)
{
Rectangle a = new Rectangle(23.00, 21.4);
Circle b = new Circle(5.8);
}
}
The compiler said that "The type or namespace of 'Rectangle' could not be found", same as Circle. What's wrong? I don't really understand although I tried many ways to fix them. How can I find and understand the errors?
RectandCir. Those are their names. Use their names, not different names.voidin your constructor definitions. This is incorrect, it should be simplypublic Rectangle(double length, double width)(orRectinstead since that's the actual class name) andpublic Circle(double radius)(again, orCirc).Rectangleis not the name of the class. I think you're right that he intended those to be constructors, but he needs to name them correctly as well.