Midterm Review
Thomas Kennedy
1 Example: No Explicitly Defined Constructors
We are not referring to the explicit
keyword.
A smart aleck on your team has written a class, Cookbook. He has failed to declare a constructor–i.e., he has declared zero constructors. How would you persuade him that an explicitly declared Default constructor is necessary?
- Declare a Cookbook variable
- Declare an array of Cookbooks
- Create a specialized Cookbook–i.e., inherit from Cookbook
- Create function that takes a Cookbook passed by value
- Create function that takes a Cookbook passed by reference
- Add a constant data member to CookBook
- Ask which data structure he used
Let us individually examine each of these.
-
Declare an array of Cookbooks
Cookbook cookbooks[10];
or
Cookbook* cookbooks = new Cookbook[10];
-
Declare a Cookbook variable
Cookbook cookbook;
-
Create a specialized Cookbook–i.e., inherit from Cookbook
class SimpleCookbook : Cookbook { // Some code // ... };
-
Create function that takes a Cookbook passed by reference
bool teachMeToCook(Cookbook &manual);
-
Add a constant data member to CookBook
class Cookbook { private: const int something; // original code // ... };
-
Ask which data structure he used–e.g.,
vector<Recipe>
vector<Recipe*>
Linked List
Array
2 UML: Relationship & Association Review
How would I represent the following in UML?
-
A
Course
may be offered in multipleSection
-s. -
A
Section
is composed ofStudent
-s. -
Course
-s may have linkedRecitation
-s andLab
-s. -
Student
-s may be required to enroll in oneLecture
, oneLab
, and oneRecitation
for a givenCourse
.This is not a relationship that can be meaningfully captured with UML class diagrams. This scenario is more suited for UML Sequence diagrams. This type of scenario will not be present on the Midterm Exam.
3 Questions to Consider
3.1 Coding
- When do I need to implement the Big-3?
- What is shallow copy?
- What is deep copy?
- What is const-correctness?
- If I define
operator<<
oroperator>>
as member functions, what am I defining? - What variable types allow me to make use of inheritance?
- value-type?
- reference-type?
- pointer-type?
3.2 Design
-
Object-oriented analysis and design is useful even if a program will not be written in an Object Oriented programming language.
-
When do I need index cards?
- What am I creating?
- In which phase of design am I currently engaged?
-
Initial Design with
CRC cardsUML Class diagrams- Assigning Responsibilities.
- Listing Collaborators.
- The A-B-C Rule.
-
What is inheritance?
- How do I represent inheritance in UML Class diagrams?
- What is the name of this association?
-
What are aggregation and composition?
- How do I represent aggregation in UML?
- How do I represent composition in UML?
- How are aggregation and composition different?
3.3 Cross-Language Class Checklist Rules
Consider our class checklist discussion, specifically the comparison of C++, Java and Python 3.
C++ | Java | Python 3 | Rust |
---|---|---|---|
Default Constructor | Default Constructor | __init__ |
new() or Default trait |
Copy Constructor | Clone and/or Copy Constructor | __deepcopy__ |
Clone trait |
Destructor | |||
finalize (deprecated/discouraged) | __del__ |
Drop trait | |
Assignment Operator (=) | |||
Accessors (Getters) | Accessors (Getters) | Accessors (@property ) |
Accessors (Getters) |
Mutators (Setters) | Mutators (Setters) | Setter (@attribute.setter ) |
Mutators (setters) |
Swap | |||
Logical Equivalence Operator (==) | equals | __eq__ |
std::cmp::PartialEq trait |
Less-Than / Comes-Before Operator (<) | hashCode | __hash__ |
std::cmp::PartialOrd trait |
Stream Insertion Operator (<<) | toString | __str__ |
std::fmt::Display trait |
__repr__ |
std::fmt::Debug trait | ||
begin() and end() |
iterator |
__iter__ |
iter() and iter_mut() |
We can expand this table to include other languages (e.g., Rust or C++20).
- Think about why these parallels exist between languages that support Object Oriented Programming (OOP).
- Be able to compare these functions.
- Think about RAII and the rule Every Constructor must intialize every attribute (data member).
- Think about output in the context of
operator<<
in C++,toString
in Java,__str__
in Python 3, andstd::fmt::Display::fmt
in Rust. - Think about the C++20 Three-Way Comparison (Spaceship operator).