-2

I am very beginner in c#. I created base class and derived class but i do not understand behavior of constructor in derived class it gives error "does not contain constructor that take 0 arguments" how to use it in derived class

 class A
    {
       public int x, y, z;

       public A(int i, int j)
       {
           x = i;
           y = j;
       }
        public void add(int i,int j)
        {
            z=x + y;
            Console.WriteLine(z);
        }
    }
    class B : A
    {
        public B (int k, int l)
        {
            x=k;
            y=l;
        }

        public void multi(int k,int l)
        {
            z = x * y;
            Console.WriteLine(z);
        }
    }

Usage:

    class Program
    {
        static void Main(string[] args)
         {
            A ad = new A(5,6);
            B m = new B(8, 9);
         }
    }
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
myadav
  • 76
  • 1
  • 1
  • 10

1 Answers1

1

Since B inherits A, it has to include a call to A's constructor, called the base contructor.

class B : A
{
    public B (int k, int l)
     : base(k, l)
    {
    }
}

This calls the code in A's constructor, populating x and y with the values in k and l.

Andrew
  • 4,953
  • 15
  • 40
  • 58