Compare commits
9 Commits
f6e9115e2c
...
8055085c1f
Author | SHA1 | Date | |
---|---|---|---|
8055085c1f | |||
f2df87ecfd | |||
9efaf7b5f9 | |||
4cacd3afa1 | |||
b9c03f8207 | |||
d6f2d44eaf | |||
d30589c5ad | |||
c6246aade2 | |||
5aad2f7644 |
@ -13,13 +13,14 @@ Lessons:
|
||||
|
||||
- [instanciating objects, accessing attributes and methods](./lessons/Instances.md)
|
||||
- [creating a class](./lessons/Classes.md)
|
||||
- [constructors](./lessons/Constructors.md)
|
||||
|
||||
Work in progress:
|
||||
|
||||
- constructors
|
||||
- inheritance
|
||||
- encapsulation
|
||||
- getter / setter / property
|
||||
- inheritance
|
||||
- polymorphism
|
||||
- virtual classes
|
||||
- interfaces
|
||||
- Factories
|
||||
|
10
cpp/Instances.md
Normal file
10
cpp/Instances.md
Normal file
@ -0,0 +1,10 @@
|
||||
# C++ Example
|
||||
|
||||
## Language Specific
|
||||
|
||||
c++ is almost identical to java in this example. The only difference here is
|
||||
that c++ doesn't require the object to be initialized with the keyword 'new'.
|
||||
|
||||
Relevant Files:
|
||||
|
||||
- [main.cpp](./classes/main.cpp)
|
@ -1,7 +1,8 @@
|
||||
# Classes in C++
|
||||
|
||||
In c++ classes are defined using a header file and the implementation in a
|
||||
regular .cpp file. Explanations to the structure can be found in the files themselves
|
||||
regular .cpp file. Explanations to the structure can be found in the files
|
||||
themselves
|
||||
|
||||
## Relevant files
|
||||
|
||||
|
@ -24,11 +24,11 @@ class car
|
||||
// define the visibility of the following fields and methods
|
||||
// in this case everything is public since visibility is a topic in future lessons
|
||||
public:
|
||||
// define two strings, manufacturer and model
|
||||
// define two fields, manufacturer and model
|
||||
std::string manufacturer;
|
||||
std::string model;
|
||||
|
||||
// define a function that will print out information
|
||||
// define a method that will print out information
|
||||
void print_info();
|
||||
};
|
||||
|
||||
|
7
cpp/constructors/README.md
Normal file
7
cpp/constructors/README.md
Normal file
@ -0,0 +1,7 @@
|
||||
# Constructors in C++
|
||||
|
||||
## Relevant files
|
||||
|
||||
- [car.h](./car.h)
|
||||
- [car.cpp](./car.cpp)
|
||||
- [main.cpp](./main.cpp)
|
23
cpp/constructors/car.cpp
Normal file
23
cpp/constructors/car.cpp
Normal file
@ -0,0 +1,23 @@
|
||||
// include the header file
|
||||
#include "car.h"
|
||||
|
||||
// include necessary libraries and namespaces
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
// define the implementation for car.print_info()
|
||||
void car::print_info()
|
||||
{
|
||||
cout << "Car Information:" << endl;
|
||||
// fields that are defined in the header file can just be used like normal variables here
|
||||
cout << "- manufacturer: " << manufacturer << endl;
|
||||
cout << "- model: " << model << endl;
|
||||
}
|
||||
|
||||
car::car(string manufacturer, string model) {
|
||||
// set manufacturer of the current object to the given manufacturer
|
||||
this->manufacturer = manufacturer;
|
||||
// set model of the current object to the given model
|
||||
this->model = model;
|
||||
}
|
22
cpp/constructors/car.h
Normal file
22
cpp/constructors/car.h
Normal file
@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
// including a necessary library and namespace to work with strings
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
// define the class
|
||||
class car
|
||||
{
|
||||
private:
|
||||
// define two fields, manufacturer and model
|
||||
// this time as private to make the data only settable through the constructor or other local functions
|
||||
std::string manufacturer;
|
||||
std::string model;
|
||||
|
||||
public:
|
||||
// define the constructor, taking manufacturer and model as parameters
|
||||
car(std::string manufacturer, std::string model);
|
||||
|
||||
// define a method that will print out information
|
||||
void print_info();
|
||||
};
|
@ -3,16 +3,11 @@
|
||||
|
||||
int main() {
|
||||
// create a new car and store it in the variable 'a'
|
||||
// in contrast to java, c++ does not need initialization with the 'new' keyword here.
|
||||
car a;
|
||||
// set some data for that car
|
||||
a.manufacturer = "Benz";
|
||||
a.model = "Velo";
|
||||
// The constructor requires manufacturer and model to be specified.
|
||||
car a("Benz", "Velo");
|
||||
|
||||
// do the same for a second car
|
||||
car b;
|
||||
b.manufacturer = "Ford";
|
||||
b.model = "Model T";
|
||||
car b("Ford", "Model T");
|
||||
|
||||
// use a function of the car class to print out the information
|
||||
a.print_info();
|
@ -1,11 +0,0 @@
|
||||
# C++ Example
|
||||
|
||||
## Language Specific
|
||||
|
||||
c++ is almost identical to java in this example.
|
||||
The only difference here is that c++ doesn't require the object
|
||||
to be initialized with the keyword 'new'.
|
||||
|
||||
Relevant Files:
|
||||
|
||||
- [main.cpp](./main.cpp)
|
@ -1,20 +0,0 @@
|
||||
/*
|
||||
|
||||
Basic Car class
|
||||
just here to make the demonstration in the main file possible
|
||||
explanation of the code here will be given in later lessons
|
||||
|
||||
*/
|
||||
|
||||
#include "car.h"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void car::print_info()
|
||||
{
|
||||
cout << "Car Information:" << endl;
|
||||
cout << "- manufacturer: " << manufacturer << endl;
|
||||
cout << "- model: " << model << endl;
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
/*
|
||||
|
||||
Basic Car class
|
||||
just here to make the demonstration in the main file possible
|
||||
explanation of the code here will be given in later lessons
|
||||
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class car
|
||||
{
|
||||
public:
|
||||
std::string manufacturer;
|
||||
std::string model;
|
||||
void print_info();
|
||||
};
|
5
java/Instances.md
Normal file
5
java/Instances.md
Normal file
@ -0,0 +1,5 @@
|
||||
# Java Example
|
||||
|
||||
Relevant Files:
|
||||
|
||||
- [main.java](./classes/main.java)
|
@ -8,6 +8,7 @@ public class Car {
|
||||
public String model = "";
|
||||
public String manufacturer = "";
|
||||
|
||||
// create the method print_info()
|
||||
public void print_info() {
|
||||
System.out.println("Car Information:");
|
||||
/**
|
||||
|
23
java/constructors/Car.java
Normal file
23
java/constructors/Car.java
Normal file
@ -0,0 +1,23 @@
|
||||
public class Car {
|
||||
|
||||
// define the fields as private to prevent outside classes from accessing them
|
||||
// access restriction will be a topic in the next lesson
|
||||
private String model = "";
|
||||
private String manufacturer = "";
|
||||
|
||||
// define the constructor
|
||||
// in java a constructor is basically a function without return type named exactly like the class
|
||||
public Car(String manufacturer, String model) {
|
||||
// set manufacturer of the current object to the given manufacturer
|
||||
this.manufacturer = manufacturer;
|
||||
// set model of the current object to the given model
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
// create the method print_info()
|
||||
public void print_info() {
|
||||
System.out.println("Car Information:");
|
||||
System.out.println("- manufacturer: " + this.manufacturer);
|
||||
System.out.println("- model: " + this.model);
|
||||
}
|
||||
}
|
6
java/constructors/README.md
Normal file
6
java/constructors/README.md
Normal file
@ -0,0 +1,6 @@
|
||||
# Java example
|
||||
|
||||
Relevant files:
|
||||
|
||||
- [main.java](./main.java)
|
||||
- [Car.java](./Car.java)
|
@ -1,15 +1,11 @@
|
||||
public class main {
|
||||
public static void main(String[] args) {
|
||||
// create a new car and store it in the variable 'a'
|
||||
Car a = new Car();
|
||||
// set some data for that car
|
||||
a.manufacturer = "Benz";
|
||||
a.model = "Velo";
|
||||
// the constructor requires manufacturer and model to be specified
|
||||
Car a = new Car("Benz", "Velo");
|
||||
|
||||
// do the same for a second car
|
||||
Car b = new Car();
|
||||
b.manufacturer = "Ford";
|
||||
b.model = "Model T";
|
||||
Car b = new Car("Ford", "Model T");
|
||||
|
||||
// use a function of the car class to print out the information
|
||||
a.print_info();
|
@ -1,18 +0,0 @@
|
||||
/*
|
||||
|
||||
Basic Car class
|
||||
just here to make the demonstration in the main file possible
|
||||
explanation of the code here will be given in later lessons
|
||||
|
||||
*/
|
||||
|
||||
public class Car {
|
||||
public String model = "";
|
||||
public String manufacturer = "";
|
||||
|
||||
public void print_info() {
|
||||
System.out.println("Car Information:");
|
||||
System.out.println("- manufacturer: " + this.manufacturer);
|
||||
System.out.println("- model: " + this.model);
|
||||
}
|
||||
}
|
@ -1,5 +0,0 @@
|
||||
# Java Example
|
||||
|
||||
Relevant Files:
|
||||
|
||||
- [main.java](./main.java)
|
14
lessons/Constructors.md
Normal file
14
lessons/Constructors.md
Normal file
@ -0,0 +1,14 @@
|
||||
# Constructors
|
||||
|
||||
Constructors are a way to give data to an object right when it gets created. The
|
||||
object can also run functions on that data during construction.
|
||||
|
||||
Constructors should only be used to initialize data which the object needs to
|
||||
function.
|
||||
|
||||
## Code examples and language specific explanations
|
||||
|
||||
- [Java](../java/constructors/README.md)
|
||||
- [Rust](../rust/Constructors.md)
|
||||
- [C++](../cpp/constructors/README.md)
|
||||
- [JavaScript (using TypeScript)](../typescript/constructors/README.md)
|
@ -7,10 +7,10 @@ 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.
|
||||
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
|
||||
/*
|
||||
@ -47,7 +47,8 @@ a.print_info();
|
||||
b.print_info();
|
||||
```
|
||||
|
||||
The above example code together with the car class will create the following output:
|
||||
The above example code together with the car class will create the following
|
||||
output:
|
||||
|
||||
```text
|
||||
Car Information:
|
||||
@ -64,7 +65,7 @@ repository :)
|
||||
|
||||
## Examples
|
||||
|
||||
- [Java](../java/instances/README.md)
|
||||
- [Rust](../rust/instances/README.md)
|
||||
- [C++](../cpp/instances/README.md)
|
||||
- [JavaScript (using TypeScript)](../typescript/instances/README.md)
|
||||
- [Java](../java/Instances.md)
|
||||
- [Rust](../rust/Instances.md)
|
||||
- [C++](../cpp/Instances.md)
|
||||
- [JavaScript (using TypeScript)](../typescript/Instances.md)
|
||||
|
14
rust/Constructors.md
Normal file
14
rust/Constructors.md
Normal file
@ -0,0 +1,14 @@
|
||||
# Constructors in rust
|
||||
|
||||
Rust is a special case for constructors. The language itself is already
|
||||
different from most other languages in structure, especially for object oriented
|
||||
programming.
|
||||
|
||||
Simply said, there are no constructors in rust. When you create an object, you
|
||||
have to initialize all data immediately and by yourself.
|
||||
|
||||
You can look into the classes or instances examples to see how object
|
||||
construction is done in rust.
|
||||
|
||||
- [main.rs](./classes/src/main.rs)
|
||||
- [car.rs](./classes/src/car.rs)
|
@ -3,8 +3,9 @@
|
||||
## Language specific
|
||||
|
||||
- Rust doesn't use the 'new' keyword
|
||||
- Classes in Rust require all attributes to be defined at all times, so you have to define all the data on creation.
|
||||
- Classes in Rust require all attributes to be defined at all times, so you have
|
||||
to define all the data on creation.
|
||||
|
||||
Relevant Files
|
||||
|
||||
- [main.rs](./src/main.rs)
|
||||
- [main.rs](./classes/src/main.rs)
|
5
rust/instances/Cargo.lock
generated
5
rust/instances/Cargo.lock
generated
@ -1,5 +0,0 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "rust-oop"
|
||||
version = "0.1.0"
|
@ -1,9 +0,0 @@
|
||||
[package]
|
||||
name = "rust-oop"
|
||||
version = "0.1.0"
|
||||
authors = ["Timo Hocker <t-hocker@web.de>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
@ -1,24 +0,0 @@
|
||||
/*
|
||||
|
||||
Basic Car class
|
||||
just here to make the demonstration in the main file possible
|
||||
explanation of the code here will be given in later lessons
|
||||
|
||||
*/
|
||||
|
||||
pub trait Information {
|
||||
fn print_info(&self);
|
||||
}
|
||||
|
||||
pub struct Car {
|
||||
pub manufacturer: String,
|
||||
pub model: String
|
||||
}
|
||||
|
||||
impl Information for Car {
|
||||
fn print_info(&self) {
|
||||
println!("Car Information:");
|
||||
println!("- manufacturer: {}", self.manufacturer);
|
||||
println!("- model: {}", self.model);
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
mod car;
|
||||
use car::Information;
|
||||
|
||||
fn main() {
|
||||
// create a new car and store it in the variable 'a'
|
||||
// rust also doesn't use the 'new' keyword
|
||||
// all attributes have to be defined upon creation of the object
|
||||
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();
|
||||
}
|
5
typescript/Instances.md
Normal file
5
typescript/Instances.md
Normal file
@ -0,0 +1,5 @@
|
||||
# Typescript Example
|
||||
|
||||
Relevant Files
|
||||
|
||||
- [index.ts](./classes/index.ts)
|
@ -6,7 +6,7 @@ export class Car {
|
||||
manufacturer: string = '';
|
||||
model: string = '';
|
||||
|
||||
// define the function print_info
|
||||
// define the method print_info
|
||||
print_info() {
|
||||
console.log('Car Information:');
|
||||
console.log(`- manufacturer: ${this.manufacturer}`);
|
||||
|
@ -1,5 +1,6 @@
|
||||
# Typescript Example
|
||||
|
||||
Relevant Files
|
||||
## Relevant Files
|
||||
|
||||
- [index.ts](./index.ts)
|
||||
- [car.ts](./car.ts)
|
23
typescript/constructors/car.ts
Normal file
23
typescript/constructors/car.ts
Normal file
@ -0,0 +1,23 @@
|
||||
/**
|
||||
* the class is defined with the export keyword to make it usable outside of the current file
|
||||
*/
|
||||
export class Car {
|
||||
// define the fields
|
||||
// setting them to private to prevent access from outside
|
||||
private manufacturer: string = '';
|
||||
private model: string = '';
|
||||
|
||||
// in typescript constructors are defined with the keyword constructor
|
||||
public constructor(manufacturer: string, model: string) {
|
||||
// set manufacturer and model of the current object to the given parameters
|
||||
this.manufacturer = manufacturer;
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
// define the method print_info
|
||||
print_info() {
|
||||
console.log('Car Information:');
|
||||
console.log(`- manufacturer: ${this.manufacturer}`);
|
||||
console.log(`- model: ${this.model}`);
|
||||
}
|
||||
}
|
@ -1,15 +1,11 @@
|
||||
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';
|
||||
// the constructor requires the parameters for model and manufacturer
|
||||
const a = new Car('Benz','Velo');
|
||||
|
||||
// do the same for a second car
|
||||
const b = new Car();
|
||||
b.manufacturer = 'Ford';
|
||||
b.model = 'Model T';
|
||||
const b = new Car('Ford','Model T');
|
||||
|
||||
// use a function of the car class to print out the information
|
||||
a.print_info();
|
@ -1,18 +0,0 @@
|
||||
/*
|
||||
|
||||
Basic Car class
|
||||
just here to make the demonstration in the main file possible
|
||||
explanation of the code here will be given in later lessons
|
||||
|
||||
*/
|
||||
|
||||
export class Car {
|
||||
manufacturer: string = '';
|
||||
model: string = '';
|
||||
|
||||
print_info() {
|
||||
console.log('Car Information:');
|
||||
console.log(`- manufacturer: ${this.manufacturer}`);
|
||||
console.log(`- model: ${this.model}`);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user