Skip to content

Commit 9b7fc66

Browse files
Update Class Topic
1 parent f23b1c5 commit 9b7fc66

19 files changed

Lines changed: 306 additions & 20 deletions

File tree

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
```Go
2-
// TODO
2+
// No Native Abstract Class Support.
3+
// Use interfaces to define required behavior.
4+
5+
type Shape interface {
6+
Area() float64
7+
}
8+
9+
type Circle struct {
10+
Radius float64
11+
}
12+
13+
func (circle Circle) Area() float64 {
14+
return math.Pi * circle.Radius * circle.Radius
15+
}
316
```
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
```Rust
2-
// TODO
2+
// No Native Abstract Class Support.
3+
// Use traits to define required behavior.
4+
5+
trait Shape {
6+
fn area(&self) -> f64;
7+
}
8+
9+
struct Circle {
10+
radius: f64,
11+
}
12+
13+
impl Shape for Circle {
14+
fn area(&self) -> f64 {
15+
std::f64::consts::PI * self.radius * self.radius
16+
}
17+
}
318
```
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
```Typescript
2-
// TODO
2+
abstract class Shape {
3+
public abstract area(): number;
4+
5+
public describe(): string {
6+
return `Area: ${this.area()}`;
7+
}
8+
}
9+
10+
class Circle extends Shape {
11+
public constructor(private readonly radius: number) {
12+
super();
13+
}
14+
15+
public area(): number {
16+
return Math.PI * this.radius * this.radius;
17+
}
18+
}
319
```

docs/programming-languages/class/basic-template/_cpp.mdx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,25 @@ void MyClass::myPublicMethod (float someVariable)
114114
```
115115
116116
```Cpp
117-
// TODO
118-
// const & Ref example
117+
// Passing and returning by const reference
118+
// Prefer this for non-fundamental types when the function should not mutate the value.
119+
120+
class MyClass
121+
{
122+
private:
123+
std::string my_private_property;
124+
125+
public:
126+
std::string const &getMyPrivateProperty() const
127+
{
128+
return this->my_private_property;
129+
}
130+
131+
void setMyPrivateProperty(std::string const &value)
132+
{
133+
this->my_private_property = value;
134+
}
135+
};
119136
```
120137
121138
```Cpp
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
11
```Go
2-
// TODO
2+
type MyClass struct {
3+
MyPublicProperty float64
4+
myPrivateProperty float64
5+
}
6+
7+
func NewMyClass(value float64) MyClass {
8+
return MyClass{
9+
MyPublicProperty: value,
10+
myPrivateProperty: value,
11+
}
12+
}
13+
14+
func (value MyClass) GetMyPrivateProperty() float64 {
15+
return value.myPrivateProperty
16+
}
17+
18+
func (value *MyClass) SetMyPrivateProperty(newValue float64) {
19+
value.myPrivateProperty = newValue
20+
}
21+
22+
myClass := NewMyClass(10.0)
23+
myClass.SetMyPrivateProperty(20.0)
324
```
Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
11
```Rust
2-
// TODO
2+
struct MyClass {
3+
my_public_property: f32,
4+
my_private_property: f32,
5+
}
6+
7+
impl MyClass {
8+
fn new(value: f32) -> Self {
9+
Self {
10+
my_public_property: value,
11+
my_private_property: value,
12+
}
13+
}
14+
15+
fn get_my_private_property(&self) -> f32 {
16+
self.my_private_property
17+
}
18+
19+
fn set_my_private_property(&mut self, value: f32) {
20+
self.my_private_property = value;
21+
}
22+
}
23+
24+
let mut my_class = MyClass::new(10.0);
25+
my_class.set_my_private_property(20.0);
326
```
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
```Cs
2-
// TODO
2+
class MyClass
3+
{
4+
public MyClass()
5+
{
6+
// Default Constructor
7+
}
8+
9+
public MyClass(int value)
10+
{
11+
// Constructor With Parameters
12+
}
13+
14+
~MyClass()
15+
{
16+
// Finalizer
17+
}
18+
}
319
```
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
```Go
2-
// TODO
2+
type MyStruct struct {
3+
Value int
4+
}
5+
6+
// Go has no constructor syntax.
7+
// Use factory functions by convention.
8+
func NewMyStruct(value int) MyStruct {
9+
return MyStruct{Value: value}
10+
}
11+
12+
// Destructors are Not Available.
13+
// Use explicit Close/Cleanup methods when needed.
14+
func (value MyStruct) Close() error {
15+
// cleanup
16+
return nil
17+
}
318
```
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
```Java
2-
// TODO
2+
class MyClass {
3+
public MyClass() {
4+
// Default Constructor
5+
}
6+
7+
public MyClass(int value) {
8+
// Constructor With Parameters
9+
}
10+
}
11+
12+
// Destructors are Not Available.
13+
// Use try-with-resources and AutoCloseable for deterministic cleanup.
14+
class MyResource implements AutoCloseable {
15+
@Override
16+
public void close() {
17+
// cleanup
18+
}
19+
}
320
```
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
```Javascript
2-
// TODO
2+
class MyClass {
3+
constructor(value) {
4+
this.value = value;
5+
}
6+
}
7+
8+
const myClass = new MyClass(10);
9+
10+
// Destructors are Not Available.
11+
// Use explicit cleanup methods when needed.
12+
class Resource {
13+
close() {
14+
// cleanup
15+
}
16+
}
317
```

0 commit comments

Comments
 (0)