object - C# cannot override inherited member -
i'm learning c# book named chegwidden gladdis. i'm making same program , same code written in book. there problem. can't override method parent class. had readed book start of chapter, 5 times, everyhing same can't figure out why can't can't override method parent class. here's code base class passfailactivity.cs
using system; namespace protectedmembers { public class passfailactivity : gradedactivity2 { private double minpassingscore; // minimum passing score /// <summary> /// constructor sets minimum passing score /// </summary> /// <param name="mps">the minimum passing score.</param> public passfailactivity(double mps) { minpassingscore = mps; } /// <summary> /// getgrade method returns letter grade determined /// score field. methos overrides base class method. /// </summary> /// <returns>the letter grade</returns> public override char getgrade() { char lettergrade; if (base.getscore() >= minpassingscore) lettergrade = 'p'; else lettergrade = 'f'; return lettergrade; } } }
and gradedactivity2.cs
using system; namespace protectedmembers { public class gradedactivity2 { protected double score; // numberic score /// <summary> /// setscore method sets score field. /// </summary> /// <param name="s">the value store in score</param> public void setscore(double s) { score = s; } /// <summary> /// getscore method returns score. /// </summary> /// <returns>the value stored in score field</returns> public double getscore() { return score; } /// <summary> /// getgrade method returns letter grade determined score field. /// </summary> /// <returns>return letter grade</returns> public char getgrade() { char lettergrade; if (score >= 90) lettergrade = 'a'; else if (score >= 80) lettergrade = 'b'; else if (score >= 70) lettergrade = 'c'; else if (score >= 60) lettergrade = 'd'; else lettergrade = 'f'; return lettergrade; } } }
and passfailexam
using system; namespace protectedmembers { public class passfailexam : passfailactivity { private int numquestions; // number of questions private double pointseach; // points each question private int nummissed; // number of questions missed /// <summary> /// constructor sets number of questions, number /// of questions missed, , minimum passing score. /// </summary> /// <param name="questions">the number of questions</param> /// <param name="missed">the number of questions missed</param> /// <param name="minpassing">the minimum passing score</param> public passfailexam(int questions, int missed, double minpassing) : base(minpassing) { // declare local variable score. double numericscore; // set numquestions , nummissed fields. numquestions = questions; nummissed = missed; // calculate points each questions , numeric score exam. pointseach = 100.0 / questions; numericscore = 100.0 - (missed * pointseach); // call base class's setscore method set mumeric score. setscore(numericscore); } /// <summary> /// getpointseach method returns number of points each questions worth /// </summary> /// <returns>the value in pointseach field</returns> public double getpointseach() { return pointseach; } /// <summary> /// getnummissed method returns number of questions missed /// </summary> /// <returns>the value in nummissed field</returns> public int getnummissed() { return nummissed; } } }
and here main
using system; namespace protectedmembers { public class passfailexamdemo { public static void main111() { int questions, // number of questions missed; // number of questions missed double minpassing; // minmum passing score // number of questions on exam console.write("how many questions " + "on exam? "); questions = convert.toint32(console.readline()); // number of questions missed. console.write("how many questions did " + "the student miss? "); missed = convert.toint32(console.readline()); // minimum passing score console.write("what minimum " + "passing score? "); minpassing = convert.toint32(console.readline()); // create passfailexam project passfailexam exam = new passfailexam(questions, missed, minpassing); // display teset results. console.writeline("each questions counts {0} points.", exam.getpointseach()); console.writeline("the exam score {0} ", exam.getscore()); console.writeline("the exam grade {0} ", exam.getgrade()); console.readline(); } } }
the output should be
how many questions on exam? 100 how many questions did student miss? 25 minimum passing score? 60 each question counts 1 points. exam score 75 exam grade p
i have tried make base method virtual , calling override when trying override , gets me error "'protectedmembers.passfailactivity.getgrade()': cannot override inherited member 'protectedmembers.gradedactivity2.getgrade()' because not marked virtual, abstract, or override". checked
the error self-explanatory, mark function virtual
in gradedactivity2
:
public virtual char getgrade()
from msdn:
the virtual keyword used modify method, property, indexer, or event declaration , allow overridden in derived class. example, method can overridden class inherits it:
Comments
Post a Comment