From cbfbdab6c4baec0f35c459f1dffbc84ba4e9cc4c Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Sat, 28 Mar 2020 19:49:57 +0100 Subject: [PATCH] complete lesson --- README.md | 26 +++++++++++++ cpp/instances/main.cpp | 8 +++- java/instances/Car.java | 6 ++- java/instances/main.java | 6 +-- lessons/Instances.md | 70 +++++++++++++++++++++++++++++++++++ rust/instances/src/main.rs | 4 ++ typescript/instances/index.ts | 4 ++ 7 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 README.md create mode 100644 lessons/Instances.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..37e9997 --- /dev/null +++ b/README.md @@ -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 diff --git a/cpp/instances/main.cpp b/cpp/instances/main.cpp index 12a9a16..91ce2a2 100644 --- a/cpp/instances/main.cpp +++ b/cpp/instances/main.cpp @@ -2,14 +2,18 @@ #include 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.model = "Velo"; - + + // do the same for a second car car b; b.manufacturer = "Ford"; b.model = "Model T"; + // use a function of the car class to print out the information a.print_info(); b.print_info(); } diff --git a/java/instances/Car.java b/java/instances/Car.java index 6e8d3da..b616316 100644 --- a/java/instances/Car.java +++ b/java/instances/Car.java @@ -10,7 +10,9 @@ public class Car { public String model = ""; public String manufacturer = ""; - public String get_info() { - return "Car Information:\n- manufacturer: " + this.manufacturer + "\n- model: " + this.model; + public void print_info() { + System.out.println("Car Information:"); + System.out.println("- manufacturer: " + this.manufacturer); + System.out.println("- model: " + this.model); } } diff --git a/java/instances/main.java b/java/instances/main.java index cb9ebb0..e221455 100644 --- a/java/instances/main.java +++ b/java/instances/main.java @@ -6,13 +6,13 @@ public class main { a.manufacturer = "Benz"; a.model = "Velo"; - // Do the same for a second car + // do the same for a second car Car b = new Car(); b.manufacturer = "Ford"; b.model = "Model T"; // use a function of the car class to print out the information - System.out.println(a.get_info()); - System.out.println(b.get_info()); + a.print_info(); + b.print_info(); } } diff --git a/lessons/Instances.md b/lessons/Instances.md new file mode 100644 index 0000000..32175c2 --- /dev/null +++ b/lessons/Instances.md @@ -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) diff --git a/rust/instances/src/main.rs b/rust/instances/src/main.rs index de3eccf..6a27d96 100644 --- a/rust/instances/src/main.rs +++ b/rust/instances/src/main.rs @@ -2,16 +2,20 @@ mod car; use car::Information; fn main() { + // create a new car and store it in the variable 'a' let a = car::Car { + // set some data for that car manufacturer: "Benz".to_string(), model: "Velo".to_string() }; + // do the same for a second car let b = car::Car { manufacturer: "Ford".to_string(), model: "Model T".to_string() }; + // use a function of the car class to print out the information a.print_info(); b.print_info(); } diff --git a/typescript/instances/index.ts b/typescript/instances/index.ts index 75effd6..c80550f 100644 --- a/typescript/instances/index.ts +++ b/typescript/instances/index.ts @@ -1,12 +1,16 @@ import {Car} from './car' +// create a new car and store it in the variable 'a' const a = new Car(); +// set some data for that car a.manufacturer = 'Benz'; a.model = 'Velo'; +// do the same for a second car const b = new Car(); b.manufacturer = 'Ford'; b.model = 'Model T'; +// use a function of the car class to print out the information a.print_info(); b.print_info(); \ No newline at end of file