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
---------------------------------------------------------------------------------------------
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 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 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!
ShanePosted Feb 14, 2008, 6:21 PM
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
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();
}
}