0

I'm making a game in C#. I created a base class Shape and seven other classes (Shape1, Shape2, ...) that inherit from Shape. In my main program I keep track of the shape currently in play by using

Shape Current;

Then I randomise its value to be equal to one of the shapes, for example:

Current = new Shape1()

The problem is, after I randomise I want to draw the shape with

Current.Draw()

Each shape has its own Draw function, and I want it to use that function specificly, and not the function in Shape. How can I do that?

5
  • 2
    Have you defined the Draw() method in the Shape class using either the virtual or abstract keyword? Commented Dec 28, 2017 at 21:58
  • 1
    It would be awesome if you provided a minimal reproducible example. Commented Dec 28, 2017 at 21:59
  • No.. should that make ot work? Commented Dec 28, 2017 at 21:59
  • Use the virtual keyword in Shape and use override in each of your child "shape" classes. Commented Dec 28, 2017 at 22:00
  • Possible duplicate of Classes and base class inheritance in C# Commented Dec 28, 2017 at 22:06

2 Answers 2

2

The concept you are describing is called polymorphism (treating different object types as one).

In C#, you do this via the virtual, abstract and override keywords. In the base class, you mark the polymorphic method as either virtual or abstract, with the difference being abstract methods must be defined by derived classes:

public abstract void Draw();

Note that virtual methods must define a body, while abstract methods must not. Then in the derived class you define the same method with the override keyword:

public override void Draw()
{
   //whatever implementation
}

For far more information, see MSDN

Sign up to request clarification or add additional context in comments.

Comments

1

I must say @BradleyDotNet explained it very well, I'm just adding a practical example that may help to clarify the use of it. Notice how I used all virtual, abstract and override keywords.

using System;

namespace ShapeExample
{
    class Program
    {
        public static void Main(string[] args)
        {
            for (int i = 0; i < 20; i++)
            {
                var shape = GetRandomShape();
                shape.Draw();
            }    
        }

        public static Random Random = new Random();

        public static Shape GetRandomShape()
        {
            var d = Random.Next(3);
            switch (d)
            {
                case 1:
                    return new Shape1();
                case 2:
                    return new Shape2();
                default:
                    return new Shape3();
            }
        }
    }

    public abstract class Shape
    {
        public virtual void Draw()
        {
            //Console.WriteLine("General Shape");
            Console.WriteLine("  _");
            Console.WriteLine(" / \\ ");
            Console.WriteLine("/___\\");
        }
    }

    public class Shape1 : Shape
    {
        public override void Draw()
        {
            //Console.WriteLine("I want a square instead");
            Console.WriteLine(" ____");
            Console.WriteLine("|    |");
            Console.WriteLine("|____|");
        }
    }

    public class Shape2 : Shape
    {
        public override void Draw()
        {
            //Console.WriteLine("I want a complicated circle instead");
            double r = 3.0;
            double r_in = r - 0.4;
            double r_out = r + 0.4;

            for (double y = r; y >= -r; --y)
            {
                for (double x = -r; x < r_out; x += 0.5)
                {
                    double value = x * x + y * y;
                    if (value >= r_in * r_in && value <= r_out * r_out)
                    {
                        Console.Write('*');
                    }
                    else
                    {
                        Console.Write(' ');
                    }
                }
                Console.WriteLine();
            }
        }
    }

    public class Shape3 : Shape
    {
        //I don't care how I look like, I'm using the regular shape drawing.

        //but I have some particular info that is not part of a regular Shape
        public int ParticularField { get; }

        public Shape3()
        {
            ParticularField = -100;
        }
    }   
}

1 Comment

Side note: an abstract class with no abstract methods is a little silly; all you are saying is you don't want the base class directly constructed even though nothing prevents it from being constructed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.