complete lesson

This commit is contained in:
Timo Hocker 2020-03-28 19:49:57 +01:00
parent 86ffdd7397
commit cbfbdab6c4
7 changed files with 117 additions and 7 deletions

26
README.md Normal file
View File

@ -0,0 +1,26 @@
# Object Oriented Programming
A short introduction to object oriented programming with examples in multiple languages
Example Languages:
- Java
- Rust
- C++
- JavaScript (using TypeScript)
Lessons:
- [instanciating objects, accessing attributes and methods](./lessons/Instances.md)
Work in progress:
- creating a class
- use attributes or functions from a function within an object
- constructors
- encapsulation
- getter / setter / property
- inheritance
- polymorphism
- virtual classes
- interfaces

View File

@ -2,14 +2,18 @@
#include <cstring> #include <cstring>
int main() { int main() {
car a; // create a new car and store it in the variable 'a'
car a;
// set some data for that car
a.manufacturer = "Benz"; a.manufacturer = "Benz";
a.model = "Velo"; a.model = "Velo";
// do the same for a second car
car b; car b;
b.manufacturer = "Ford"; b.manufacturer = "Ford";
b.model = "Model T"; b.model = "Model T";
// use a function of the car class to print out the information
a.print_info(); a.print_info();
b.print_info(); b.print_info();
} }

View File

@ -10,7 +10,9 @@ public class Car {
public String model = ""; public String model = "";
public String manufacturer = ""; public String manufacturer = "";
public String get_info() { public void print_info() {
return "Car Information:\n- manufacturer: " + this.manufacturer + "\n- model: " + this.model; System.out.println("Car Information:");
System.out.println("- manufacturer: " + this.manufacturer);
System.out.println("- model: " + this.model);
} }
} }

View File

@ -6,13 +6,13 @@ public class main {
a.manufacturer = "Benz"; a.manufacturer = "Benz";
a.model = "Velo"; a.model = "Velo";
// Do the same for a second car // do the same for a second car
Car b = new Car(); Car b = new Car();
b.manufacturer = "Ford"; b.manufacturer = "Ford";
b.model = "Model T"; b.model = "Model T";
// use a function of the car class to print out the information // use a function of the car class to print out the information
System.out.println(a.get_info()); a.print_info();
System.out.println(b.get_info()); b.print_info();
} }
} }

70
lessons/Instances.md Normal file
View File

@ -0,0 +1,70 @@
# Object Instances
For this short Introduction we'll only create two objects, give them some data
and let them do their magic with one function.
Objects are generally used to keep data logically grouped and also make it easy
to perform actions on them without having to account for all the logic at the
time of using those functions.
As an example I have created a Car class. This is probably going to be the
main class used in all further lessons, since it has a lot of room for more data
and functions to add except for manufacturer, model and the function print_info
in this example.
```java
/*
* First we create a new car and store that in a variable, here called 'a'.
* Usually a programming language has a keyword like 'new' to instantiate new objects.
* here in Java a new Object is created by Specifying variable type, variable name
* the keyword new and again the type that should be instantiated.
*/
Car a = new Car();
/*
* To access data within an object you can use the object name followed by a dot
* and the name of the property.
* The data can be read or modified at will.
* Since this is just the start of working with oop,
* I'll also use very early cars here
*/
a.manufacturer = "Benz";
a.model = "Velo";
/*
* Then we do the same thing for a second car
*/
Car b = new Car();
b.manufacturer = "Ford";
b.model = "Model T";
/*
* Functions can be accessed in the same way as properties, just with
* parentheses behind to actually run the functions.
* These will print out all the data we put into the objects before.
*/
a.print_info();
b.print_info();
```
The above example code together with the car class will create the following output:
```text
Car Information:
- manufacturer: Benz
- model: Velo
Car Information:
- manufacturer: Ford
- model: Model T
```
If any of the examples don't produce the same output, then congrats, you just
got yourself the homework to fix the error and submit a pull request to this
repository :)
## Examples
- [Java](../java/instances)
- [Rust](../rust/instances)
- [C++](../cpp/instances)
- [JavaScript (using TypeScript)](../typescript/instances)

View File

@ -2,16 +2,20 @@ mod car;
use car::Information; use car::Information;
fn main() { fn main() {
// create a new car and store it in the variable 'a'
let a = car::Car { let a = car::Car {
// set some data for that car
manufacturer: "Benz".to_string(), manufacturer: "Benz".to_string(),
model: "Velo".to_string() model: "Velo".to_string()
}; };
// do the same for a second car
let b = car::Car { let b = car::Car {
manufacturer: "Ford".to_string(), manufacturer: "Ford".to_string(),
model: "Model T".to_string() model: "Model T".to_string()
}; };
// use a function of the car class to print out the information
a.print_info(); a.print_info();
b.print_info(); b.print_info();
} }

View File

@ -1,12 +1,16 @@
import {Car} from './car' import {Car} from './car'
// create a new car and store it in the variable 'a'
const a = new Car(); const a = new Car();
// set some data for that car
a.manufacturer = 'Benz'; a.manufacturer = 'Benz';
a.model = 'Velo'; a.model = 'Velo';
// do the same for a second car
const b = new Car(); const b = new Car();
b.manufacturer = 'Ford'; b.manufacturer = 'Ford';
b.model = 'Model T'; b.model = 'Model T';
// use a function of the car class to print out the information
a.print_info(); a.print_info();
b.print_info(); b.print_info();