Access Modifiers and Friends
- This is chapter 8. Read it.
- This is a detailed review of classes.
- I am only going to hit the points I feel need emphasized.
- Here is an example of an exception class in a header and a cpp file
- Note,
override
is only used once, in the headers.
- This occurs occasionally with other words as well.
- We will discuss these further soon.
- DansExceptT.h
- DansExceptT.cpp
- Declare members that do not change this as
const
- Access Control
- What does
public
mean?
- Everything can get access to public members.
- What does
private
mean?
- Only instances of the class can access private members.
- Oh, and friends.
- We will discuss
protected
later.
- It relates to inheritance.
- Derived classes are allowed to access protected members of the base class.
- Non-derived classes can not get at protected members of the base class.
- Access controls can be declared multiple times and in any order.
- But it is generally better to keep them grouped.
- Access control can be overcome by
friend
- This is at the beginning of chapter 9
- Declaring a class or function to be a fried overrides the access controls.
- Gregorie says that friends are
- Easy to abuse
- Break encapsulation.
- Scott Meyer is very opposed to friend functions.
- He says that the more "things" that have access to private data the greater the impact if something changes.
- Thus friends provide a huge hole where things can become really broken on a change.
- He comes close to suggesting you never use them.
- Think about changing a private variable name
- How many places should you have to edit to accomplish this
- The class header file.
- The class implementation.
- EVERY friend function/class.
- Meyers says to declare all member data private.
- Notice in these examples, class members are initialized at definition
-
int data {value};
- This has become a preferred way to initialize data.