But What Goes in a Class?
- On page 598 she introduces the Categories of Abstract Data Type Operations
- Constructor: a function that initializes a new instance of a class.
- this occurs when an instance of the class is created.
- Transformer: a function that changes the member data of a class
- Also known as a setter or a mutator.
- These protect the class to make sure that the data stays in the domain of the class.
- Observer: a function that allows the client to observe the state of the class without changing it.
- Also known as a getter or a accessor
- Destructor: a function that cleans up the state of the class when it is destroyed
- This occurs when an instance of a class goes out of lifetime.
- Iterator: a function that allows the client to step through the data in a class.
- We are implementing a primitive version of an iterator with FirstWord, CurrentWord, NextWord.
- These are for containers
- Some terms
- A class instance: A variable of the class type.
- Declaring a variable of a class instantiates or creates an instance of a class.
- A class member: A component of a class (function or data)
- The class interface: The components of a class that are accessible to a client.
- The methods.
- You usually want this to be fixed, so client code does not need to change if/when the class is modified.
- A class client: the code that declares and manipulates an instance of a class.
- Class state: the value of the member data.
- Scope:
- Lifetime:
- Classes have the same limitations as structs
- No direct comparison
- No math
- No direct I/O
- But you can define these for classes (and structs).
- Class Scope
- Members (data and functions) are available to all other members.
- See this reference
- Member Access
- We have used
public
and private
member access.
- When a member is declared public, clients have access to this member.
- When a member is declared private, clients do not have access to this member.
- There is also a
protected
class, but that is for 330.
- Unless there is a very good reason, all data should be private.
- If passed an instance of the same class, a class instance has access to the passed instances private (and protected) members.
- Constructor and destructor.
- As we said the constructor is called when the class is created.
- And the destructor is called when the class goes out of lifetime.
- Take a look at classBasics.cpp
- For an ADT to truly work you need to keep implementation separate from specification
- This is perhaps not 100% enforceable, but header and implementation files partially accomplish this.
- Encapsulation: Designing a class so that its attributes are isolated from the actions of external code except through the formal interface.
- Abstraction Barrier: The invisible wall around an object that encapsulates implementation details. This barrier can only be breached through the public interface.
- Information hiding: the encapsulation and hiding of implementation details to keep the user of an abstraction from depending on or incorrectly manipulating these details.