Cross-Language Class Checklist

Thomas J Kennedy

Contents:

1 Revisiting Cross-Language Class Checklist Rules

Revist our class checklist discussions, 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()
  1. Think about why so many parallels exist between languages that support Object Oriented Programming (OOP).

  2. Think about RAII and the rule Every Constructor must intialize every attribute (data member).

    • We will see this in Java with the deprecated/discouraged finalize and Python with the with context management mechanic.

  3. Think about output in the context of operator<< in C++, toString in Java, __str__ in Python 3, and std::fmt::Display::fmt in Rust.

  4. Think about the C++20 Three-Way Comparison (Spaceship operator).

2 Focusing on C++ & Java

Let us take our Class Checklist Comparison Table and focus only on C++ and Java.

Example 1: C++ & Java Class Checklist Table
C++ Java
Default Constructor Default Constructor
Copy Constructor Clone and/or Copy Constructor
Destructor
finalize (deprecated/discouraged)
Assignment Operator (=)
Accessors (Getters) Accessors (Getters)
Mutators (Setters) Mutators (Setters)
Swap
Logical Equivalence Operator (==) equals
Less-Than / Comes-Before Operator (<) hashCode
Stream Insertion Operator (<<) toString
begin() and end() iterator

If we look at the table four rows (listed in bold) are immediately familiar:

  1. Default Constructor - all objects need to be initialized to some default state.

  2. Copy Constructor & clone - it must be possible to copy all objects.

    clone allows dynamic binding to be leveraged (which is the standard in Java).

  3. Accessors & Mutators - the principles of encapsulation still apply.

The only difference between C++ and Java for each of these items is syntax. The mechanics and conceptual approach are the same (barring a few pedantic differences).


Example 2: C++ & Java Class Checklist Table - The Remainder I
C++ Java
Destructor
finalize (deprecated/discouraged)
Assignment Operator (=)
Swap
Logical Equivalence Operator (==) equals
Less-Than / Comes-Before Operator (<) hashCode
Stream Insertion Operator (<<) toString
begin() and end() iterator

If we strip out the four bolded rows, we are left with nine (9) rows. Let us tackle a few…

  1. Destructor vs finalize - In C++ the destructor guarantees memory deallocation (e.g., calls to delete or delete[]).

    It is also common to shoehorn in some RAII (e.g., closing a file). However, finalize is not equivalent or analogous to Destructors (in the general case).

  2. Assignment Operator - Object variables in Java behave a lot like C++ smart pointers (mostly like std::shared_ptr). Java Garbage collection handles the rest.

  3. Swap - In Java object variables are functionally fancy pointers. A Java swap is conceptaully a pointer swap.


We are left with…

Example 3: C++ & Java Class Checklist Table - The Remainder II
C++ Java
Logical Equivalence Operator (==) equals
Less-Than / Comes-Before Operator (<) hashCode
Stream Insertion Operator (<<) toString
begin() and end() iterator

The next two rows are (in my opinion) more interesting:

 
  1. operator== vs equals - barring a few mechanical differences these two functions are equivalent.

  2. operator< and hashCode are not equivalent. However, they do serve similar purposes in each language’s built-in libraries.

We are now left with:

  1. operator<< vs toString - these are both output functions (albeit with a few subtle differences).

  2. begin and end vs iterator - iterators will be the focus of an upcoming lecture.