Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] Deriving Classes

Started by alexmullins Jun 4, 2006 at 7:25 PM 1 replies 900+ views
Original Post
alexmullins
alexmullins
Hi, I recently moved to C# because i liked the noob friendly syntax. But can some one please tell me why i get these errors: Error 3 'Animal.age' is inaccessible due to its protection level C:\Documents and Settings\Alex Mullins.ALEX-AE87B4CE0A\Desktop\DirectX\directx\ConsoleApplication1\Program.cs 35 85 ConsoleApplication1 Error 4 'Animal.weight' is inaccessible due to its protection level C:\Documents and Settings\Alex Mullins.ALEX-AE87B4CE0A\Desktop\DirectX\directx\ConsoleApplication1\Program.cs 35 90 ConsoleApplication1 here is the source code
[source lang = c#]
using System;

public class Animal
{
    //private member variables
    private int age;
    private int weight;

    public Animal(int age, int weight)
    {
        this.age = age;
        this.weight = weight;
    }

    public void ShowA()
    {
        Console.WriteLine("Animal is {0} and weighs {1}", age, weight);
    }

}

public class Human : Animal
{
    private string species = "Human";
    private string name;

    public Human(int age, int weight, string name) :
        base(age, weight)
    {
        this.name = name;
    }

    public void ShowH()
    {
        Console.WriteLine("{0} is {1} years old and weighs {2} and is a {3}", name, age, weight, species);
    }

}

public class Tester
{
    public static void Main(string[] args)
    {
        Animal a = new Animal(1, 12);
        a.ShowA();

        Human h = new Human(14, 180, "alex");
        h.ShowH();
    }
}



Thank you for any help
Palidine
Palidine
private means only the actual class can access the memer variables. i.e. derived classes cannot access it. If you want child classes to be able to access the variable make it protected.

public: everyone can access
protected: class and derived classes can access
private: class and only class can access

-me
alexmullins
alexmullins
mmk
i forgot all about protected variables :o
thank you for your help

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.