Classes and instantiation
Defines a class with a constructor and methods, then instantiates and uses it.
C C23
C23
#include <stdio.h>
// C has no class: a struct holds state, name-prefixed functions act as methods
typedef struct {
char name[32];
} Person;
void person_init(Person *p, const char *name) {
snprintf(p->name, sizeof(p->name), "%s", name);
}
void person_greet(const Person *p) {
printf("Hello, %s", p->name);
}
int main(void) {
Person p;
person_init(&p, "Alice");
person_greet(&p);
return 0;
}
Output
Hello, Alice
C++ C++26
C++26
#include <iostream>
#include <string>
#include <utility>
class Person {
std::string name;
public:
explicit Person(std::string name) : name(std::move(name)) {} // constructor + initializer list
std::string greet() const { return "Hello, " + name; }
};
int main() {
Person p("Alice");
std::cout << p.greet() << "\n";
return 0;
}
Output
Hello, Alice
C# v11.0.100
v11.0.100
var p = new Person("Alice");
Console.WriteLine(p.Greet());
class Person {
public string Name { get; } // read-only property
public Person(string name) { Name = name; }
public string Greet() => "Hello, " + Name;
}
Output
Hello, Alice
Go v1.27.4
v1.27.4
package main
import "fmt"
type Person struct {
name string
}
func (p Person) greet() string {
return "Hello, " + p.name
}
func main() {
p := Person{name: "Alice"}
fmt.Println(p.greet())
}
Output
Hello, Alice
Java v27.0.1
v27.0.1
public class Main {
static class Person {
String name;
Person(String name) {
this.name = name;
}
String greet() {
return "Hello, " + name;
}
}
public static void main(String[] args) {
Person p = new Person("Alice");
System.out.println(p.greet());
}
}
Output
Hello, Alice
JavaScript v26.13.1
v26.13.1
class Person {
constructor(name) {
this.name = name;
}
greet() {
return "Hello, " + this.name;
}
}
const p = new Person("Alice");
console.log(p.greet());
Output
Hello, Alice
PHP v8.6.0
v8.6.0
class Person {
public string $name;
public function __construct(string $name) {
$this->name = $name;
}
public function greet(): string {
return "Hello, " . $this->name;
}
}
$p = new Person("Alice");
echo $p->greet();
Output
Hello, Alice
Python v3.15.2
v3.15.2
class Person:
def __init__(self, name):
self.name = name
def greet(self):
return "Hello, " + self.name
p = Person("Alice")
print(p.greet())
Output
Hello, Alice
Rust v1.99.1
v1.99.1
struct Person {
name: String,
}
impl Person {
fn greet(&self) -> String {
format!("Hello, {}", self.name)
}
}
fn main() {
let p = Person { name: String::from("Alice") };
println!("{}", p.greet());
}
Output
Hello, Alice
TypeScript v26.13.1
v26.13.1
class Person {
name: string; // fields must be declared with types first (or annotated directly on constructor parameters)
constructor(name: string) {
this.name = name;
}
greet(): string {
return "Hello, " + this.name;
}
}
const p = new Person("Alice");
console.log(p.greet());
Output
Hello, Alice
Language differences
C: no classes — the example holds state in a struct with person_-prefixed functions as its methods, semantics enforced by convention.
Go: no class — a struct plus methods with receivers; instantiation via Person{...}.
Java: every public top-level class needs its own file, so the example uses a static nested class; instantiation via new.
JavaScript: ES6 class + constructor; instantiation via new.
PHP: constructor differences — __construct, members accessed with $this->; instantiation via new.
Python: __init__ with self as the first method parameter; instantiation calls the class directly.
Rust: struct + impl; instantiation via Person { ... }.