



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




template
int CoordCmp(coordT c1, coordT c2) { if (c1.x < c2.x) return (-1); else if (c1.x > c2.x) return (1); else if (c1.y < c2.y) return (-1); else if (c1.y > c2.y) return (1); else return (0); } int main() { Vector
Final version of Sort template (from CS106b cmpfn.h) template
Implementing member functions Members of receiver accessible in member function void Time::shiftBy (int dh, int dm) { hour += dh; minute += dm; } Can send other messages to receiver void Time::shiftBy(int dh, int dm) { setHour(hour + dh); setMinute(minute + dm); } Special variable: this (pointer to receiver) void Time::shiftBy(int dh, int dm) { this->hour += dh; this->setMinute(minute + dm); } Maintaining object consistency Setters can constrain to correct range void Time::setHour(int newValue) { if (newValue < 1) hour = 1; else if (newValue > 12) hour = 12; else hour = newValue; } void Time::setMinute(int newValue) { minute = newValue % 60; } What if data members were public? What is advantage of making all access, even within implementation, go through setters? Constructors Special function to init newly created object
Destructors Special function to clean up object