As we move onward to the C++ style for ADTs, we'll use class
rather than struct
and also use built-in features of the language (i.e. things that C++ adds beyond C) to support good practices in a more robust way. In particular, a class
in C++ gives us:
Member Functions
Both data (i.e. member variables) and behaviors (i.e. member functions) for an ADT are encapsulated as members of a class
.
Member Access Levels
Give public
access to an ADT's interface, e.g. functions we want other parts of our code to call while at the same time restricting internal details like raw member data or helper functions to private
access.
Constructors
Use constructors to ensure ADTs are always initialized (rather than having to remember to separately call an _Init()
function).
1: Warm Up Exercise
1.1
Let's start today with a quick exercise that helps motivate the transition we'll make from C-Style to C++ Style ADTs.
1.1 Exercise: Warm Up
Consider the code here that creates and uses a C-style ADT, specifically the
You're welcome to check your solution with this walkthrough video: |
2: Introduction to Classes and Member Functions
2.1
The C++ builds on the C language by adding language features to formalize several of the patterns for ADTs that we've used so far. We'll call this the "C++ Style" for ADTs and generally switch to using Here's an introduction:
2.1 Exercise:
halfPerimeter()
Consider another member function,
You're welcome to check your solution with this walkthrough video: |
3: Member Access Levels
In C++, you can separate member declarations into different access levels ( (The exercise following the next section on constructors will also incorporate member access levels.) |
4: Constructors
4.1
The C++ style also uses constructors as a formal mechanism for initialization.
4.1 Exercise:
Coffee class
Consider the
Consider each of the following code snippets that we might write in a
You're welcome to check your solution with this walkthrough video: |
5: Implicitly Defined Constructors
5.1
There are a few constructors the compiler may automatically provide for your classes.
5.1 Exercise: Default Constructors
Consider each of the following classes. Are they default-constructible (i.e. can you define a default-initialized variable with that class type)? Why or why not?
You're welcome to check your solution with this walkthrough video: |
6: Composing C++ ADTs (Classes as Members)
6.1
As we did for C-style ADTs, let's take a look at composing more complex ADTs in C++ as classes with other classes for member variables. In this case, we need to ensure that the constructor for the outside class calls each of the constructors for its members (and the compiler double checks this for us).
6.1 Exercise:
Professor Constructors
Here again are the classes from the video:
Consider several possible constructors for the
You're welcome to check your solution with this walkthrough video. (Please accept my apologies for the notification sounds in the video… apparently some group chat of mine was going nuts.) |
7: Best Practices for C++ ADTs
Finally, let's consider some more miscellaneous topics and best practices for writing well-designed classes, including:
|