-5

I've started learning C#, but now i'm a little confused, because there i have some problem. I'm trying to do the inheritance of one class to another and it's not working. It says something like "You have no right parameter" So, there is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication15 // Base class
{
    public class cakes
    {
        public int cakes_for;

        public cakes(int number) // constructor
        {
            cakes_for = number;
        }
        public int cakes_get // get value
        {
            get
            {
                return cakes_for;
            }   
            set
            {
                cakes_for = value;
            }
        }
        public static int cakes_plus_number(int n) // number plus constant
        {
            return n + 42;
        }
    }
    }


namespace ConsoleApplication15 // Derived Class
{
    public class Class2 : cakes // inheritance test
    {
        public int cakeses;
        public int Size { get; set; }
    }
}
Gene
  • 4,192
  • 5
  • 32
  • 56
Roman Krylov
  • 49
  • 1
  • 11
  • 9
    Your base class has a constructor with an argument. Your child class doesn't implement it. – ernest Nov 11 '15 at 14:59
  • 1
    You need to add a constructor to your derived class, which you would have found if you did some research for the actual compiler error you get. Such as [Base Class Doesn't Contain Parameterless Constructor?](http://stackoverflow.com/questions/7689742/base-class-doesnt-contain-parameterless-constructor). – CodeCaster Nov 11 '15 at 14:59

3 Answers3

1

The class cakes does not have a parameter-less constructor, therefore any derived types must explicitly call one of the constructors of the base-class. Class2 needs to invoke the constructor cakes(int number) with a value for number, since the base class cakes cannot be instantiated without it. You can access the base-class constructors via the syntax : base() after constructor signature in derived class. Example:

public class Class2 : cakes
{
    public Class2(int number) : base(number) { }
    public int cakeses;
    public int Size { get; set; }
}

Example 2:

public class Class3 : cakes
{
    public Class3() : base(8) { }
}
odyss-jii
  • 2,619
  • 15
  • 21
1

You should add a constructor to your derived class like so:

public class Class2 : cakes // inheritance test
{
    public Class2(int number) : base(number)
    {}
    public int cakeses;
    public int Size { get; set; }
}

A few tings to note:

  • the classes you marked with comments are actually namespaces, not the same thing.

  • I would suggest looking into Capitalization styles as well as it is all over the place in your example.

Igor Meszaros
  • 2,081
  • 2
  • 22
  • 46
0

I modified your code slightly. I also showed how to instantiate and use it in the Form1() constructor. Of course that isn't the right place to put it in a final version, but this should compile and execute to give you an idea of the process.

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        cake2 test = new cake2();
        test.cakes = 2; //inheritance
        test.CakeSize= 50; //not inheritance
        MessageBox.Show(test.cakes.ToString() + " | " + test.CakeSize.ToString());
    }
}

public class cake
{
    private int _cakes;
    public int cakes // get value
    {
        get
        {
            return _cakes;
        }
        set
        {
            _cakes = value;
        }
    }
    public cake()
    {
        public cake() : this(0) { } //constructor chaining.  Call your original function if no value is passed in
    }
    public cake(int number) // constructor
    {
        _cakes = number;
    }

    public static int cakes_plus_number(int n) // number plus constant //I don't see the need for static here, but whatever.
    {
        return n + 42;
    }
}

public class cake2 : cake
{
    private int _cake2;

    public int cakes2
    {
        get { return _cake2; }
        set { _cake2 = value; }
    }
    private int _size;

    public int CakeSize
    {
        get { return _size; }
        set { _size = value; }
    }

  }
}
Aaron
  • 1,313
  • 16
  • 26