c# - Polymorphism - exercise -
alright, exercise:
define class named student, containing 3 grades of students. class have function calculates grades average. now, define class named student1 derived student , add function calculate sum of grades.in main program, define student variable , object of type student1. perform placement of object variable , run function of student1.
note: not homework, i'm learning own. code:
class student { protected int grade1, grade2, grade3; public student(int grade1, int grade2, int grade3) { this.grade1 = grade1; this.grade2 = grade2; this.grade3 = grade3; } public double average() { return (grade1 + grade2 + grade3) / 3; } } class student1 : student { public student1(int grade1, int grade2, int grade3) : base(grade1, grade2, grade3) { } public double sum() { return grade1 + grade2 + grade3; } } class program { static void main(string[] args) { } }
i don't know in main class, how perform placement , also, wanted know what's benefit of doing it, let me know if have mistakes far, alot.
ok: guess they're looking for, although english little ropey:
1) declare student variable
student s;
2) declare student1 object
student1 s1 = new student1(1,2,3);
3) perform placement of object variable:
s = s1;
4) run function (note you'll have cast s student1 type access type specific function sum)
console.writeline(((student1)s).sum());
Comments
Post a Comment