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.

Naming class membersChapter 13: Making Classes Work 171A member function is a lot like a member of a family. The full name of thefunction addCourse(int, float) is Student::addCourse(int, float),just as my full name is Stephen Davis. The short name of the function isaddCourse(int, float), just as my short name is Stephen. The class nameat the beginning of the full name indicates that the function is a member ofthe class Student. (The :: between the class name and the function name issimply a separator.) The name Davis on the end of my name indicates that Iam a member of the Davis family.Another name for a full name is extended name.You can define an addCourse(int, float) function that has nothing to dowith Student — there are Stephens out there who have nothing to do withmy family. (I mean this literally: I know several Stephens who want nothing todo with my family.)You could have a function Teacher::addCourse(int, float) or evenGolf::addCourse(). A function addCourse(int, float) without a classname is just a plain ol’ conventional non-member function.The extended name for the non-member function is ::addCourse(int,float). (Note the colon without a family name in front.)Calling a Member FunctionBefore you look at how to call a member function, remember how to access adata member:class Student{public:int semesterHours;float gpa;};Student s;void fn(void){// access data members of ss.semesterHours = 10;s.gpa = 3.0;}

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

Saved successfully!

Ooh no, something went wrong!