A classy solution
Task 1, How will I store the word information?
Structures are nice, but
They do not automatically initialize themselves
The user (other programmer) can mess with the data fields
And I have no protection to make sure they don't mess them up.
For example, they might not like "dan" as a word, so they set the count to be -100
And the functions are not tightly grouped with the data.
A better solution is a class.
Classes
Have member data - or data fields just like structures.
They also have member functions.
These are the basic operations you can perform on that data.
We will deal with some theory soon, but let's make a
WordT
class
One bit of theory
An
Abstract Datatype
is a data type whose properties (domain and operations) are specified independently of any particular implementation. (P 596)
In C++ classes are used to implement ADT's
For our WordT
What is the domain, or what data will we store
The word, a string.
The number of times we have seen that word, probably a size_t
What are the operations?
Set and get the word
Increment the count
Get the count.
These are generally classified as
Constructors
Getters or Observers
Setters or Transformers
Class syntax
Classes have
access modifiers
These are instructions that tell the compiler "who" has access to the members.
These are either public, private, or protected.
Ignore protected for now.
Public means anyone can access the member
Private means only instances of the class can access the member.
By default everything in a class is private.
But we will always specify modifiers.
Unless you have a good reason, all data is private.
Provide member functions to get and set the data.
Finally the syntax
class identifier { access modifier: member list access modifier: member list };
In this case, a member list is
function prototypes
variable declarations.
Class Scope
When inside of a class (member function) you have access to all other members.
Data and functions.
To place a function in the class scope use the scope operator.
Let's build the WordT as an example.