Code Dictionary: C, C++, C#, Python, Lua, JavaScript
Concept | C | C++ | C# | Python | Lua | JavaScript |
---|---|---|---|---|---|---|
Include/import | #include <stdio.h> |
#include <iostream> |
using System; |
import math |
require "math" |
import fs from "fs" |
Define constant | #define PI 3.14 |
const double PI = 3.14; |
const double PI = 3.14; |
PI = 3.14 |
PI = 3.14 |
const PI = 3.14; |
Struct | struct P {int x; int y;}; |
struct P {int x; int y;}; |
struct P { public int X, Y; } |
class P: |
P = { x = 0, y = 0 } |
class P { constructor() {...} } |
Class | N/A | class Foo { ... }; |
class Foo { ... } |
class Foo: |
Foo = {} |
class Foo { ... } |
Main function | int main() {...} |
int main() {...} |
static void Main() |
if __name__ == "__main__": ... |
N/A | function main() {...} |
Public/Private | N/A | public: / private: |
public int X; |
_x (convention) |
By convention | this.x = x; |
Inheritance | N/A | class B : public A {...} |
class B : A {...} |
class B(A): ... |
metatables | class B extends A {...} |
Virtual/Override | N/A | virtual void foo(); |
virtual / override |
def foo(self): ... |
Overwrite function | method() {...} |
Abstract | N/A | virtual void foo() = 0; |
abstract void Foo(); |
raise NotImplementedError() |
N/A | throw new Error("not implemented") |
Generics/Templates | N/A | template<typename T> |
class C<T> {...} |
Duck typing/inheritance | Tables/functions | function f<T>(x: T) (TS) |
Namespace/Module | N/A | namespace MyNS {...} |
namespace MyNS {...} |
import myns / packages |
Module/table | export/import/global obj |
Constructor | N/A | Foo() {...} |
public Foo() {...} |
def __init__(self): ... |
function Foo:new() ... |
constructor() {...} |
Destructor | N/A | ~Foo() {...} |
~Foo() {...} (rare) |
def __del__(self): ... |
N/A | dispose() (manual) |
New (alloc) | malloc(sizeof(Foo)) |
new Foo() |
new Foo() |
Foo() |
Foo:new() |
new Foo() |
Delete (free) | free(ptr); |
delete p; |
GC (none) | GC (none) | GC (none) | GC (none) |
Pointer | int *p; |
int *p; |
unsafe (rare) | N/A | N/A | N/A |
Reference | N/A | int& ref = x; |
ref parameter | all vars are references | N/A | N/A |
Const/readonly | const int x = 5; |
const int x = 5; |
const int x = 5; |
ALL_CAPS (convention) | N/A | const x = 5; |
Operator overload | N/A | Foo operator+(Foo a, Foo b) |
public static Foo operator +(Foo a, Foo b) |
def __add__(self, other): ... |
N/A | N/A |
Static | N/A | static int count; |
static int count; |
@staticmethod in class |
N/A (just global) | static foo() {...} |
Enum | enum Color {RED,GREEN}; |
enum Color {RED, GREEN}; |
enum Color { Red, Green } |
from enum import Enum |
{RED = 1, GREEN = 2} |
const Color = { RED: 1, GREEN: 2 } |
Access member | obj.x |
obj->x or obj.x |
obj.X |
obj.x |
obj.x |
obj.x |