Lesson11-Person-Student-Case
PERSON CLASS
using System;
namespace Lesson11InheritanceApp
{
//public partial class Person :IComparable<Person>
publicpartialclassPerson
{
//data members
privatestringlastName = "n.a";
privatestringfirstName= "n.a";
privateint age = 0;
privatestring occupation = "n.a";
//Properties
// Verbose style - Property for last name
publicstringLastName
{
get
{
returnlastName;
}
set
{
lastName = value;
}
}
// Abbreviated style - C# 7.0 - Expression Bodied Method notation
publicstringFirstName { get => firstName; set => firstName = value; }
publicint Age { get { return age; } set => age = Math.Abs(value); }
publicstring Occupation { get { return occupation; } set => occupation = value; }
// Constructor with zero arguments
public Person()
{
}
// Constructor with four arguments
public Person( stringlNameValue,
stringfNameValue, intageValue)
{
LastName = lNameValue;
FirstName = fNameValue;
Age = ageValue;
}
//user-defined methods (new formatting feature C# 7.0)
publicoverridestringToString()
{
returnstring.Format( $"Person [ "
+ $"First= {FirstName} Last= {LastName} "
+ $"Age= {age} "
+ $"Ocuppation= {Occupation} ]");
}
//a virtual method could be overriden by its derived sub-classes
publicvirtualintGetSleepAmount()
{
return 8;
}
//demo: use only if person is abstract
publicvirtualvoidSetHobby(stringactivityValue) { }
publicvirtualvoidRaiseSalary(doubleincrementValue) { }
//public virtual intCompareTo(Person other)
//{
// if (this.age == other.age) return 0;
// else if (this.ageother.age) return 1;
// else return -1;
//}
}
}
PERSON (PART 2)
namespace Lesson11InheritanceApp
{
partialclassPerson
{
publicstringfavoriteTypeOfMusic;
}
}
STUDENT CLASS
namespace Lesson11InheritanceApp
{
publicclassStudent : Person, IComparable<Student>
{
//data members
privatestring major = "not declared yet";
privatedoublegpa = 0.0;
privateintstudentId = 0;
//properties (bodied expressions)
publicstring Major { get => major; set => major = value; }
publicdoubleGpa { get => gpa; set => gpa = value; }
publicintStudentId { get=> studentId; set=> studentId = value; }
//constructors
public Student() :base() { }
public Student(stringlNameValue,stringfNameValue, intageValue,
intidValue, stringmajorValue, doublegpaValue)
:base(lNameValue, fNameValue, ageValue)
{
StudentId = idValue;
Major = majorValue;
Gpa = gpaValue;
}
//user-defined methods
publicoverridestringToString()
{
returnstring.Format($"\nStudent [ ID= {StudentId} Major= {Major} GPA= {Gpa}" +
$"\n\t" + base.ToString() +
$" ]");
// base.ToString()
}
publicoverrideintGetSleepAmount()
{
//return base.GetSleepAmount();
return 6;
}
publicoverridevoidSetHobby(stringactivityValue)
{
List<stringlistHobbies = new List<string>();
//assume List<string> listHobbieshas been added to data members
listHobbies.Add(activityValue);
}
publicsealedoverridevoidRaiseSalary(doubleincrementValue)
{
//some logic here
}
publicintCompareTo(Student other)
{
//return this.LastName.CompareTo(other.LastName);
//ordering by GPA
if (this.Gpa == other.Gpa) return 0;
elseif (this.Gpaother.Gpa) return 1;
elsereturn -1;
}
}
}
PROGRAM
namespace Lesson11InheritanceApp
{
classProgram
{
staticvoid Main(string[] args)
{
Person p1 = newPerson("Prince", "Diane", 22);
Console.WriteLine(p1);
Person p2 = newPerson();
Console.WriteLine(p2);
//if (p1.CompareTo(p2)!=0)
//{
// Console.WriteLine("Different people!");
//}
//List<Person> list = new List<Person>() { p1, p2, new Person("Potter", "Harry", 13) };
//list.Sort();
//foreach (Person p in list)
//{
// Console.WriteLine(p);
//}
Student s1 = newStudent();
Console.WriteLine(s1); ;
s1.FirstName = "Hermione";
s1.LastName = "Granger";
s1.LastName = "MGCA";
s1.Age = 14;
s1.Gpa = 4.00;
Console.WriteLine(s1);
Student s2 = newStudent("Parker", "Peter", 21, 2001234, "CHEM", 3.8);
Console.WriteLine(s2);
Console.WriteLine("Sleep " + s2.GetSleepAmount());
List<Student> listStud = new List<Student>() { s1, s2 };
listStud.Sort();
listStud.Reverse();
Console.WriteLine("\n\nSORTED STUDENT LIST");
foreach(Student s inlistStud)
{
Console.WriteLine(s);
}
Console.Read();
}
}
}
CONSOLE