05 Abstraction 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

C++ Programming

Abstraction 1

Mostafa S. Ibrahim
Teaching, Training and Coaching since more than a decade!

Artificial Intelligence & Computer Vision Researcher


PhD from Simon Fraser University - Canada
Bachelor / Msc from Cairo University - Egypt
Ex-(Software Engineer / ICPC World Finalist)
Rectangle as a struct
● Let’s design a rectangle as an object
○ What are potential data members?
○ What are potential member functions?

Img Src
Rectangle as a class
Accessor & mutator functions
● Accessors = Getters, Mutators = Setters
● An accessor is a class function used to access/read data members
○ We used GetWidth as an accessor
● A mutator is a class method used to mutate/change data members.
○ We used SetWidth as a mutator
● Your IDE may provide you with generation for them (e.g. as in eclipse)
Accessor & mutator: Eclipse
● Put cursor in the class, then click Alt+Shift+S ⇒ Menu appear
○ Select from menu: Generate setters and getters
○ Decide which setters & getters should be given
Accessor & mutator: Eclipse
● You can also change style of generation (e.g. getWidth or GetWidth)
● Go to Eclipse → Window →Preferences
Accessor & mutator: The proper way?
● There is a debate around setters and getters
○ Are they evil or not? Should we avoid as possible? Really break OOP encapsulation?
● Consider the following tips
○ Don’t be the guy who generate setters & getters for every field.
■ For setters: This can cause problems. E.g. Inject wrong data through setters
■ For getters: Exposing implementation details can cause code issues
○ Think twice what to expose with non-data-only objects
○ “Don’t ask for the information you need to do the work; ask the object that has the information
to do the work for you.” Allen Holub
“Acquire knowledge and impart it to the people.”

“Seek knowledge from the Cradle to the Grave.”

You might also like