SHARE
TWEET

Untitled

a guest Sep 20th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. using namespace std;
  5.  
  6. class Book
  7. {
  8.     char name[20];
  9.     char author[20];
  10.     size_t year;
  11.     Book *ad = nullptr;
  12.  
  13. public:
  14.     Book(){}
  15.    
  16.     Book(const char* name, const char* author, size_t year)
  17.     {
  18.         strcpy_s(this->name, name);
  19.         strcpy_s(this->author, author);
  20.         this->year = year;
  21.     }
  22.  
  23.     void setName(const char* name) { strcpy_s(this->name, name); }
  24.     void setAuthor(const char* author) { strcpy_s(this->author, author); }
  25.     void setYear(size_t year) { this->year = year; }
  26.     void addAd(Book *book) { this->ad = book; }
  27.  
  28.     char *getName() { return name; }
  29.     char *getAuthor() { return author; }
  30.     size_t getYear() { return year; }
  31.  
  32.     void adPrn()
  33.     {
  34.         if (ad != nullptr)
  35.         {
  36.             cout << "Advertise:\n";
  37.             ad->bookprn();
  38.         }
  39.     }
  40.  
  41.     void bookprn()
  42.     {
  43.         printf("Author: %s\nName: %s\nYear: %d\n", author, name, year);
  44.     }
  45. };
  46.  
  47. int main()
  48. {
  49.     Book a("History", "Ivan", 1992);
  50.     Book b("Geometry", "Pogorelov", 1988);
  51.  
  52.     a.bookprn();
  53.     b.bookprn();
  54.     /*a.setName("Math");
  55.     a.setAuthor("Sergey");
  56.     a.setYear(1991);
  57.     a.bookprn(a);*/
  58.  
  59.  
  60.     srand(time(NULL));
  61.  
  62.     string base = "I decided to make a random string generator for fun, and it works for the most part.Since I'm an amateur at programming, I thought I would post the code to get some feedback on it, and hopefully improve any bad habits";
  63.     Book adv[20];
  64.     adv[0] = a;
  65.     adv[1] = b;
  66.     for (int i = 2; i < 20; ++i)
  67.     {
  68.         size_t pos = rand() % (base.size() - 15);
  69.        
  70.         adv[i].setAuthor(base.substr(pos, rand()%15+1).c_str());
  71.        
  72.         pos = rand() % (base.size() - 20);
  73.         adv[i].setName(base.substr(pos, 19).c_str());
  74.         adv[i].setYear(rand() % 200 + 1819);
  75.         adv[i].bookprn();
  76.     }
  77.     for (int i = 0; i < 20; ++i)
  78.     {
  79.         char adIndex;
  80.         do
  81.             adIndex = rand() % 20;
  82.         while (adIndex == i);
  83.         adv[i].addAd(&adv[adIndex]);
  84.     }
  85.     cout << "===============================================\n";
  86.     adv[0].bookprn();
  87.     adv[0].adPrn();
  88.     adv[1].bookprn();
  89.     adv[1].adPrn();
  90.  
  91.     system("pause");
  92. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top