Cross-Language Class Checklist
Thomas J Kennedy
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() |
-
Think about why so many parallels exist between languages that support Object Oriented Programming (OOP).
-
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 thewith
context management mechanic.
-
-
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).
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()
andend()
iterator
If we look at the table four rows (listed in bold) are immediately familiar:
-
Default Constructor - all objects need to be initialized to some default state.
-
Copy Constructor &
clone
- it must be possible to copy all objects.clone
allows dynamic binding to be leveraged (which is the standard in Java). -
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()
andend()
iterator
If we strip out the four bolded rows, we are left with nine (9) rows. Let us tackle a few…
-
Destructor vs
finalize
- In C++ the destructor guarantees memory deallocation (e.g., calls todelete
ordelete[]
).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). -
Assignment Operator - Object variables in Java behave a lot like C++ smart pointers (mostly like
std::shared_ptr
). Java Garbage collection handles the rest. -
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()
andend()
iterator
The next two rows are (in my opinion) more interesting:
-
operator==
vsequals
- barring a few mechanical differences these two functions are equivalent. -
operator<
andhashCode
are not equivalent. However, they do serve similar purposes in each language’s built-in libraries.
We are now left with:
-
operator<<
vstoString
- these are both output functions (albeit with a few subtle differences). -
begin
andend
vsiterator
- iterators will be the focus of an upcoming lecture.