Rich Porter
Rich Porter

Reputation: 213

Very basic exercise in Virtual functions

Alright, so here's the exercise: You must define three classes. One class named MyNum that contains a variable of type int. Second class called MyString, will be derived from MyNum and contains string. Third-class call MyType and returns a variable of type MyNum. Each class will be set to a constructor function and virtual function called Show. The constructor function of MyType receive a MyNum variable, whereas the function Show of MyType will run the Show function of MyNum. Now, you need set two objects of type MyType and initialize them. Once an object type MyNum and once an object of type MyString.

Here's the code:

class MyNum
{
    protected int num;
    public MyNum(int num)
    {
        this.num = num;
    }
    public virtual void Show()
    {
        Console.WriteLine("The number is : " + num);
    }
}
class MyString : MyNum
{
    string str;
    public MyString(string str)
    {
        this.str= str;
    }
}
class MyType : MyNum
{
    MyNum num2;
    public MyType(MyNum num)
    {
        this.num2 = num;
    }
    public override void Show()
    {
        base.Show();
    }
}
class Program
{
    static void Main(string[] args)
    {

    }
}

I'm having the following error:

Error 1 'ConsoleApplication1.MyNum' does not contain a constructor that takes '0' arguments C:\Users\x\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 23 16 ConsoleApplication1

Anyone knows why am I having this error? Thanks.

Upvotes: 1

Views: 185

Answers (3)

Reed Copsey
Reed Copsey

Reputation: 564811

Since your class subclasses MyNum, it needs to be able to construct it. You don't have a default constructor, so you have to explicitly call it with a value.

For example:

             // Since this is here, you're saying "MyType is a MyNum"
class MyType : MyNum
{
    MyNum num2;
    public MyType(MyNum num) // You need to call the base class constructor 
                             // here to construct that portion of yourself
       : base(42) // Call this with an int...
    {
        this.num2 = num;
    }

The class MyString will need similar treatment. It will have to have a constructor that calls the base class constructor, too.

Note that, if the MyNum class had a default constructor (which could be protected), this wouldn't matter. Instead of calling these constructors, the other alternative is to do something like:

class MyNum
{
    public MyNum(int num)
    {
        this.num = num;
    }

    // Add a default constructor that gets used by the subclasses:
    protected MyNum()
        : this(42)
    {
    }

Edit in response to comments:

If you want to override the base class constructor, try:

class MyType : MyNum
{
    public MyType(int num)
       : base(num)
    {
    }

    public override void Show()
    {
          Console.WriteLine("The number [MyType] is : " + this.num);
    }
}

Upvotes: 8

DaveShaw
DaveShaw

Reputation: 52808

MyString does not have a constructor, so it will use the one from the base class MyNum, which doesn't have a default one with no parameters.

MyString needs a constructor or you need a paramterless one in the base class.

Upvotes: 1

mkro
mkro

Reputation: 1892

MyString implicitely has a zero-arg constructor because you did not specify any constructor in that class. You could either declare a zero-arg constructor in the super class or declare a one-arg constructor in MyString.

Upvotes: 2

Related Questions