Properties and methods
Demonstrates private properties with methods that read and write them (encapsulation).
C C23
C23
#include <stdio.h>
// C has no access control; the underscore prefix convention marks "private" (not enforced at compile time, by convention)
typedef struct {
int _count; // conventional private field
} Counter;
void counter_increment(Counter *c) {
c->_count++;
}
int counter_get(const Counter *c) {
return c->_count;
}
int main(void) {
Counter c = { 0 };
counter_increment(&c);
counter_increment(&c);
printf("%d", counter_get(&c));
return 0;
}
Output
2
C++ C++26
C++26
#include <iostream>
class Counter {
int count = 0; // private by default (class default access level is private)
public:
void increment() { count++; }
int get() const { return count; }
};
int main() {
Counter c;
c.increment();
c.increment();
std::cout << c.get() << "\n";
return 0;
}
Output
2
C# v11.0.100
v11.0.100
var c = new Counter();
c.Increment();
c.Increment();
Console.WriteLine(c.Get());
class Counter {
private int count = 0; // private field
public void Increment() => count++;
public int Get() => count;
}
Output
2
Go v1.27.4
v1.27.4
package main
import "fmt"
type Counter struct {
count int
}
func (c *Counter) Increment() {
c.count++
}
func (c *Counter) Get() int {
return c.count
}
func main() {
c := &Counter{}
c.Increment()
c.Increment()
fmt.Println(c.Get())
}
Output
2
Java v27.0.1
v27.0.1
public class Main {
static class Counter {
private int count = 0;
void increment() {
count++;
}
int get() {
return count;
}
}
public static void main(String[] args) {
Counter c = new Counter();
c.increment();
c.increment();
System.out.println(c.get());
}
}
Output
2
JavaScript v26.13.1
v26.13.1
class Counter {
#count = 0;
increment() {
this.#count++;
}
get() {
return this.#count;
}
}
const c = new Counter();
c.increment();
c.increment();
console.log(c.get());
Output
2
PHP v8.6.0
v8.6.0
class Counter {
private int $count = 0;
public function increment(): void {
$this->count++;
}
public function get(): int {
return $this->count;
}
}
$c = new Counter();
$c->increment();
$c->increment();
echo $c->get();
Output
2
Python v3.15.2
v3.15.2
class Counter:
def __init__(self):
self._count = 0
def increment(self):
self._count += 1
def get(self):
return self._count
c = Counter()
c.increment()
c.increment()
print(c.get())
Output
2
Rust v1.99.1
v1.99.1
struct Counter {
count: i32,
}
impl Counter {
fn increment(&mut self) {
self.count += 1;
}
fn get(&self) -> i32 {
self.count
}
}
fn main() {
let mut c = Counter { count: 0 };
c.increment();
c.increment();
println!("{}", c.get());
}
Output
2
TypeScript v26.13.1
v26.13.1
class Counter {
#count = 0; // private field (# is true privacy; TS also has the private compile-time modifier)
increment(): void {
this.#count++;
}
get(): number {
return this.#count;
}
}
const c = new Counter();
c.increment();
c.increment();
console.log(c.get());
Output
2
Language differences
C: no access control — the example marks private fields with a leading underscore by convention and uses counter_-prefixed functions as methods.
Go: distinguishes exported/unexported by initial letter case; mutating self needs a pointer receiver *Counter.
Java: four visibility levels — private/protected/public plus package-private (no modifier; the example's methods are package-private).
JavaScript: # private fields (ES2022).
PHP: visibility — private/protected/public.
Python: no enforced privacy — the _ prefix convention (__ triggers name mangling).
Rust: items are private within their module by default and exported with pub; mutating self needs &mut self.