Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- using namespace std;
- class Book
- {
- char name[20];
- char author[20];
- size_t year;
- Book *ad = nullptr;
- public:
- Book(){}
- Book(const char* name, const char* author, size_t year)
- {
- strcpy_s(this->name, name);
- strcpy_s(this->author, author);
- this->year = year;
- }
- void setName(const char* name) { strcpy_s(this->name, name); }
- void setAuthor(const char* author) { strcpy_s(this->author, author); }
- void setYear(size_t year) { this->year = year; }
- void addAd(Book *book) { this->ad = book; }
- char *getName() { return name; }
- char *getAuthor() { return author; }
- size_t getYear() { return year; }
- void adPrn()
- {
- if (ad != nullptr)
- {
- cout << "Advertise:\n";
- ad->bookprn();
- }
- }
- void bookprn()
- {
- printf("Author: %s\nName: %s\nYear: %d\n", author, name, year);
- }
- };
- int main()
- {
- Book a("History", "Ivan", 1992);
- Book b("Geometry", "Pogorelov", 1988);
- a.bookprn();
- b.bookprn();
- /*a.setName("Math");
- a.setAuthor("Sergey");
- a.setYear(1991);
- a.bookprn(a);*/
- srand(time(NULL));
- 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";
- Book adv[20];
- adv[0] = a;
- adv[1] = b;
- for (int i = 2; i < 20; ++i)
- {
- size_t pos = rand() % (base.size() - 15);
- adv[i].setAuthor(base.substr(pos, rand()%15+1).c_str());
- pos = rand() % (base.size() - 20);
- adv[i].setName(base.substr(pos, 19).c_str());
- adv[i].setYear(rand() % 200 + 1819);
- adv[i].bookprn();
- }
- for (int i = 0; i < 20; ++i)
- {
- char adIndex;
- do
- adIndex = rand() % 20;
- while (adIndex == i);
- adv[i].addAd(&adv[adIndex]);
- }
- cout << "===============================================\n";
- adv[0].bookprn();
- adv[0].adPrn();
- adv[1].bookprn();
- adv[1].adPrn();
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement