Midterm Review

Thomas Kennedy

Contents:

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?

Let us individually examine each of these.

2 UML: Relationship & Association Review

How would I represent the following in UML?

  1. A Course may be offered in multiple Section-s.

     

  2. A Section is composed of Student-s.

     

  3. Course-s may have linked Recitation-s and Lab-s.

     

  4. Student-s may be required to enroll in one Lecture, one Lab, and one Recitation for a given Course.

    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

  1. When do I need to implement the Big-3?
    1. What is shallow copy?
    2. What is deep copy?
  2. What is const-correctness?
  3. If I define operator<< or operator>> as member functions, what am I defining?
  4. What variable types allow me to make use of inheritance?
    1. value-type?
    2. reference-type?
    3. pointer-type?

3.2 Design

  1. Object-oriented analysis and design is useful even if a program will not be written in an Object Oriented programming language.

  2. When do I need index cards?

    • What am I creating?
    • In which phase of design am I currently engaged?
  3. Initial Design with CRC cards UML Class diagrams

    • Assigning Responsibilities.
    • Listing Collaborators.
    • The A-B-C Rule.
  4. What is inheritance?

    • How do I represent inheritance in UML Class diagrams?
    • What is the name of this association?
  5. 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).

  1. Think about why these parallels exist between languages that support Object Oriented Programming (OOP).
  2. Be able to compare these functions.
  3. Think about RAII and the rule Every Constructor must intialize every attribute (data member).
  4. Think about output in the context of operator<< in C++, toString in Java, __str__ in Python 3, and std::fmt::Display::fmt in Rust.
  5. Think about the C++20 Three-Way Comparison (Spaceship operator).