Skip to content

Hello, i need help with derived classes

Shane

Asked Feb 13, 2008, 7:57 PM EST1 answer
Share
LinkedInX

hi, new to these forums, sorry if this is in the wrong place.

so im almost done with my code, all i need to do now is code a Main method to test the Person class.  Declare an array in the Main method that stores instances of the Person class as elements.  Demonstrate the functionality of the Person class using the array.

 

How do i test the person class using arrays? I have it set so that only the derived class can directly get/set it's inherited state variables and prevent outside classes from directly getting/setting them.

 

Remember Main method defines and initializes an array of objects from the base class.  array will test the functionality of the class.

 

heres the code

C#
---------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
public class Person // Person class
{
private string name; // class constructors
private string address;
private int age;
public Person(string n, string a)
{
name = n;
address = a;
}
public Person(int aG)
{
age = aG;
}
protected string Name
{
get { return name; } // accessor/mutator property
set { name = value; }
}
protected string Address
{

get { return address; } // accessor/mutator property

C#
set { address = value; }
}
protected int Age
{
get { return age; } // accessor/mutator property
set { age = value; }
}
public void Speak(string n, int aG) //Speak Method
{
Console.WriteLine("My name is " + n + " I am " + aG + " years old");
}
public void Rest(string a) // Rest Method
{
Console.WriteLine("He Sleeps at " + a + " his address");
}

~Person() { } // Destructor

C#
public class Actor : Person // Derived class
{
private string role;
private string movieName;
private double salary;
public Actor(string hisRole, string movie)
: base(hisRole, movie)
{
hisRole = role;
movie = movieName;
}

public string Act(string n, string a) // Act method. This where i test out accessing instances in Person class
{
string name;
name = "Steve";
address = "123 fake street";
Console.WriteLine("Hi im an actor.");
return name;

}
public int Complian(int aG) // Complain class
{
int age;
age = (20);
Console.WriteLine("Bla bla bla");
return age;
}
}
}

public class TestPersonClass
{
public static void Main()
{

}
}

---------------------------------------------------------------------------------------------

 

let me know if this makes any sense!

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    Well, thanks for the help, i figured it out lol

    I changed much of the code. Changed the mutators to public and made the class constructors protected.

     

    heres the Main() method now

    Code Snippet

    C#
    public class Test
    {
    public static void Main(string[]args)
    {
    Person[] personArray = new Person[5];
    for (int i = 0; i < 5; i++)
    personArray[i] = new Person(i);
    personArray[0].Name = "Steve Zimmerman";
    Console.WriteLine(personArray[0].Name);
    personArray[1].Address = "1234 fake street";
    Console.WriteLine(personArray[1].Address);
    personArray[2].Age = 20;
    Console.WriteLine(personArray[2].Age);
    personArray[3].Speak();
    personArray[4].Rest();
    }
    }
    0