11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

204Part III: Introduction to ClassesA class member can also be protected by declaring it private. In this book,I use the protected keyword exclusively since it expresses the more genericconcept.Making an Argument for UsingProtected MembersNow that you know a little more about how to use protected members in anactual class, I can replay the arguments for using protected members.Protecting the internal state of the classMaking the gpa member protected precludes the application from setting thegrade point average to some arbitrary value. The application can add courses,but it can’t change the grade point average.If the application has a legitimate need to set the grade point average directly,the class can provide a member function for that purpose, as follows:class Student{public:// same as beforefloat grade(){return gpa;}// here we allow the grade to be changedfloat grade(float newGPA){float oldGPA = gpa;// only if the new value is validif (newGPA > 0 && newGPA

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!