Cheatsheet: Data Structures
    Array / List
    Map / Dict
    Set
    Tuple
    Struct / Data Class
    Stacks
    Other Data Structures
    Collection Conversions
Cheatsheet: Control Flow
Cheatsheet: Class

Polyglot CheatSheet - Class

Updated: 2022-04-04
  • There is no way to write a Java program without a class.
  • Rust does not have the keyword class, instead it is achieved by struct + impl.

Constructor / Destructor

C++

class Foo {
 public:
  Foo(const Foo& arg);             // copy constructor
  Foo(Foo&& arg);                  // move constructor
  Foo& operator=(const Foo& rhs);  // copy assignment operator
  Foo& operator=(Foo&& rhs);       // move assignment operator

  ~Foo(); // destructor
}

Rust

Constructor:

struct Foo {
  my_field: isize
}

impl Foo {
  // constructor, by convention
  pub fn new(field: isize) -> Foo {
    Foo { my_field: field }
  }
}

Destructor:

fn drop(&mut self);

Java

public class Foo {
  private final int myField;

  // constructor
  public Foo(final int field) {
    this.myField = field;
  }
}

Python

  • constructor: __init__()
  • destructor: __del__(); called when all references to the object have been deleted i.e when an object is garbage collected.

Methods

Rust

struct doesn’t support methods, use the impl keyword

Inheritance

JavaScript

class Foo extends Parent {}

Python

Supports multiple inheritance.

class Foo(Parent1, Parent2):