21 lines
787 B
Plaintext
21 lines
787 B
Plaintext
import std::io;
|
|||
|
|
trait shape {
|
||
|
|
def area(this: &shape): f64;
|
||
|
|
def grow(this: &mut shape, by: f64): void;
|
||
|
|
}
|
||
|
|
struct square: shape { var side: f64; }
|
||
|
|
struct circle { var radius: f64; }
|
||
|
|
def square::area(this: &square): f64 { return this.side * this.side; }
|
||
|
|
def square::grow(this: &mut square, by: f64): void { this.side += by; }
|
||
|
|
def circle::area(this: &circle): f64 { return 3 * this.radius * this.radius; }
|
||
|
|
def circle::grow(this: &mut circle, by: f64): void { this.radius *= by; }
|
||
|
|
def report(s: &shape): string { return $"{s.area()} {s}"; }
|
||
|
|
def enlarge(s: &mut shape): void { s.grow(2); }
|
||
|
|
def main(): void {
|
||
|
|
var sq := square{3};
|
||
|
|
var ci := circle{1};
|
||
|
|
enlarge(&mut sq);
|
||
|
|
enlarge(&mut ci);
|
||
|
|
val shape_ref: &shape = &sq;
|
||
|
|
println($"{report(&sq)} {report(&ci)} {shape_ref.area()}");
|
||
|
|
}
|