Language Specific Class Checklists
Thomas Kennedy
We will start by revisiting the C++ class checklist, mapping its parts into Java and Python 3.
1 Language Specific Class Checklists
Every semester I draw a table on the whiteboard. I last drew this table on 16 October 2018, and discussed the C++ class checklist and how each item compares to Java and Python 3.
It was obvious that the potato used to take the photo was not properly calibrated. Consider the following table…
C++ | Java | Python 3 |
---|---|---|
Default Constructor | Default Constructor | __init__ |
Copy Constructor | Clone and/or Copy Constructor | __deepcopy__ |
Destructor | ||
finalize (deprecated/discouraged) | __del__ |
|
Assignment Operator (=) | ||
Accessors (Getters) | Accessors (Getters) | Accessors (consider @property ) |
Mutators (Setters) | Mutators (Setters) | Setter (consider @attribute.setter ) |
Swap | ||
Logical Equivalence Operator (==) | equals | __eq__ |
Less-Than / Comes-Before Operator (<) | hashCode | __hash__ |
Stream Insertion Operator (<<) | toString | __str__ |
__repr__ |
||
begin() and end() |
iterator |
__iter__ |
You should recall from your previous coursework that not all accessors are getters and not all mutators are setters. The accessor and mutator rows deal with getters and setters, respectively.
Let us expand the table to include one more language… Rust.
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() |
-
The Python
__str__
method generates human readable output (much like the JavatoString
method). -
There is no analogue in C++ or Java to Python’s
__repr__
. The__repr__
method is used for debugging. It, by definition, should generate an unambiguous string representation.
We are not going to look in Java or Python in detail… Yet!