Home > OS >  Abstract class doesnt return anything
Abstract class doesnt return anything

Time:11-09

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.

CodePudding user response:

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.

CodePudding user response:

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;

    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.

  • Related