Compare commits
19 Commits
551e8835d7
...
master
Author | SHA1 | Date | |
---|---|---|---|
ec1ebcaed6 | |||
f6e6180958 | |||
608dafaf85 | |||
a0bdd99b3f | |||
0d55cf1273 | |||
804adb3b89 | |||
5abcc6ed5a | |||
8055085c1f | |||
f2df87ecfd | |||
9efaf7b5f9 | |||
4cacd3afa1 | |||
b9c03f8207 | |||
d6f2d44eaf | |||
d30589c5ad | |||
c6246aade2 | |||
5aad2f7644 | |||
f6e9115e2c | |||
f5c60a26f2 | |||
8143d66e49 |
6
.vscode/settings.json
vendored
Normal file
6
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"system_error": "cpp",
|
||||||
|
"xlocale": "cpp"
|
||||||
|
}
|
||||||
|
}
|
15
README.md
15
README.md
@ -12,15 +12,16 @@ Example Languages:
|
|||||||
Lessons:
|
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)
|
||||||
|
- [constructors](./lessons/Constructors.md)
|
||||||
|
- [encapsulation](./lessons/Encapsulation.md)
|
||||||
|
- [inheritance](./lessons/Inheritance.md)
|
||||||
|
- [properties](./lessons/Properties.md)
|
||||||
|
- [polymorphism](./lessons/Polymorphism.md)
|
||||||
|
|
||||||
Work in progress:
|
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
|
- virtual classes
|
||||||
- interfaces
|
- interfaces
|
||||||
|
- Factories
|
||||||
|
- Generics
|
||||||
|
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)
|
5
cpp/Polymorphism.md
Normal file
5
cpp/Polymorphism.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# Polymorphism
|
||||||
|
|
||||||
|
Relevant files:
|
||||||
|
|
||||||
|
- [main.cpp](./inheritance/main.cpp)
|
10
cpp/classes/README.md
Normal file
10
cpp/classes/README.md
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
# 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
|
||||||
|
|
||||||
|
## Relevant files
|
||||||
|
|
||||||
|
- [car.h](./car.h)
|
||||||
|
- [car.cpp](./car.cpp)
|
23
cpp/classes/car.cpp
Normal file
23
cpp/classes/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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
That's everything for the .cpp file.
|
||||||
|
Since fields don't have an implementation and are already defined in the header file, they can be left out here.
|
||||||
|
|
||||||
|
*/
|
40
cpp/classes/car.h
Normal file
40
cpp/classes/car.h
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
// #pragma once is usually included in header files to make sure they are only loaded once
|
||||||
|
// it's a simpler version of include guards, which are necessary to prevent conflicts
|
||||||
|
#pragma once
|
||||||
|
/*
|
||||||
|
|
||||||
|
in case #pragma once is not supported by your compiler you'll need more basic include guards
|
||||||
|
in this case it would be
|
||||||
|
#ifndef car_h
|
||||||
|
#define car_h
|
||||||
|
|
||||||
|
...class contents
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
// including a necessary library and namespace to work with strings
|
||||||
|
#include <string>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// define the class
|
||||||
|
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 fields, manufacturer and model
|
||||||
|
std::string manufacturer;
|
||||||
|
std::string model;
|
||||||
|
|
||||||
|
// define a method that will print out information
|
||||||
|
void print_info();
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
That completes the header file, as you can see, there are no implementations in here.
|
||||||
|
That's because they will be added in the car.cpp file.
|
||||||
|
|
||||||
|
*/
|
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();
|
||||||
|
};
|
15
cpp/constructors/main.cpp
Normal file
15
cpp/constructors/main.cpp
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#include "car.h"
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// create a new car and store it in the variable 'a'
|
||||||
|
// The constructor requires manufacturer and model to be specified.
|
||||||
|
car a("Benz", "Velo");
|
||||||
|
|
||||||
|
// do the same for a second car
|
||||||
|
car b("Ford", "Model T");
|
||||||
|
|
||||||
|
// use a function of the car class to print out the information
|
||||||
|
a.print_info();
|
||||||
|
b.print_info();
|
||||||
|
}
|
13
cpp/inheritance/README.md
Normal file
13
cpp/inheritance/README.md
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
# Inheritance
|
||||||
|
|
||||||
|
c++ also allows to inherit from multiple base classes at once, but that is a
|
||||||
|
little to complicated for this lesson.
|
||||||
|
|
||||||
|
Relevant files:
|
||||||
|
|
||||||
|
- [animal.h](./animal.h)
|
||||||
|
- [animal.cpp](./animal.cpp)
|
||||||
|
- [cat.h](./cat.h)
|
||||||
|
- [cat.cpp](./cat.cpp)
|
||||||
|
- [dog.h](./dog.h)
|
||||||
|
- [dog.cpp](./dog.cpp)
|
11
cpp/inheritance/animal.cpp
Normal file
11
cpp/inheritance/animal.cpp
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include "animal.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
animal::animal(std::string name) {
|
||||||
|
this->name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
void animal::make_sound() {
|
||||||
|
cout << name << ":" << endl;
|
||||||
|
}
|
13
cpp/inheritance/animal.h
Normal file
13
cpp/inheritance/animal.h
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class animal
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
std::string name;
|
||||||
|
// define make_sound as virtual to allow overriding in subclasses
|
||||||
|
virtual void make_sound();
|
||||||
|
animal(std::string name);
|
||||||
|
};
|
12
cpp/inheritance/cat.cpp
Normal file
12
cpp/inheritance/cat.cpp
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#include "cat.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
void cat::make_sound() {
|
||||||
|
// call the base class function first
|
||||||
|
animal::make_sound();
|
||||||
|
|
||||||
|
// add additional functionality
|
||||||
|
cout << "meow" << endl;
|
||||||
|
}
|
16
cpp/inheritance/cat.h
Normal file
16
cpp/inheritance/cat.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
// include the animal header
|
||||||
|
#include "animal.h"
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// make the class inherit from animal
|
||||||
|
class cat: public animal
|
||||||
|
{
|
||||||
|
// include the animal constructor
|
||||||
|
using animal::animal;
|
||||||
|
public:
|
||||||
|
// override the make_sound function
|
||||||
|
void make_sound() override;
|
||||||
|
};
|
10
cpp/inheritance/dog.cpp
Normal file
10
cpp/inheritance/dog.cpp
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#include "dog.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
void dog::make_sound() {
|
||||||
|
// implement own functionality
|
||||||
|
cout << "doggo " << name << " says:" << endl;
|
||||||
|
cout << "woof!" << endl;
|
||||||
|
}
|
16
cpp/inheritance/dog.h
Normal file
16
cpp/inheritance/dog.h
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
// include the animal header
|
||||||
|
#include "animal.h"
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
// make the class inherit from animal
|
||||||
|
class dog: public animal
|
||||||
|
{
|
||||||
|
// include the animal constructor
|
||||||
|
using animal::animal;
|
||||||
|
public:
|
||||||
|
// override the make_sound function
|
||||||
|
void make_sound() override;
|
||||||
|
};
|
17
cpp/inheritance/main.cpp
Normal file
17
cpp/inheritance/main.cpp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#include "animal.h"
|
||||||
|
#include "cat.h"
|
||||||
|
#include "dog.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// because cat and dog are subclasses of animal, they can be stored in an array of animals
|
||||||
|
animal * animals[2] = {
|
||||||
|
new dog("buster"),
|
||||||
|
new cat("mittens")
|
||||||
|
};
|
||||||
|
|
||||||
|
for (animal * a : animals)
|
||||||
|
// when calling the make_sound function, the implementation of the actual class gets called (in this case cat or dog)
|
||||||
|
a->make_sound();
|
||||||
|
}
|
@ -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)
|
5
java/Polymorphism.md
Normal file
5
java/Polymorphism.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# Polymorphism
|
||||||
|
|
||||||
|
Relevant files
|
||||||
|
|
||||||
|
- [Program.java](./inheritance/Program.java)
|
21
java/classes/Car.java
Normal file
21
java/classes/Car.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
/**
|
||||||
|
* this class is also declared with the public keyword to make it accessible
|
||||||
|
* outside of the current file
|
||||||
|
*/
|
||||||
|
public class Car {
|
||||||
|
|
||||||
|
// define the fields
|
||||||
|
public String model = "";
|
||||||
|
public String manufacturer = "";
|
||||||
|
|
||||||
|
// create the method print_info()
|
||||||
|
public void print_info() {
|
||||||
|
System.out.println("Car Information:");
|
||||||
|
/**
|
||||||
|
* To access fields of the current object the this keyword is used. It is
|
||||||
|
* possible to omit that keyword, but it's recommended to be used anyways.
|
||||||
|
*/
|
||||||
|
System.out.println("- manufacturer: " + this.manufacturer);
|
||||||
|
System.out.println("- model: " + this.model);
|
||||||
|
}
|
||||||
|
}
|
@ -2,4 +2,4 @@
|
|||||||
|
|
||||||
Relevant Files:
|
Relevant Files:
|
||||||
|
|
||||||
- [main.java](./main.java)
|
- [Car.java](./Car.java)
|
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)
|
14
java/constructors/main.java
Normal file
14
java/constructors/main.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
public class main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// create a new car and store it in the variable 'a'
|
||||||
|
// 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("Ford", "Model T");
|
||||||
|
|
||||||
|
// use a function of the car class to print out the information
|
||||||
|
a.print_info();
|
||||||
|
b.print_info();
|
||||||
|
}
|
||||||
|
}
|
11
java/inheritance/Animal.java
Normal file
11
java/inheritance/Animal.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
public class Animal {
|
||||||
|
public String name;
|
||||||
|
|
||||||
|
public Animal(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void make_sound() {
|
||||||
|
System.out.println(this.name + ":");
|
||||||
|
}
|
||||||
|
}
|
17
java/inheritance/Cat.java
Normal file
17
java/inheritance/Cat.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
// make the class cat inherit from Animal
|
||||||
|
public class Cat extends Animal {
|
||||||
|
// java requires an explicit constructor when the supertype awaits parameters in the constructor
|
||||||
|
public Cat(String name) {
|
||||||
|
// call supertype constructor
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// override make_sound
|
||||||
|
public void make_sound() {
|
||||||
|
// optional: call the parent class function
|
||||||
|
super.make_sound();
|
||||||
|
|
||||||
|
// add additional functionality
|
||||||
|
System.out.println("meow");
|
||||||
|
}
|
||||||
|
}
|
14
java/inheritance/Dog.java
Normal file
14
java/inheritance/Dog.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
public class Dog extends Animal {
|
||||||
|
// java requires an explicit constructor when the supertype awaits parameters in the constructor
|
||||||
|
public Dog(String name) {
|
||||||
|
// call supertype constructor
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// override the method make_sound
|
||||||
|
public void make_sound() {
|
||||||
|
// implement own functionality
|
||||||
|
System.out.println("doggo " + this.name + " says:");
|
||||||
|
System.out.println("woof!");
|
||||||
|
}
|
||||||
|
}
|
14
java/inheritance/Program.java
Normal file
14
java/inheritance/Program.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
public class Program {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// because cat and dog are subclasses of animal, they can be stored in an array of animals
|
||||||
|
Animal[] animals = {
|
||||||
|
new Dog("buster"),
|
||||||
|
new Cat("mittens")
|
||||||
|
};
|
||||||
|
|
||||||
|
for (Animal a : animals) {
|
||||||
|
// when calling the make_sound function, the implementation of the actual class gets called (in this case cat or dog)
|
||||||
|
a.make_sound();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
java/inheritance/README.md
Normal file
7
java/inheritance/README.md
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# Inheritance
|
||||||
|
|
||||||
|
Relevant files:
|
||||||
|
|
||||||
|
- [Animal.java](./Animal.java)
|
||||||
|
- [Cat.java](./Cat.java)
|
||||||
|
- [Dog.java](./Dog.java)
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
25
lessons/Classes.md
Normal file
25
lessons/Classes.md
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
# Creating Classes
|
||||||
|
|
||||||
|
This lesson is going to use the same code from last time to explain the class
|
||||||
|
structure itself.
|
||||||
|
|
||||||
|
Since the example languages can have quite different styles of class
|
||||||
|
declaration, I will just give a basic explanation here and then discuss every
|
||||||
|
language in detail in their own description file.
|
||||||
|
|
||||||
|
Classes are blueprints for the objects you create. You define how the object
|
||||||
|
will look like and more importantly for the computer how much space it will use
|
||||||
|
in memory.
|
||||||
|
|
||||||
|
The main parts of a class are fields and methods. One for storing data and one
|
||||||
|
for processing that data. Fields are placed at the top of the class and look
|
||||||
|
like general variable declarations. Methods can take in arguments and also use
|
||||||
|
the fields declared in the class. They can modify data in the object or just
|
||||||
|
return some data to the caller.
|
||||||
|
|
||||||
|
## Code examples and language specific explanations
|
||||||
|
|
||||||
|
- [Java](../java/classes/README.md)
|
||||||
|
- [Rust](../rust/classes/README.md)
|
||||||
|
- [C++](../cpp/classes/README.md)
|
||||||
|
- [JavaScript (using TypeScript)](../typescript/classes/README.md)
|
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)
|
32
lessons/Encapsulation.md
Normal file
32
lessons/Encapsulation.md
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
# Encapsulation
|
||||||
|
|
||||||
|
Encapsulation is a simple lesson, this one is not gonna need any code examples.
|
||||||
|
|
||||||
|
There are typically 3 keywords you need for encapsulation: private, protected
|
||||||
|
and public.
|
||||||
|
|
||||||
|
## Private
|
||||||
|
|
||||||
|
Private methods and fields are only acessible from within the same class, they
|
||||||
|
can't be read or written from outside.
|
||||||
|
|
||||||
|
## Protected
|
||||||
|
|
||||||
|
Protected is just like private, but descendant classes can access them.
|
||||||
|
|
||||||
|
## Public
|
||||||
|
|
||||||
|
Public is accessible from anywhere.
|
||||||
|
|
||||||
|
## The static modifier
|
||||||
|
|
||||||
|
Static is another one of those keywords, although it doesn't really control
|
||||||
|
access to fields and methods. Anything with the static keyword can be used
|
||||||
|
without an instance of the class. That means you can directly access with with
|
||||||
|
Class.property instead of creating an instance of that class first. This is used
|
||||||
|
for example in java for the main method, since it needs to be accessible without
|
||||||
|
running any of the programs code first.
|
||||||
|
|
||||||
|
Encapsulation is not a safety feature, it's only to make clear, which properties
|
||||||
|
should be used by others. Methods like reflection and directly reading/writing
|
||||||
|
memory can still access private or protected properties.
|
15
lessons/Inheritance.md
Normal file
15
lessons/Inheritance.md
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
# Inheritance
|
||||||
|
|
||||||
|
Inheritance is useful to minimize code duplicates and generalize types in base
|
||||||
|
classes.
|
||||||
|
|
||||||
|
A Class can inherit all public and protected functions and fields from a base
|
||||||
|
class and add its own or override existing ones. Private fields and functions
|
||||||
|
still exist in the base class, but can't be acessed from the extending class.
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
- [Java](../java/inheritance/README.md)
|
||||||
|
- [Rust](../rust/Inheritance.md)
|
||||||
|
- [C++](../cpp/inheritance/README.md)
|
||||||
|
- [JavaScript (using TypeScript)](../typescript/inheritance/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
|
As an example I have created a Car class. This is probably going to be the main
|
||||||
main class used in all further lessons, since it has a lot of room for more data
|
class used in all further lessons, since it has a lot of room for more data and
|
||||||
and functions to add except for manufacturer, model and the function print_info
|
functions to add except for manufacturer, model and the function print_info in
|
||||||
in this example.
|
this example.
|
||||||
|
|
||||||
```java
|
```java
|
||||||
/*
|
/*
|
||||||
@ -47,7 +47,8 @@ a.print_info();
|
|||||||
b.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
|
```text
|
||||||
Car Information:
|
Car Information:
|
||||||
@ -64,7 +65,7 @@ repository :)
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
- [Java](../java/instances/README.md)
|
- [Java](../java/Instances.md)
|
||||||
- [Rust](../rust/instances/README.md)
|
- [Rust](../rust/Instances.md)
|
||||||
- [C++](../cpp/instances/README.md)
|
- [C++](../cpp/Instances.md)
|
||||||
- [JavaScript (using TypeScript)](../typescript/instances/README.md)
|
- [JavaScript (using TypeScript)](../typescript/Instances.md)
|
||||||
|
19
lessons/Polymorphism.md
Normal file
19
lessons/Polymorphism.md
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# Polymorphism
|
||||||
|
|
||||||
|
Polymorphism is the ability to treat subclasses like their base classes, for
|
||||||
|
example passing cat, which extends animal to a function that expects an animal
|
||||||
|
as parameter. or storing objects of different types that all extend a common
|
||||||
|
class in an array of that common type.
|
||||||
|
|
||||||
|
You lose acess to the properties of the subclass, but you can still access the
|
||||||
|
base class like usual. When the subclass overrides behaviour of the base class,
|
||||||
|
the overridden functionality in the subclass is still called.
|
||||||
|
|
||||||
|
You can regain access to properties of the subclass with casting, but you have
|
||||||
|
to make sure that the object is actually an instance of that class or you might
|
||||||
|
run into runtime errors.
|
||||||
|
|
||||||
|
- [Java](../java/Polymorphism.md)
|
||||||
|
- [Rust](../rust/Inheritance.md)
|
||||||
|
- [C++](../cpp/Polymorphism.md)
|
||||||
|
- [JavaScript (using TypeScript)](../typescript/Polymorphism.md)
|
61
lessons/Properties.md
Normal file
61
lessons/Properties.md
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
# Properties
|
||||||
|
|
||||||
|
Properties are a way to restrict read/write access to a field and to add
|
||||||
|
additional verifications, computations or function calls to accessing a field.
|
||||||
|
|
||||||
|
To define a property you need one private field that stores your data and the
|
||||||
|
getter/setter for it. If you don't want it to be readable or writable, you can
|
||||||
|
leave out either of the accessor functions.
|
||||||
|
|
||||||
|
None of the languages featured here have a full implementation of properties.
|
||||||
|
Languages like c# and Delphi do though.
|
||||||
|
|
||||||
|
Delphi has the simplest syntax for properties I've seen so far.
|
||||||
|
|
||||||
|
```pas
|
||||||
|
// this property will read from the field FFoo and write using the function set_foo
|
||||||
|
property Foo read FFoo write set_foo;
|
||||||
|
```
|
||||||
|
|
||||||
|
C# is a bit more complicated, but still better than the languages here
|
||||||
|
|
||||||
|
```cs
|
||||||
|
public string Foo
|
||||||
|
{
|
||||||
|
get => _foo;
|
||||||
|
set {
|
||||||
|
_foo = do_something(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Typescript
|
||||||
|
|
||||||
|
Typescript allows a somewhat property implementation. In the class itself you
|
||||||
|
define getter and setter functions using the get and set keywords
|
||||||
|
|
||||||
|
```ts
|
||||||
|
public get foo(): string {
|
||||||
|
return this._foo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public set foo(val: string): void {
|
||||||
|
this._foo = do_something(val);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
From the outside, the property can be read and written like in c# and Delphi,
|
||||||
|
just like working with a variable.
|
||||||
|
|
||||||
|
## C++, Java and Rust
|
||||||
|
|
||||||
|
Those languages don't provide a property syntax at all, instead you have to
|
||||||
|
create public functions like set_foo and get_foo to replace them.
|
||||||
|
|
||||||
|
## Additional note to rust
|
||||||
|
|
||||||
|
Properties don't exist in rust either. But replacing them with getters and
|
||||||
|
setters is also discouraged. Quoting from a reddit post
|
||||||
|
`Expose behavior, not state`. That means you should present functions to outside
|
||||||
|
viewers and not your plain data. An object should be able to do all its intended
|
||||||
|
functions itself without having an outside observer read or write its data.
|
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)
|
6
rust/Inheritance.md
Normal file
6
rust/Inheritance.md
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
# Inheritance and Polymorphism
|
||||||
|
|
||||||
|
Rust does not allow extending classes. Instead the base class is stored as a
|
||||||
|
field in the extending class. Polymorphism can then be achieved by passing the
|
||||||
|
field instead of the whole object to a function. Although casting back to the
|
||||||
|
advanced class or overriding functions is not possible.
|
@ -3,8 +3,9 @@
|
|||||||
## 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 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
|
Relevant Files
|
||||||
|
|
||||||
- [main.rs](./src/main.rs)
|
- [main.rs](./classes/src/main.rs)
|
11
rust/classes/README.md
Normal file
11
rust/classes/README.md
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
# Classes in Rust
|
||||||
|
|
||||||
|
Rust doesn't have classes like Java or other typical c style languages. Instead
|
||||||
|
there are Structs and Traits.
|
||||||
|
|
||||||
|
Structs are responsible for storing data and nothing else. Traits define
|
||||||
|
functions that can be used.
|
||||||
|
|
||||||
|
## Relevant Files
|
||||||
|
|
||||||
|
- [car.rs](./src/car.rs)
|
27
rust/classes/src/car.rs
Normal file
27
rust/classes/src/car.rs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
* Define the trait Information that will contain the function print_info()
|
||||||
|
*/
|
||||||
|
pub trait Information {
|
||||||
|
// define the function, &self is an argument that will be autofilled
|
||||||
|
// when running the function and it will reference the current object
|
||||||
|
fn print_info(&self);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Define the struct Car, which will hold all the necessary information
|
||||||
|
*/
|
||||||
|
pub struct Car {
|
||||||
|
pub manufacturer: String,
|
||||||
|
pub model: String
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Implementing the trait Information for the struct Car
|
||||||
|
*/
|
||||||
|
impl Information for Car {
|
||||||
|
fn print_info(&self) {
|
||||||
|
println!("Car Information:");
|
||||||
|
println!("- manufacturer: {}", self.manufacturer);
|
||||||
|
println!("- model: {}", self.model);
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
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)
|
5
typescript/Polymorphism.md
Normal file
5
typescript/Polymorphism.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# Polymorphism
|
||||||
|
|
||||||
|
Relevant files
|
||||||
|
|
||||||
|
- [index.ts](./inheritance/index.ts)
|
5
typescript/classes/README.md
Normal file
5
typescript/classes/README.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# Typescript Example
|
||||||
|
|
||||||
|
## Relevant Files
|
||||||
|
|
||||||
|
- [car.ts](./car.ts)
|
@ -1,15 +1,12 @@
|
|||||||
/*
|
/**
|
||||||
|
* the class is defined with the export keyword to make it usable outside of the current file
|
||||||
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 {
|
export class Car {
|
||||||
|
// define the fields
|
||||||
manufacturer: string = '';
|
manufacturer: string = '';
|
||||||
model: string = '';
|
model: string = '';
|
||||||
|
|
||||||
|
// define the method 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,5 +1,6 @@
|
|||||||
# Typescript Example
|
# Typescript Example
|
||||||
|
|
||||||
Relevant Files
|
## Relevant Files
|
||||||
|
|
||||||
- [index.ts](./index.ts)
|
- [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}`);
|
||||||
|
}
|
||||||
|
}
|
12
typescript/constructors/index.ts
Normal file
12
typescript/constructors/index.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import {Car} from './car'
|
||||||
|
|
||||||
|
// create a new car and store it in the variable 'a'
|
||||||
|
// 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('Ford','Model T');
|
||||||
|
|
||||||
|
// use a function of the car class to print out the information
|
||||||
|
a.print_info();
|
||||||
|
b.print_info();
|
11
typescript/constructors/package.json
Normal file
11
typescript/constructors/package.json
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"name": "ts-oop",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC"
|
||||||
|
}
|
66
typescript/constructors/tsconfig.json
Normal file
66
typescript/constructors/tsconfig.json
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
/* Basic Options */
|
||||||
|
// "incremental": true, /* Enable incremental compilation */
|
||||||
|
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
|
||||||
|
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
|
||||||
|
// "lib": [], /* Specify library files to be included in the compilation. */
|
||||||
|
// "allowJs": true, /* Allow javascript files to be compiled. */
|
||||||
|
// "checkJs": true, /* Report errors in .js files. */
|
||||||
|
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
|
||||||
|
// "declaration": true, /* Generates corresponding '.d.ts' file. */
|
||||||
|
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
|
||||||
|
// "sourceMap": true, /* Generates corresponding '.map' file. */
|
||||||
|
// "outFile": "./dist", /* Concatenate and emit output to single file. */
|
||||||
|
"outDir": "./dist", /* Redirect output structure to the directory. */
|
||||||
|
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
|
||||||
|
// "composite": true, /* Enable project compilation */
|
||||||
|
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
|
||||||
|
// "removeComments": true, /* Do not emit comments to output. */
|
||||||
|
// "noEmit": true, /* Do not emit outputs. */
|
||||||
|
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
|
||||||
|
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
|
||||||
|
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
|
||||||
|
|
||||||
|
/* Strict Type-Checking Options */
|
||||||
|
"strict": true, /* Enable all strict type-checking options. */
|
||||||
|
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
|
||||||
|
// "strictNullChecks": true, /* Enable strict null checks. */
|
||||||
|
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
|
||||||
|
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
|
||||||
|
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
|
||||||
|
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
|
||||||
|
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
||||||
|
|
||||||
|
/* Additional Checks */
|
||||||
|
// "noUnusedLocals": true, /* Report errors on unused locals. */
|
||||||
|
// "noUnusedParameters": true, /* Report errors on unused parameters. */
|
||||||
|
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
|
||||||
|
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
||||||
|
|
||||||
|
/* Module Resolution Options */
|
||||||
|
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
||||||
|
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
|
||||||
|
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
||||||
|
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
||||||
|
// "typeRoots": [], /* List of folders to include type definitions from. */
|
||||||
|
// "types": [], /* Type declaration files to be included in compilation. */
|
||||||
|
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
|
||||||
|
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
|
||||||
|
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
|
||||||
|
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
||||||
|
|
||||||
|
/* Source Map Options */
|
||||||
|
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
|
||||||
|
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
||||||
|
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
|
||||||
|
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
|
||||||
|
|
||||||
|
/* Experimental Options */
|
||||||
|
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
|
||||||
|
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
|
||||||
|
|
||||||
|
/* Advanced Options */
|
||||||
|
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
|
||||||
|
}
|
||||||
|
}
|
7
typescript/inheritance/README.md
Normal file
7
typescript/inheritance/README.md
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# Inheritance
|
||||||
|
|
||||||
|
Relevant files:
|
||||||
|
|
||||||
|
- [animal.ts](./animal.ts)
|
||||||
|
- [cat.ts](./cat.ts)
|
||||||
|
- [dog.ts](./dog.ts)
|
11
typescript/inheritance/animal.ts
Normal file
11
typescript/inheritance/animal.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
export class Animal {
|
||||||
|
public name: string;
|
||||||
|
|
||||||
|
public constructor(name: string) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public make_sound() {
|
||||||
|
console.log(`${this.name}:`);
|
||||||
|
}
|
||||||
|
}
|
13
typescript/inheritance/cat.ts
Normal file
13
typescript/inheritance/cat.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import {Animal} from './animal';
|
||||||
|
|
||||||
|
// make the class cat inherit from Animal
|
||||||
|
export class Cat extends Animal {
|
||||||
|
// override make_sound
|
||||||
|
public make_sound() {
|
||||||
|
// optional: call the parent class function
|
||||||
|
super.make_sound();
|
||||||
|
|
||||||
|
// add additional functionality
|
||||||
|
console.log('meow');
|
||||||
|
}
|
||||||
|
}
|
11
typescript/inheritance/dog.ts
Normal file
11
typescript/inheritance/dog.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import {Animal} from './animal';
|
||||||
|
|
||||||
|
export class Dog extends Animal {
|
||||||
|
|
||||||
|
// override the method make_sound
|
||||||
|
public make_sound() {
|
||||||
|
// implement own functionality
|
||||||
|
console.log(`doggo ${this.name} says:`);
|
||||||
|
console.log('woof!');
|
||||||
|
}
|
||||||
|
}
|
14
typescript/inheritance/index.ts
Normal file
14
typescript/inheritance/index.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import { Dog } from "./dog";
|
||||||
|
import { Cat } from "./cat";
|
||||||
|
import { Animal } from "./animal";
|
||||||
|
|
||||||
|
// because cat and dog are subclasses of animal, they can be stored in an array of animals
|
||||||
|
const animals: Animal[] = [
|
||||||
|
new Dog('buster'),
|
||||||
|
new Cat('mittens')
|
||||||
|
]
|
||||||
|
|
||||||
|
for (const a of animals) {
|
||||||
|
// when calling the make_sound function, the implementation of the actual class gets called (in this case cat or dog)
|
||||||
|
a.make_sound();
|
||||||
|
}
|
11
typescript/inheritance/package.json
Normal file
11
typescript/inheritance/package.json
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"name": "ts-oop",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC"
|
||||||
|
}
|
66
typescript/inheritance/tsconfig.json
Normal file
66
typescript/inheritance/tsconfig.json
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
/* Basic Options */
|
||||||
|
// "incremental": true, /* Enable incremental compilation */
|
||||||
|
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
|
||||||
|
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
|
||||||
|
// "lib": [], /* Specify library files to be included in the compilation. */
|
||||||
|
// "allowJs": true, /* Allow javascript files to be compiled. */
|
||||||
|
// "checkJs": true, /* Report errors in .js files. */
|
||||||
|
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
|
||||||
|
// "declaration": true, /* Generates corresponding '.d.ts' file. */
|
||||||
|
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
|
||||||
|
// "sourceMap": true, /* Generates corresponding '.map' file. */
|
||||||
|
// "outFile": "./dist", /* Concatenate and emit output to single file. */
|
||||||
|
"outDir": "./dist", /* Redirect output structure to the directory. */
|
||||||
|
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
|
||||||
|
// "composite": true, /* Enable project compilation */
|
||||||
|
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
|
||||||
|
// "removeComments": true, /* Do not emit comments to output. */
|
||||||
|
// "noEmit": true, /* Do not emit outputs. */
|
||||||
|
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
|
||||||
|
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
|
||||||
|
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
|
||||||
|
|
||||||
|
/* Strict Type-Checking Options */
|
||||||
|
"strict": true, /* Enable all strict type-checking options. */
|
||||||
|
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
|
||||||
|
// "strictNullChecks": true, /* Enable strict null checks. */
|
||||||
|
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
|
||||||
|
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
|
||||||
|
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
|
||||||
|
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
|
||||||
|
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
|
||||||
|
|
||||||
|
/* Additional Checks */
|
||||||
|
// "noUnusedLocals": true, /* Report errors on unused locals. */
|
||||||
|
// "noUnusedParameters": true, /* Report errors on unused parameters. */
|
||||||
|
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
|
||||||
|
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
|
||||||
|
|
||||||
|
/* Module Resolution Options */
|
||||||
|
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
|
||||||
|
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
|
||||||
|
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
|
||||||
|
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
||||||
|
// "typeRoots": [], /* List of folders to include type definitions from. */
|
||||||
|
// "types": [], /* Type declaration files to be included in compilation. */
|
||||||
|
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
|
||||||
|
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
|
||||||
|
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
|
||||||
|
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
||||||
|
|
||||||
|
/* Source Map Options */
|
||||||
|
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
|
||||||
|
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
||||||
|
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
|
||||||
|
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
|
||||||
|
|
||||||
|
/* Experimental Options */
|
||||||
|
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
|
||||||
|
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
|
||||||
|
|
||||||
|
/* Advanced Options */
|
||||||
|
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user