SD6502 Programming Person
SD6502 Programming II 1Lab Work
continued from last lab(week 5)
Task 1:Inheritance
- Let’s add a few more classes to the PolytechLibrary which you created in last lab session.(a) Add a class and name it Teacher.cs(b) Add attributes(fields) such as FirstName, LastName, Email Address and Subject as properties.(c) Add another class name it Student.cs (d) Add some fields(properties), FirstName, LastName . . . .Wait, didn’t you add same fields already for teacher class. Teacherand students have things in common such as firstname, lastname, email address and so on. Unlike Teacher, students do not havesubject to teach. We can make use of a OO concept called inheritance here. What we can do is definea class, say person, and put all the common fields (attributes) there. Then make Student and Teacher classes inherit all the common attribute from Person class. Of course, we will add the unique fields oftheir own in their class definition.
- Add a class person
- Add common attributes to Person class and remove those from Teacher and student classpublic class Person {
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
Also,
Teacher and Student should now look like this,
public class Teacher : Person {
public string Subject { get; set; }
}
public class Student : Person {
}
Note: The class names Teacher and Student is followed by a colon and then Person. The Personclass is called base or parent class. Similarly Teacher and student classes are called derived or childor subclass. Also, if you are working with VS 2022 replace internal keyword public. VS2022/.Netframework 6 automatically adds internal keyword when you add a new class file.
.SD6502 Programming II 2
One other thing, if you don’t want others who are using the 代 写SD6502 Programming II 1Lab PolytechLibrary to be able to use thePerson class directly(by directly I mean creating an object and using it from their code) you can makeperson class abstract. In this way you are making others to use Teacher and Student class directlyfrom their code.
- Add a keyword abstract before the class keyword in the definition of person classpublic abstract class Person {
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
Let’s add some attributes(feilds) to the empty Student class. A attribute called GradLevel to showwhat level they are studying in. What type of data a GradLevel would hold ? In our polytech wehave level 5, level 7 and level 8 graduates. How would you model this fixed but discrete set ofdata ? Enumeration datatype are perfectly suited in this situation. So let’s define a enum and call itGradlevels. Also, add a property to get and set Gradlevels field.
public class Student: Person {
public enum GradLevels {Level_5, Level_7, Level_8, Level_9 }
public GradeLevels GradLevel {get; set; }
}
Task 2: Working with derived class objects
Let’s go back to our PolytechFormApp. Double click the button to get the source code.Try making it a new Person, i.e. create an object of Person class. Did you get any error why?Create Teacher and Student objects and try and access all the fields.
Task 3:Abstract methods
Abstract classes are classes that are designed such that you can’t instantiate them or use them directly. The
same is true for abstract methods. An abstract method is a method with no code in it. Let’s say, for example,we want to implement a method that will allow us to get a grade point average, but it’s going to be a differentcomputation depending on whether you’re talking about the teacher or the student. The student is going tohave the average of all the grades for the semester or the period or whatever time period we’re talking aboutand the teacher’s grade point average is going to bethe average grade point average for all of the studentsin this class. So it makes sense to have two different, completely different implementations of a function bythe same name. So create the abstract method and then override it in each of the subclasses with specific
mplementations for those classes.
.SD6502 Programming II 3
public abstract float ComputeGradeAverage ( ) ;
In Student class
public override float ComputeGradeAverage ( ) {
return 4.0f;
}
In Teacher class
public override float ComputeGradeAverage ( ) {
//TODO: fix the implementation later
return 0.0f;
}
Note: If a method(s) is declared abstract in base class all derived classes must have its definition (im
plementation). In this case method ComputeGradeAverage must be implemented by Student and Teacherclasses. Otherwise, the code won’t compile. Also, note thatyou can also declare any method abstract innon-abstract class.
Task 4: Testing the abstract method
Now that you have an abstract method and an overridden version of it in our two subclasses, why don’t wetake a look at what it looks like in our test program ?Add two buttons and write code to test the overridden version of ComputeGradeAverage from bothclasses.
Task 5:
Do all the examples exercise from Lecture slides. Codes are given here in the folder, try to understand:
- How base and derived classes work in C#
- How polymorphism is achieved in C#
- What does extension methods do in C# and why it is useful?
Task 5:
Start working on your assignment.
Submission
NO submission needed. You will continue this work in the next lab. Show to your lab instructor when youcomplete week 7 lab.Credits: C# essentials, Bruce Van Horn.
.