aufgabe 1

This commit is contained in:
Frank Mayer 2024-11-20 17:54:08 +01:00
parent d985865048
commit d281783d58
Signed by: tsukinoko-kun
GPG Key ID: 427B3E61E69C2E51
8 changed files with 125 additions and 31 deletions

22
src/Fahrzeug.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "Fahrzeug.h"
#include <iostream>
void Fahrzeug::Print() {
std::cout << "Hersteller: " << hersteller << std::endl
<< "Modell: " << modell << std::endl
<< "Kennzeichen: " << kennzeichen << std::endl
<< "Reichweite in km: " << reichweiteInKm << std::endl
<< "Ladung in Prozent: " << ladungInProzent << std::endl;
}
Fahrzeug::Fahrzeug(std::string hersteller, std::string modell,
std::string kennzeichen, unsigned int reichweiteInKm,
unsigned int ladungInProzent) {
this->hersteller = hersteller;
this->modell = modell;
this->kennzeichen = kennzeichen;
this->reichweiteInKm = reichweiteInKm;
this->ladungInProzent = ladungInProzent;
}
Fahrzeug::Fahrzeug() {}

23
src/Fahrzeug.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef FAHRZEUG_H
#define FAHRZEUG_H
#include <string>
class Fahrzeug {
public:
void Print();
public:
Fahrzeug(std::string hersteller, std::string modell, std::string kennzeichen,
unsigned int reichweiteInKm, unsigned int ladungInProzent);
Fahrzeug();
protected:
std::string hersteller;
std::string modell;
std::string kennzeichen;
unsigned int reichweiteInKm;
unsigned int ladungInProzent;
};
#endif // FAHRZEUG_H

18
src/Fuhrpark_1.cpp Normal file
View File

@ -0,0 +1,18 @@
#include "Fuhrpark_1.h"
#include "Fahrzeug.h"
#include <stdexcept>
void Fuhrpark_1::Add(const Fahrzeug &arg) {
if (anzElem < Max_Elems) {
pFuhrpark[anzElem] = new Fahrzeug(arg);
++anzElem;
} else {
throw new std::out_of_range("Fuhrpark_1 instalce is full");
}
}
void Fuhrpark_1::Print() const {
for (size_t i = 0; i < anzElem; ++i) {
pFuhrpark[i]->Print();
}
}

17
src/Fuhrpark_1.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef FUHRPARK1_H
#define FUHRPARK1_H
#include "Fahrzeug.h"
class Fuhrpark_1 {
public:
void Add(const Fahrzeug &arg);
void Print() const;
private:
static const size_t Max_Elems = 100;
size_t anzElem;
Fahrzeug *pFuhrpark[Max_Elems];
};
#endif // !FUHRPARK1_H

12
src/Fuhrpark_2.cpp Normal file
View File

@ -0,0 +1,12 @@
#include "Fuhrpark_2.h"
#include "Fahrzeug.h"
void Fuhrpark_2::Add(const Fahrzeug &arg) {
pFuhrpark->push_back(new Fahrzeug(arg));
}
void Fuhrpark_2::Print() const {
for (size_t i = 0; i < pFuhrpark->size(); ++i) {
(*pFuhrpark)[i]->Print();
}
}

16
src/Fuhrpark_2.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef FUHRPARK2_H
#define FUHRPARK2_H
#include "Fahrzeug.h"
#include <vector>
class Fuhrpark_2 {
public:
void Add(const Fahrzeug &arg);
void Print() const;
private:
std::vector<Fahrzeug *> *pFuhrpark = new std::vector<Fahrzeug *>;
};
#endif // !FUHRPARK2_H

View File

@ -1,21 +0,0 @@
#ifndef DEFER_H
#define DEFER_H
#include <functional>
#include <iostream>
class Defer {
public:
Defer(std::function<void()> func) : func_(func) {}
~Defer() { func_(); }
private:
std::function<void()> func_;
};
#define CONCAT_IMPL(a, b) a##b
#define CONCAT(a, b) CONCAT_IMPL(a, b)
#define DEFER(code) Defer CONCAT(defer__, __LINE__)([&]() { code; });
#endif // DEFER_H

View File

@ -1,15 +1,22 @@
#include "defer.h"
#include <iostream>
struct Meep {
std::string foo = "Meep!";
};
#include "Fahrzeug.h"
#include "Fuhrpark_1.h"
#include "Fuhrpark_2.h"
int main() {
Meep *meep = new Meep();
DEFER(delete meep);
Fuhrpark_1 f1;
Fuhrpark_2 f2;
Fahrzeug ts("Tesla", "Model S", "M-1234", 500, 80);
f1.Add(ts);
f2.Add(ts);
Fahrzeug pt("Porsche", "Taycan", "M-5678", 400, 90);
pt.Print();
f1.Add(pt);
f2.Add(pt);
f1.Print();
f2.Print();
DEFER(std::cout << "Goodbye, " << meep->foo << std::endl);
std::cout << "Hello, " << meep->foo << std::endl;
return 0;
}