0

This is my code:

class Program
{
    abstract class Shape
    {
        public abstract double GetArea();
    }
    class Rectangle : Shape
    {
        double length = 100.57;
        double width = 100.14;

        public override double GetArea()
        {
            double result = length * width;

            return result;
        }
    }
    class Circle : Shape
    {
        double radius = 50.34;

        public override double GetArea()
        {
            double result = Math.PI * Math.Pow(radius, 2);

            return result;
        }
    }
    static void Main(string[] args)
    {
        
    }
}

Why does it not return anything? I wanted the result from length * width in my GetArea() method and the area from the circle too. I tried it with return and then the result but if I start the application nothing happens. I also tried doing this:

static void Main(string[] args)
    {
        GetArea();
    }

But that gets just red underlined.

  • Did you mean to do `new Rectangle().GetArea()`? – canton7 Nov 09 '21 at 09:45
  • @canton7 I dont know what to say i just want the area from my Rectangle with length * width and then print it out in the console as a double. –  Nov 09 '21 at 09:46
  • You're not actually calling the code you have written here, it will just enter the `Main` method, do nothing and then exit. – DavidG Nov 09 '21 at 09:47
  • And how do i call it? @DavidG –  Nov 09 '21 at 09:48
  • Like canton said, you need to create an instance of each of your classes and call the `GetArea` method. – DavidG Nov 09 '21 at 09:49

2 Answers2

1

You've made a whole lot of class definitions. That's great, but class definitions don't do anything by themselves. They are just a description of what can be done.

And what is it you are instructing your application to do?

static void Main(string[] args)
{
    
}

Nothing. That's why nothing is happening.

Why does it not return anything? I wanted the result from length * width in my GetArea() method and the area from the circle too.

Notice that you never specified that you wanted a circle or a rectangle. That should be your first clue that the code you wrote is not actually instructing the compiler the way you want it to.

The basic approach to OOP is that you first instantiate the object, and then you use the object as you want to.

Instantiation is a fancy word for creation. A class definition states how a circle behaves, but it does not create a circle for you. You have to tell the compiler "create me an instance of a Circle", which in code is done via the new keyword. Then, when you have that instance, you can call the GetArea method that you defined on its class.

Therefore:

static void Main(string[] args)
{
    Shape myCircle = new Circle();
    double circleArea = myCircle.GetArea();

    Console.WriteLine(circleArea);

    Shape myRectangle = new Rectangle();
    double rectangleArea = myRectangle.GetArea();

    Console.WriteLine(rectangleArea);
}

I do question if you've not jumped ahead a bit too much, as it seems you're already trying to tackle inheritance (abstract inheritance, at that) well before you seem to grasp the basics of OOP. You might want to take a step back and first get comfortable with using objects before bothering with inheritance.

Flater
  • 12,908
  • 4
  • 39
  • 62
  • But you dont got to do Shape myCircle = new Circle(); you can do Circle myCircle = new Circle(); too, does it make a difference? –  Nov 09 '21 at 09:54
  • @preqs: Your question is the heart of inheritance. If you ask that question, then you haven't understood inheritance, and I suggest referring to your lesson material about inheritance before trying to apply it. If it helps, a concrete use case: consider what it would be like having a method which prints the area of _any_ `Shape` you pass it, and this method doesn't know (nor does it need to know) whether the passed object is a `Circle`, `Rectangle`, `Triangle`, ... all it knows is that it is a `Shape`. The keyword here is "polymorphism". – Flater Nov 09 '21 at 09:57
  • Okay now i understand and i was kinda understanding polymorphism just not these abstract things. –  Nov 09 '21 at 10:03
  • @preqs: Maybe a bit more directly, I intentionally used `Shape myCircle` because your code is a direct attempt at using inheritance and polymorphism, so it seemed best to write code that made concrete use of it. – Flater Nov 09 '21 at 10:04
  • @preqs: The only thing `abstract` adds to inheritance and polymorphism is that is disallows you doing `new Shape()` directly. That is it. You use `abstract` on a class when you want to say "it makes no sense to directly instantiate this. This class is only a half-implementation and is supposed to be inherited by others to complete its implementation. It makes sense to instantiate those derived classes but not this base class". – Flater Nov 09 '21 at 10:06
1

It looks like you don't have any idea about objective programming. You should do some tutorials at least about C#, classes and objects.

Well, I'll try to clarify something at least.

What is a class? A class is something like blueprint for an object. For example if you have technical plan for a TV this is your class. If you have a TV - the device - this is your object. So class it's something that tells how to create and use an object of that class.

In your code you have three classes:

  • abstract class Shape - this is an abstract, you cannot create any object of that class; it's like you wanted to create a MAMMAL (not human, not dog, not cat, just mammal).
  • concrete class Circle
  • concrete class Rectange

Now you can create objects of concrete classes. But you don't do it here. You haven't created any rectangle and want to get area of it.

Look one more time at your Rectangle class. As I said - this is a class. Just a blueprint. But you make this class really narrow by defining its width and length. This class represents only just ONE rectangle (the same with circle). This is bad class. This class should represent ANY rectangle, not just one. So lets rewrite i a little:

class Rectangle : Shape
{
    double readonly length;
    double readonly width;

    public Rectangle(double length, double width)
    {
        this.length = length;
        this.width = width;
    }

    public override double GetArea()
    {
        double result = length * width;

        return result;
    }
}

This is far more better class, because it can represent any Rectangle. So now you can create the object of this class in your Main function:

Rectangle rect = new Rectangle(100.57, 100.14);
double area = rect.GetArea();

Do the same thing with your circle. And remember - do some more reading about objective programming.

Orion
  • 566
  • 4
  • 9
Adam Jachocki
  • 1,897
  • 1
  • 12
  • 28