more lessons
This commit is contained in:
17
cpp/inheritance/README.md
Normal file
17
cpp/inheritance/README.md
Normal 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)
|
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;
|
||||
}
|
12
cpp/inheritance/animal.h
Normal file
12
cpp/inheritance/animal.h
Normal 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
9
cpp/inheritance/cat.cpp
Normal 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
12
cpp/inheritance/cat.h
Normal 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
9
cpp/inheritance/dog.cpp
Normal 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
12
cpp/inheritance/dog.h
Normal 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
12
cpp/inheritance/main.cpp
Normal 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();
|
||||
}
|
Reference in New Issue
Block a user