1
Answer

Hello, i need help with derived classes

Shane

Shane

16y
2k
1

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!

Answers (1)