more lessons

This commit is contained in:
2020-05-24 17:04:37 +02:00
parent 5abcc6ed5a
commit 804adb3b89
25 changed files with 348 additions and 1 deletions

17
cpp/inheritance/README.md Normal file
View File

@ -0,0 +1,17 @@
# Inheritance
c++ also allows to inherit from multiple base classes at once, but that is a
little to complicated for this lesson.
Relevant files:
- [main.cpp](./index.cpp)
- [animal.h](./animal.h)
- [animal.cpp](./animal.cpp)
- [cat.h](./cat.h)
- [cat.cpp](./cat.cpp)
- [dog.h](./dog.h)
- [dog.cpp](./dog.cpp)

View 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;
}

12
cpp/inheritance/animal.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include <string>
using namespace std;
class animal
{
public:
std::string name;
virtual void make_sound();
animal(std::string name);
};

9
cpp/inheritance/cat.cpp Normal file
View File

@ -0,0 +1,9 @@
#include "cat.h"
#include <iostream>
#include <string>
void cat::make_sound() {
animal::make_sound();
cout << "meow" << endl;
}

12
cpp/inheritance/cat.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include <string>
#include "animal.h"
using namespace std;
class cat: public animal
{
using animal::animal;
public:
void make_sound() override;
};

9
cpp/inheritance/dog.cpp Normal file
View File

@ -0,0 +1,9 @@
#include "dog.h"
#include <iostream>
#include <string>
void dog::make_sound() {
cout << "doggo " << name << " says:" << endl;
cout << "woof!" << endl;
}

12
cpp/inheritance/dog.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include <string>
#include "animal.h"
using namespace std;
class dog: public animal
{
using animal::animal;
public:
void make_sound() override;
};

12
cpp/inheritance/main.cpp Normal file
View File

@ -0,0 +1,12 @@
#include "cat.h"
#include "dog.h"
#include <string>
int main() {
dog d("buster");
cat c("mittens");
d.make_sound();
c.make_sound();
}