上海交通大学交大密西根 联合学院·一 ◆ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Accessors Defined for every private date member if it need to be accessed by other objects or functions. ·Definition typeOfthe Variable getVariableName_( { return VariableName;
Accessors Accessors • Defined for every private date member if it need to be accessed by other objects or functions. • Definition typeOftheVariable getVariableName () { return VariableName; }
Accessors (Examples) 。PointT: double getx (freturn x;} double getY (freturn y;) 。PenT: double getx (return location.getx(;} double getY (return location.getYO;} const string &getColor freturn color;} ·MouseT: double getx {return x;} double getY (return y;}
Accessors Accessors (Examples) (Examples) • PointT: – double getX () {return x;} – double getY () {return y;} • PenT: – double getX () { return location.getX();} – double getY () { return location.getY();} – const string &getColor () {return color;} • MouseT: – double getX () {return x;} – double getY () {return y;}
上海交通大学交大密西根 联合学院·一 ◆ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Accessors (Examples) GwindowsT: double getWidth (const {return width; double getHeight const {return height;) const string &getColor const {return color;} const string &getTitle()const (return title;}
Accessors Accessors (Examples) (Examples) • GwindowsT: – double getWidth () const {return width;} – double getHeight () const {return height;} – const string &getColor () const {return color;} – const string &getTitle() const {return title;}
上海交通大学交大密西根 联合学院一 ◆ 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Mutators If you allow user to change the value of private variable,you should define a mutate function for it. 。Definition void setVariableName (typeOfthe Variable new Value) VariableName new Value;
Mutators Mutators • If you allow user to change the value of private variable, you should define a mutate function for it. • Definition void setVariableName (typeOftheVariable newValue) { VariableName = newValue; }
上海交通大学交大密西根 ·联合学院一 181 UM-SJTU Joint Institute University of Michigan Shanghal Jiao Tong University Mutators (Examples) PointT: void setx (const double x); void setY (const double y); ·PenT: void setx (double x); void setY (double y); void setColor (const string clr); 。MouseT: The location for mouse is read only.It is therefore no mutate functions are defined for it
Mutators Mutators (Examples) (Examples) • PointT: – void setX (const double x); – void setY (const double y); • PenT: – void setX (double x); – void setY (double y); – void setColor (const string clr); • MouseT: – The location for mouse is read only. It is therefore no mutate functions are defined for it