Press "Enter" to skip to content

Lesson 08.2 – Using class constructors with derived classes

Lesson Objectives

At the end of this lesson, you will know…

  • How a child (derived) class calls the constructor of its base class

 

Let’s clean up the code a little

Adding a constructor to a base class is a little more involved than adding one to a class that doesn’t have any derived (child) classes.

When you create an object from a derived class, it uses the constructor for both that class and its base class. So, if you add a custom constructor to your base class, you need to have a way for the derived class to pass values to it.

If you think about this, it makes sense why this needs to be done. With a custom constructor, you are saying, “I need these values before I can create this object.” When you create an object from a derived class, the base class still needs to have those values. So, you have to pass them to the derived class’ constructor, which passes them on to the base class’ constructor.

Here’s how you do that.

 


Link to video on YouTube

 

Step 1: Start Visual Studio Express 2013 for Desktop, and open the solution.

 

Step 2: Double-click on the Item class, in the Engine project, to start editing it. After the properties, add these lines, to make your constructor:

public Item(int id, string name, string namePlural)
{
    ID = id;
    Name = name;
    NamePlural = namePlural;
}

NOTE: If you try to build the solution right now, you’ll see there are errors with the HealingPotion and Weapon classes. These are the classes that are derived from the Item class, and [right now] they don’t have any values to pass to the Item class’ new constructor.

 

Step 3: Open the HealingPotion class, and add these lines to make its new constructor:

public HealingPotion(int id, string name, string namePlural, int amountToHeal)  :  base(id, name, namePlural)
{
    AmountToHeal = amountToHeal;
}

Notice that the HealingPotion constructor has parameters for the one property it has (AmountToHeal) and the three properties it uses from the base class (ID, Name, and NamePlural).

Inside the constructor, it sets the AmountToHeal property to the value that was passed through the amountToHeal parameter.

Also notice that after the constructor has a list of its parameters, it has this piece of code:

:  base(id, name, namePlural)

What that does is take the values from the parameters in the HealingPotion constructor (id, name, and namePlural) and passes them on to the constructor of the Item class. This is how we get parameters into the base class, when instantiating a derived class.

 

Step 4: Edit the Weapon class, by adding this constructor code:

public Weapon(int id, string name, string namePlural, int minimumDamage, int maximumDamage)  :  base(id, name, namePlural)
{
    MinimumDamage = minimumDamage;
    MaximumDamage = maximumDamage;
}

After you add this, you can try rebuilding the solution. Now, since the derived classes are passing the required values to the base class, it will build without any errors.

 

Step 5: Edit the LivingCreature class. Add this constructor code:

public LivingCreature(int currentHitPoints, int maximumHitPoints)
{
    CurrentHitPoints = currentHitPoints;
    MaximumHitPoints = maximumHitPoints;
}

Again, if you try to build the solution now, you’ll get an error that the Monster and Player classes (the classes that derive from LivingCreature) have a problem.

 

Step 6: Edit the Monster class, by inserting this constructor:

public Monster(int id, string name, int maximumDamage, int rewardExperiencePoints, int rewardGold, int currentHitPoints, int maximumHitPoints) :  base(currentHitPoints, maximumHitPoints)
{
    ID = id;
    Name = name;
    MaximumDamage = maximumDamage;
    RewardExperiencePoints = rewardExperiencePoints;
    RewardGold = rewardGold;
}

 

Step 7: Insert this constructor code into the Player class:

public Player(int currentHitPoints, int maximumHitPoints, int gold, int experiencePoints, int level)  :  base(currentHitPoints, maximumHitPoints)
{
    Gold = gold;
    ExperiencePoints = experiencePoints;
    Level = level;
}

Now, if we try building the solution, we get an error. That’s because in the SuperAdventure UI code, we are instantiating a Player object without any parameters. Now that we have a custom constructor in the Player class, we need to use that and pass in the appropriate parameters.

 

Step 8: Right-click on SuperAdventure.cs, in the SuperAdventure project, then select “View Code”. Change line 23 to this:

_player = new Player(10, 10, 20, 0, 1);

Now that we are passing all the values in the constructor, to set the player object’s properties, we can delete lines 25 through 29, where we were setting the properties individually.

You could leave lines 25 through 29. They’ll still work, and they won’t hurt anything. But you’re already setting the properties to those values, so you might as well remove them and make your code a little cleaner.

 

Summary

You’ll probably need to use some amount of inheritance in any decent-sized program. And now you know how to make a custom constructor work in classes that use inheritance.

This may be overkill for the simple game that we’re building. However, you’re going to need to know how inheritance works if you program in C#, or another object-oriented programming language. Hopefully, with small, simple classes like these, it’s easy to understand.

 

Source code for this lesson

Source code on GitHub

Source code on DropBox

 

Next Lesson: Lesson 09.1 – Using your classes as datatypes

Previous lesson: Lesson 08.1 – Setting properties with a class constructor

All lessons: Learn C# by Building a Simple RPG Index

45 Comments

  1. ping an
    ping an January 6, 2025

    Hello Scott Lilly:
    After I completed all the steps in this lesson, the error occurred, and the error message read something like “Cannot implicitly convert type ‘int’ to ‘string’ “.I have uploaded my project, could you please help me to find out what the problem is and how to change it
    I am looking forward to your reply.

    • Scott Lilly
      Scott Lilly January 7, 2025

      I downloaded the “Q-Question” project, but it did not have any error message when I compiled it. Is the code in a different location?

Leave a Reply

Your email address will not be published. Required fields are marked *