2
Reply

Student in need of help with inheritance.

William McHugh

William McHugh

Sep 14 2008 6:06 PM
2.8k
I am a student at Westwood college, and I am having trouble understanding inheritance in C#

I understand all of the parent class and child class rules, and the syntax. What has really been giving me issues is trying to pass control from one child class to another.
Perhaps I should elaborate:

I have my base class "character" and three derived classes "warrior", "mage", and "thief." Each of these classes has a special void function only it can access, like determining strength for the warrior, intellect for the mage, and dexterity for the thief. I am trying to figure out how to pass control from an void function input, where the user is given a choice between the three chracters, makes a choice, and then control passes to that chracter for the remainder of the program.

Ill post my code, it is very short as right now it is just in a command window, I havent added the GUI yet.
//charcter.cs
using System;

public class character
{
    public int attribute;
    public int healthPoints;
    public string name;
    public string species;
    public char userChoice;

    public void BaseCharacter(int atr, int hp, string nm, string spc)
    {
        attribute = atr;
        healthPoints = hp;
        name = nm;
        species = spc;
    }

    public int Attributes
    {
        get
        {
            return attribute;
        }
        set
        {
            attribute = value;
        }
    }

    public int HealthPoints
    {
        get
        {
            return healthPoints;
        }
        set
        {
            healthPoints = value;
        }
    }

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }

    }

    public string Species
    {
        get
        {
            return species;
        }
        set
        {
            species = value;
        }
    }

    public void calcPoints()
    {
        Random randomNumbers = new Random();
        healthPoints = randomNumbers.Next(100);
    }

    public void display()
    {
        Console.WriteLine("Welcome to Heroes BETA.");


    }

    public void nameDisplay()
    {
        Console.WriteLine("Please type your name: ");
        name = Console.ReadLine();
        Console.WriteLine("Hello {0}", name);

    }

}//end character.cs

//warrior .cs is empty at the moment

//main.cs
sing System;

public class main
{
    public static void Main(string[] args)
    {
        character warChar = new character();

        warChar.display();
        warChar.nameDisplay();

        Console.WriteLine("Please choose your character.\n'w' for warrior.\n'm' for mage.\n't' for thief.");
        Console.ReadLine();
        if (Console.ReadLine() = 'w')
            character warChar = new character();//error CS1023
       
    }
      
    Any help to just give me the syntax for something  like this would be greatly appreciated!
      

}

Answers (2)