Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cstdlib>
- #include <ctime>
- class Quadrat {
- private:
- int Augenwert;
- public:
- Quadrat(int augenwert) { Augenwert = augenwert; }
- };
- class Wuerfel {
- private:
- std::vector<Quadrat> Q;
- public:
- Wuerfel();
- ~Wuerfel();
- int wuerfeln();
- int dreimalwuerfeln();
- };
- Wuerfel::Wuerfel()
- {
- srand(time(NULL)); //initialisieren des Zufallsgenerators
- Quadrat *Wuerfelflaeche = NULL;
- for(int i=1; i <= 6; i++) {
- Wuerfelflaeche = new Quadrat(i); //Speicher für Quadrat allozieren und Zeiger Wuerfelfläche zuweisen
- if (Wuerfelflaeche == NULL) { //Prüfen ob Speicher bekommen
- std::cerr << "Error! Kein Speicher bekommen!\n";
- exit(EXIT_FAILURE);
- }
- Q.push_back(*Wuerfelflaeche); //Wuerfelfläche an vector hängen
- }
- }
- Wuerfel::~Wuerfel()
- {
- Q.clear();
- }
- int Wuerfel::wuerfeln()
- {
- return (rand() % 6) + 1; //Anzahl der Werte ist 6 (rand() % 6 = Werte von 0-5) deshalb noch + 1)
- }
- int Wuerfel::dreimalwuerfeln()
- {
- int augenzahl = 0;
- for (int i = 0; i < 3; i++) {
- augenzahl += wuerfeln();
- }
- return augenzahl;
- }
- int main()
- {
- Wuerfel W;
- std::cout << W.wuerfeln() << std::endl;
- std::cout << W.dreimalwuerfeln() << std::endl;
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement