L B
L B

Reputation: 49

inheritence C# using new functions

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?

Upvotes: 0

Views: 107

Answers (2)

Raudel Ravelo
Raudel Ravelo

Reputation: 648

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;
        }
    }   
}

Upvotes: 1

BradleyDotNET
BradleyDotNET

Reputation: 61379

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

Upvotes: 2

Related Questions