Compare commits
No commits in common. "8055085c1fe652c13c4c2c70af01ab5d70d29e90" and "f6e9115e2ca6b1bbedf74450fa6e3d6031571251" have entirely different histories.
8055085c1f
...
f6e9115e2c
@ -13,14 +13,13 @@ Lessons:
|
|||||||
|
|
||||||
- [instanciating objects, accessing attributes and methods](./lessons/Instances.md)
|
- [instanciating objects, accessing attributes and methods](./lessons/Instances.md)
|
||||||
- [creating a class](./lessons/Classes.md)
|
- [creating a class](./lessons/Classes.md)
|
||||||
- [constructors](./lessons/Constructors.md)
|
|
||||||
|
|
||||||
Work in progress:
|
Work in progress:
|
||||||
|
|
||||||
- inheritance
|
- constructors
|
||||||
- encapsulation
|
- encapsulation
|
||||||
- getter / setter / property
|
- getter / setter / property
|
||||||
|
- inheritance
|
||||||
- polymorphism
|
- polymorphism
|
||||||
- virtual classes
|
- virtual classes
|
||||||
- interfaces
|
- interfaces
|
||||||
- Factories
|
|
||||||
|
@ -1,10 +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](./classes/main.cpp)
|
|
@ -1,8 +1,7 @@
|
|||||||
# Classes in C++
|
# Classes in C++
|
||||||
|
|
||||||
In c++ classes are defined using a header file and the implementation in a
|
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
|
regular .cpp file. Explanations to the structure can be found in the files themselves
|
||||||
themselves
|
|
||||||
|
|
||||||
## Relevant files
|
## Relevant files
|
||||||
|
|
||||||
|
@ -24,11 +24,11 @@ class car
|
|||||||
// define the visibility of the following fields and methods
|
// define the visibility of the following fields and methods
|
||||||
// in this case everything is public since visibility is a topic in future lessons
|
// in this case everything is public since visibility is a topic in future lessons
|
||||||
public:
|
public:
|
||||||
// define two fields, manufacturer and model
|
// define two strings, manufacturer and model
|
||||||
std::string manufacturer;
|
std::string manufacturer;
|
||||||
std::string model;
|
std::string model;
|
||||||
|
|
||||||
// define a method that will print out information
|
// define a function that will print out information
|
||||||
void print_info();
|
void print_info();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,7 +0,0 @@
|
|||||||
# Constructors in C++
|
|
||||||
|
|
||||||
## Relevant files
|
|
||||||
|
|
||||||
- [car.h](./car.h)
|
|
||||||
- [car.cpp](./car.cpp)
|
|
||||||
- [main.cpp](./main.cpp)
|
|
@ -1,23 +0,0 @@
|
|||||||
// 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;
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
#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();
|
|
||||||
};
|
|
11
cpp/instances/README.md
Normal file
11
cpp/instances/README.md
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# 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)
|
20
cpp/instances/car.cpp
Normal file
20
cpp/instances/car.cpp
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
20
cpp/instances/car.h
Normal file
20
cpp/instances/car.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
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();
|
||||||
|
};
|
@ -3,11 +3,16 @@
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// create a new car and store it in the variable 'a'
|
// create a new car and store it in the variable 'a'
|
||||||
// The constructor requires manufacturer and model to be specified.
|
// in contrast to java, c++ does not need initialization with the 'new' keyword here.
|
||||||
car a("Benz", "Velo");
|
car a;
|
||||||
|
// set some data for that car
|
||||||
|
a.manufacturer = "Benz";
|
||||||
|
a.model = "Velo";
|
||||||
|
|
||||||
// do the same for a second car
|
// do the same for a second car
|
||||||
car b("Ford", "Model T");
|
car b;
|
||||||
|
b.manufacturer = "Ford";
|
||||||
|
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
|
||||||
a.print_info();
|
a.print_info();
|
@ -1,5 +0,0 @@
|
|||||||
# Java Example
|
|
||||||
|
|
||||||
Relevant Files:
|
|
||||||
|
|
||||||
- [main.java](./classes/main.java)
|
|
@ -8,7 +8,6 @@ public class Car {
|
|||||||
public String model = "";
|
public String model = "";
|
||||||
public String manufacturer = "";
|
public String manufacturer = "";
|
||||||
|
|
||||||
// create the method print_info()
|
|
||||||
public void print_info() {
|
public void print_info() {
|
||||||
System.out.println("Car Information:");
|
System.out.println("Car Information:");
|
||||||
/**
|
/**
|
||||||
|
@ -1,23 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
# Java example
|
|
||||||
|
|
||||||
Relevant files:
|
|
||||||
|
|
||||||
- [main.java](./main.java)
|
|
||||||
- [Car.java](./Car.java)
|
|
18
java/instances/Car.java
Normal file
18
java/instances/Car.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
5
java/instances/README.md
Normal file
5
java/instances/README.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# Java Example
|
||||||
|
|
||||||
|
Relevant Files:
|
||||||
|
|
||||||
|
- [main.java](./main.java)
|
@ -1,11 +1,15 @@
|
|||||||
public class main {
|
public class main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// create a new car and store it in the variable 'a'
|
// create a new car and store it in the variable 'a'
|
||||||
// the constructor requires manufacturer and model to be specified
|
Car a = new Car();
|
||||||
Car a = new Car("Benz", "Velo");
|
// set some data for that car
|
||||||
|
a.manufacturer = "Benz";
|
||||||
|
a.model = "Velo";
|
||||||
|
|
||||||
// do the same for a second car
|
// do the same for a second car
|
||||||
Car b = new Car("Ford", "Model T");
|
Car b = new Car();
|
||||||
|
b.manufacturer = "Ford";
|
||||||
|
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
|
||||||
a.print_info();
|
a.print_info();
|
@ -1,14 +0,0 @@
|
|||||||
# 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
|
to perform actions on them without having to account for all the logic at the
|
||||||
time of using those functions.
|
time of using those functions.
|
||||||
|
|
||||||
As an example I have created a Car class. This is probably going to be the main
|
As an example I have created a Car class. This is probably going to be the
|
||||||
class used in all further lessons, since it has a lot of room for more data and
|
main class used in all further lessons, since it has a lot of room for more data
|
||||||
functions to add except for manufacturer, model and the function print_info in
|
and functions to add except for manufacturer, model and the function print_info
|
||||||
this example.
|
in this example.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
/*
|
/*
|
||||||
@ -47,8 +47,7 @@ a.print_info();
|
|||||||
b.print_info();
|
b.print_info();
|
||||||
```
|
```
|
||||||
|
|
||||||
The above example code together with the car class will create the following
|
The above example code together with the car class will create the following output:
|
||||||
output:
|
|
||||||
|
|
||||||
```text
|
```text
|
||||||
Car Information:
|
Car Information:
|
||||||
@ -65,7 +64,7 @@ repository :)
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
- [Java](../java/Instances.md)
|
- [Java](../java/instances/README.md)
|
||||||
- [Rust](../rust/Instances.md)
|
- [Rust](../rust/instances/README.md)
|
||||||
- [C++](../cpp/Instances.md)
|
- [C++](../cpp/instances/README.md)
|
||||||
- [JavaScript (using TypeScript)](../typescript/Instances.md)
|
- [JavaScript (using TypeScript)](../typescript/instances/README.md)
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
# 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)
|
|
5
rust/instances/Cargo.lock
generated
Normal file
5
rust/instances/Cargo.lock
generated
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
[[package]]
|
||||||
|
name = "rust-oop"
|
||||||
|
version = "0.1.0"
|
9
rust/instances/Cargo.toml
Normal file
9
rust/instances/Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[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]
|
@ -3,9 +3,8 @@
|
|||||||
## Language specific
|
## Language specific
|
||||||
|
|
||||||
- Rust doesn't use the 'new' keyword
|
- Rust doesn't use the 'new' keyword
|
||||||
- Classes in Rust require all attributes to be defined at all times, so you have
|
- Classes in Rust require all attributes to be defined at all times, so you have to define all the data on creation.
|
||||||
to define all the data on creation.
|
|
||||||
|
|
||||||
Relevant Files
|
Relevant Files
|
||||||
|
|
||||||
- [main.rs](./classes/src/main.rs)
|
- [main.rs](./src/main.rs)
|
24
rust/instances/src/car.rs
Normal file
24
rust/instances/src/car.rs
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
23
rust/instances/src/main.rs
Normal file
23
rust/instances/src/main.rs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
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();
|
||||||
|
}
|
@ -1,5 +0,0 @@
|
|||||||
# Typescript Example
|
|
||||||
|
|
||||||
Relevant Files
|
|
||||||
|
|
||||||
- [index.ts](./classes/index.ts)
|
|
@ -6,7 +6,7 @@ export class Car {
|
|||||||
manufacturer: string = '';
|
manufacturer: string = '';
|
||||||
model: string = '';
|
model: string = '';
|
||||||
|
|
||||||
// define the method print_info
|
// define the function print_info
|
||||||
print_info() {
|
print_info() {
|
||||||
console.log('Car Information:');
|
console.log('Car Information:');
|
||||||
console.log(`- manufacturer: ${this.manufacturer}`);
|
console.log(`- manufacturer: ${this.manufacturer}`);
|
||||||
|
@ -1,23 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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,6 +1,5 @@
|
|||||||
# Typescript Example
|
# Typescript Example
|
||||||
|
|
||||||
## Relevant Files
|
Relevant Files
|
||||||
|
|
||||||
- [index.ts](./index.ts)
|
- [index.ts](./index.ts)
|
||||||
- [car.ts](./car.ts)
|
|
18
typescript/instances/car.ts
Normal file
18
typescript/instances/car.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
/*
|
||||||
|
|
||||||
|
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}`);
|
||||||
|
}
|
||||||
|
}
|
@ -1,11 +1,15 @@
|
|||||||
import {Car} from './car'
|
import {Car} from './car'
|
||||||
|
|
||||||
// create a new car and store it in the variable 'a'
|
// create a new car and store it in the variable 'a'
|
||||||
// the constructor requires the parameters for model and manufacturer
|
const a = new Car();
|
||||||
const a = new Car('Benz','Velo');
|
// set some data for that car
|
||||||
|
a.manufacturer = 'Benz';
|
||||||
|
a.model = 'Velo';
|
||||||
|
|
||||||
// do the same for a second car
|
// do the same for a second car
|
||||||
const b = new Car('Ford','Model T');
|
const b = new Car();
|
||||||
|
b.manufacturer = 'Ford';
|
||||||
|
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
|
||||||
a.print_info();
|
a.print_info();
|
Loading…
x
Reference in New Issue
Block a user