Kapitel 7
Alle Codedateien dieses Kapitels herunterladen
07_1_HotelPrivate.cpp
#include <iostream>
#include <vector>
#include <string>
std::vector<std::string> split(const std::string& str)
{
std::vector<std::string> parts;
size_t start = 0;
size_t end = str.find(',');
while (end != std::string::npos)
{
parts.push_back(str.substr(start, end - start));
start = end + 1;
end = str.find(',', start);
}
parts.push_back(str.substr(start, end));
return parts;
}
class Person
{
public:
Person(const std::string& f, const std::string& l, int a)
: firstName(f), lastName(l), age(a)
{
}
Person(const std::string& input)
{
std::vector<std::string> parts = split(input);
if (parts.size() == 3)
{
lastName = parts.at(0);
firstName = parts.at(1);
age = std::stoi(parts.at(2));
}
}
Person()
{
}
std::string toString()
{
return lastName + ", " + firstName
+ ". Alter: " + std::to_string(age);
}
std::string firstName;
std::string lastName;
int age = -1;
};
class Room
{
public:
Room(const std::string& n, int c, int p)
: name(n), capacity(c), pricePerPerson(p)
{
}
bool book(const Person& person, int maxPrice)
{
if (pricePerPerson <= maxPrice // Preis okay
&& persons.size() < capacity) // Frei
{
persons.push_back(person);
return true;
}
// Dieser Raum eignet sich nicht
return false;
}
std::vector<Person> getPersons()
{
return persons;
}
const std::string name;
const int capacity;
const int pricePerPerson;
private:
std::vector<Person> persons;
};
class Hotel
{
public:
Hotel(const std::string& n, const std::vector<Room>& r)
: name(n), rooms(r)
{
}
bool book(const Person& person, int maxPrice)
{
// Einen freien Raum finden, der dem Preis genügt
// Wichtig: Per Referenz auf den Raum zugreifen!
for (Room& room : rooms)
{
if (room.book(person, maxPrice))
{
return true;
}
}
// Keinen Raum gefunden
return false;
}
std::string toString()
{
std::string output = "Belegung Hotel " + name + ":\n"
+ "=============================\n";
for (Room room : rooms)
{
output += "Raum " + room.name + ":\n";
for (Person person : room.getPersons())
{
output += person.toString() + "\n";
}
if (room.getPersons().empty())
{
output += " Leer\n";
}
output += "-----------------------------\n";
}
return output;
}
const std::string name;
private:
std::vector<Room> rooms;
};
int main()
{
const std::vector<Room> rooms = {
Room("101", 2, 40),
Room("102", 2, 50),
Room("103", 6, 20),
Room("104", 1, 70)
};
Hotel hotel("California", rooms);
// Interaktive Buchung
std::cout << "Buchungssystem" << std::endl;
while (true)
{
std::cout << "Nachname,Vorname,Alter:" << std::endl;
// String über die Konsole entgegennehmen
std::string input;
std::getline(std::cin, input);
// String an den Konstruktor übergeben
Person person(input);
if (person.age < 0)
{
// Hieran erkennen wir eine "ungültige" Person
std::cerr << "Eingabefehler!" << std::endl;
}
else
{
std::cout << "Maximalpreis: ";
std::getline(std::cin, input);
int maxPrice = std::stoi(input);
if (hotel.book(person, maxPrice))
{
std::cout << "Zimmer gefunden!" << std::endl;
std::cout << hotel.toString();
}
else
{
std::cout << "Kein Zimmer gefunden!" << std::endl;
}
}
}
return 0;
}
07_2_1_Tiere.cpp
#include <string>
#include <iostream>
class Animal
{
public:
Animal(const std::string& name)
: name(name)
{}
void eat()
{
std::cout << name << " isst" << std::endl;
}
std::string name;
};
class Cat : public Animal
{
public:
Cat(const std::string& name)
: Animal(name)
{
}
void purr()
{
std::cout << name << " schnurrt" << std::endl;
}
bool eatsOnlyCannedFood = true; // Isst nur Nassfutter
};
int main()
{
Cat mia("Mia");
mia.eat();
mia.purr();
Cat carlo("Carlo");
carlo.eatsOnlyCannedFood = false;
return 0;
}
07_2_2_KatzeUndMaus.cpp
#include <string>
#include <vector>
#include <iostream>
class Animal
{
public:
Animal(const std::string& name)
: name(name)
{}
void eat()
{
std::cout << name << " isst" << std::endl;
}
std::string name;
};
class Cat : public Animal
{
public:
Cat(const std::string& name)
: Animal(name)
{
}
void purr()
{
std::cout << name << " schnurrt" << std::endl;
}
bool eatsOnlyCannedFood = false; // Isst nur Nassfutter
};
class Mouse : public Animal
{
public:
Mouse(const std::string& name)
: Animal(name)
{
}
};
void feed(const std::vector<Animal>& animals)
{
for (Animal animal : animals)
{
animal.eat();
// Hier ist es nur möglich, die Methoden der Klasse
// Animal zu verwenden. Nicht aber zum Beispiel purr()
}
}
int main()
{
Cat mia("Mia");
Cat carlo("Carlo");
Mouse philipp("Philipp");
std::vector<Animal> animals = { mia, carlo, philipp };
feed(animals);
return 0;
}
07_2_3_KatzeUndMaus_Protected.cpp
#include <string>
#include <vector>
#include <iostream>
class Animal
{
public:
Animal(const std::string& name)
: name(name)
{}
void eat()
{
std::cout << name << " isst" << std::endl;
}
std::string getName()
{
return name;
}
protected:
std::string name;
};
class Cat : public Animal
{
public:
Cat(const std::string& name)
: Animal(name)
{
}
void purr()
{
std::cout << name << " schnurrt" << std::endl;
}
bool eatsOnlyCannedFood = false; // Isst nur Nassfutter
};
class Mouse : public Animal
{
public:
Mouse(const std::string& name)
: Animal(name)
{
}
};
void feed(const std::vector<Animal>& animals)
{
for (Animal animal : animals)
{
animal.eat();
// Hier ist es nur möglich, die Methoden der Klasse
// Animal zu verwenden. Nicht aber zum Beispiel purr()
}
}
int main()
{
Cat mia("Mia");
Cat carlo("Carlo");
Mouse philipp("Philipp");
std::vector<Animal> animals = { mia, carlo, philipp };
feed(animals);
return 0;
}
07_3_1_VirtuelleMethoden.cpp
#include <iostream>
class Animal
{
public:
virtual void eat()
{
std::cout << "Tier isst" << std::endl;
}
};
class Cat : public Animal
{
public:
void eat() override
{
std::cout << "Katze isst eine Maus" << std::endl;
}
};
class Mouse : public Animal
{
public:
void eat() override
{
std::cout << "Maus isst K\204se" << std::endl;
}
};
class Worm : public Animal
{
};
int main()
{
// Die Standardimplementierung der Oberklasse
Animal tier;
tier.eat();
// Jeweils eine eigene Implementierung
Cat cat;
cat.eat();
Mouse mouse;
mouse.eat();
// Nochmals die Standardimplementierung der Oberklasse
Worm worm;
worm.eat();
return 0;
}
07_3_2_Fuetterung.cpp
#include <iostream>
class Animal
{
public:
virtual void eat()
{
std::cout << "Tier isst" << std::endl;
}
};
class Cat : public Animal
{
public:
void eat() override
{
std::cout << "Katze isst eine Maus" << std::endl;
}
};
class Mouse : public Animal
{
public:
void eat() override
{
std::cout << "Maus isst K\204se" << std::endl;
}
};
class Worm : public Animal
{
};
void feed(Animal& animal)
{
animal.eat();
}
void feedNoReference(Animal animal)
{
animal.eat();
}
int main()
{
Cat cat;
Mouse mouse;
feed(cat);
feed(mouse);
std::cout << "---------------" << std::endl;
feedNoReference(cat);
feedNoReference(mouse);
return 0;
}
07_3_3_VirtuelleMethodenImVektor.cpp
#include <iostream>
#include <vector>
class Animal
{
public:
virtual void eat()
{
std::cout << "Tier isst" << std::endl;
}
};
class Cat : public Animal
{
public:
void eat() override
{
std::cout << "Katze isst eine Maus" << std::endl;
}
};
class Mouse : public Animal
{
public:
void eat() override
{
std::cout << "Maus isst K\204se" << std::endl;
}
};
class Worm : public Animal
{
};
int main()
{
std::vector<Animal> zoo = { Animal(), Cat(), Mouse() };
for (Animal& animal : zoo)
{
animal.eat();
}
return 0;
}
07_4_ConstMethoden.cpp
#include <iostream>
#include <string>
class Animal
{
public:
virtual void eat() const
{
std::cout << name << " isst" << std::endl;
}
void rename(const std::string& newName)
{
name = newName;
}
protected:
std::string name;
};
void feed(const Animal& animal)
{
animal.eat();
}
int main()
{
Animal animal;
animal.rename("Leila");
feed(animal);
return 0;
}