Advertisement
Rapptz

Untitled

Sep 2nd, 2012
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <array>
  4. #include <algorithm>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <cmath>
  8.  
  9. class Bunny {
  10. private:
  11.     bool isMale;
  12.     std::string colour;
  13.     int age;
  14.     std::string name;
  15.     bool isRadioactive;
  16.     bool dead;
  17. public:
  18.     Bunny(): isMale(0), colour("White"), age(0), name("Place"), isRadioactive(0), dead(0) {}
  19.     void setSex() {
  20.         srand((unsigned)time(NULL));
  21.         isMale = round((double)rand()/(double)RAND_MAX);
  22.     }
  23.     void setColour() {
  24.         srand((unsigned)time(NULL));
  25.         std::array<std::string,4> colours = { "White","Brown","Black", "Spotted" };
  26.         int selector = rand()%3;
  27.         colour = colours[selector];
  28.     }
  29.     void setAge() {
  30.         if(isRadioactive) {
  31.             if(age < 50)
  32.                 age++;
  33.             else
  34.                 dead = 1;
  35.         }
  36.         else {
  37.             if(age < 10)
  38.                 age++;
  39.             else
  40.                 dead = 1;
  41.         }
  42.     }
  43.     void setRadioactive() {
  44.         srand((unsigned)time(NULL));
  45.         int chance = (rand()%100)+1;
  46.         if(chance == 2)
  47.             isRadioactive = true;
  48.     }
  49.     void setName() {
  50.         std::vector<std::string> names = {"Thumper","Oreo","Daisy","Bunny",\
  51.         "Bella", "Lily","Charlie","Lola","Molly","Oliver","Jack","Peanut","Coco",\
  52.         "Lucy","Baily","Clover","Smokey","Honey","Fluffy","Ginger","Willow",\
  53.         "Chloe","Bunbun","Gizmo","Snowball"};
  54.         int selection = rand()%names.size();
  55.         name = names[selection];
  56.     }
  57.     bool isDead() {
  58.         return dead;
  59.     }
  60.     friend std::ostream& operator<<(std::ostream& out, Bunny& b);
  61. };
  62. std::ostream& operator<<(std::ostream& out, Bunny& b) {
  63.     out << "Name: " << b.name << "\n";
  64.     if(b.isMale)
  65.         out << "Sex: Male\n";
  66.     else
  67.         out << "Sex: Female\n";
  68.     out << "Colour: " << b.colour << "\n";
  69.     out << "Age: " << b.age << " years old\n";
  70.     out << "Radioactive Bunny: " << b.isRadioactive << "\n";
  71.     out << "Is Dead: " << b.dead << "\n";
  72.     return out;
  73. }
  74.  
  75. int main() {
  76.     srand((unsigned)time(NULL));
  77.  
  78.     std::vector<Bunny> bunnies;
  79.     for(int i = 0; i < 5; i++) {
  80.         bunnies.push_back(Bunny());
  81.         bunnies[i].setSex();
  82.         bunnies[i].setColour();
  83.         bunnies[i].setAge();
  84.         bunnies[i].setRadioactive();
  85.         bunnies[i].setName();
  86.     }
  87.     for(auto i : bunnies)
  88.         std::cout << i << std::endl;
  89.  
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement