81 lines
1.9 KiB
HTML
81 lines
1.9 KiB
HTML
<section>
|
|
<h2>Object (dis)orientation</h2>
|
|
<h6> How to survive a "post-OO" world</h6>
|
|
</section>
|
|
<section>
|
|
<section>
|
|
Class/Inheritance has challenges
|
|
</section>
|
|
<section>
|
|
Combining Data + Code
|
|
<pre><code data-trim data-noescape data-line-numbers="5|7-9">
|
|
class MyBase {
|
|
protected:
|
|
int useless;
|
|
public:
|
|
virtual void makeSound() = 0;
|
|
|
|
virtual int get_thing() {
|
|
return -1;
|
|
};
|
|
};
|
|
</code></pre>
|
|
<p class="fragment">You have to know if this is an "interface", abstract class, or regular class.</p>
|
|
<small class="fragment">(it's an abstract class, since <code>makeSound</code> is pure virtual)</small>
|
|
</section>
|
|
<section>
|
|
<p> A class can be one of many things </p>
|
|
<ul>
|
|
<li>Interface - abstract class with no members, and purely virtual </li>
|
|
<li>Abstract Class - class with <em>some</em> pure virtual methods </li>
|
|
<li>"Regular" Class - class that is standalone and is fully implemented </li>
|
|
</ul>
|
|
<small class="fragment">These concepts all exist under the <code>class</code> keyword </small>
|
|
</section>
|
|
</section>
|
|
<section>
|
|
In Rust, you trade <em>Inheritance</em> for <em>Traits</em> and <em>Types</em>.
|
|
</section>
|
|
|
|
<section>
|
|
<section>
|
|
<p>Traits are <em>Interfaces</em></p>
|
|
<pre><code data-trim data-noescape>
|
|
pub trait Mammal {
|
|
fn get_temp(&self) -> i32;
|
|
}
|
|
</code></pre>
|
|
</section>
|
|
<section>
|
|
<p>... that are explicitly implemented</p>
|
|
<pre><code data-trim data-noescape>
|
|
pub struct Cat {
|
|
age: i32,
|
|
outdoor: bool,
|
|
}
|
|
impl Mammal for Cat {
|
|
fn get_temp(&self) -> i32 {
|
|
if self.outdoor {
|
|
65
|
|
} else {
|
|
72
|
|
}
|
|
}
|
|
}
|
|
</code></pre>
|
|
|
|
</section>
|
|
<section>
|
|
<p> And can be used statically <em>or</em> dynamically!</p>
|
|
<pre><code data-trim>
|
|
fn static_mammal<T: Mammal>(m: T) {
|
|
// the type of T must be known at compile time.
|
|
}
|
|
fn dyn_mammal(m: &dyn Mammal) {
|
|
// This uses a vtable to dispatch at runtime.
|
|
}
|
|
</code></pre>
|
|
</section>
|
|
</section>
|
|
|