Advertisement
myloyo

наследование

Jun 6th, 2023 (edited)
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. class Edition
  8. {
  9. protected:
  10.     string name, fam;
  11. public:
  12.     string getName() {
  13.         return name;
  14.     }
  15.     string getFam() {
  16.         return fam;
  17.     }
  18.     void setName(string name) {
  19.         this->name = name;
  20.     }
  21.     void setFam(string fam) {
  22.         this->fam = fam;
  23.     }
  24.     virtual void info() {
  25.         cout <<"Name:" << name << "Author:" << " " << fam;
  26.     }
  27.  
  28. };
  29.  
  30. class Book : public Edition
  31. {
  32. private:
  33.     string izdat;
  34.     int year;
  35. public:
  36.     Book(string name, string fam, string izdat, int year) {
  37.         setName(name);
  38.         setFam(fam);
  39.         setIzdat(izdat);
  40.         setYear(year);
  41.     }
  42.     void setIzdat(string izdat) {
  43.         this->izdat = izdat;
  44.     }
  45.     void setYear(int year) {
  46.         this->year = year;
  47.     }
  48.  
  49.     void info() {
  50.         cout << "Name:" << getName() << " " << "Author:" << getFam() << " " << "Publishing:" << izdat << " " << "Year of publishing:" << year << endl;
  51.     }
  52. };
  53.  
  54. class Article : public Edition
  55. {
  56. private:
  57.     string magazine;
  58.     int num, year;
  59. public:
  60.     Article(string name, string fam, string magazine, int num, int year) {
  61.         setName(name);
  62.         setFam(fam);
  63.         setmagazine(magazine);
  64.         setnum(num);
  65.         setyear(year);
  66.     }
  67.     void setmagazine(string magazine) {
  68.         this->magazine = magazine;
  69.     }
  70.     void setnum(int num) {
  71.         this->num = num;
  72.     }
  73.     void setyear(int year) {
  74.         this->year = year;
  75.     }
  76.  
  77.     void info() {
  78.         cout << "Name:" << getName() << " " << "Author:" << getFam() << " " << "Magazine:" << magazine << " " << "Numeric of Magazine:" << num << " " << "Year of publishing:" << year << endl;
  79.     }
  80.  
  81. };
  82.  
  83. class OnlineResource : public Edition
  84. {
  85. private:
  86.     string link, annotation;
  87. public:
  88.     OnlineResource(string name, string fam, string link, string annotation) {
  89.         setName(name);
  90.         setFam(fam);
  91.         setlink(link);
  92.         setannotation(annotation);
  93.     }
  94.  
  95.     void setlink(string link) {
  96.         this->link = link;
  97.     }
  98.  
  99.     void setannotation(string annotation) {
  100.         this->annotation = annotation;
  101.     }
  102.  
  103.     void info() {
  104.         cout << "Name:" << getName() << " " << "Author:" << getFam() << " " << "Link:" << link << " " << "Annotation:" << annotation << endl;
  105.     }
  106.  
  107. };
  108.  
  109. void find_fam(Edition** a, string fam, int n) {
  110.     for (int i = 0; i < n; i++) {
  111.         if (a[i]->getFam() == fam) {
  112.             a[i]->info();
  113.             cout << endl;
  114.         }
  115.     }
  116. }
  117.  
  118. int main() {
  119.     int n = 12;
  120.     Edition** base = new Edition * [n];
  121.     base[0] = new Book("Harry Potter and the Sorcerer's Stone", "Rowling", "Scholastic", 2012);
  122.     base[1] = new Book("Hunger Games", "Collins", "Mavink", 2013);
  123.     base[2] = new Book("Play to live: War", "DmitriRus", "eksmo", 2019);
  124.     base[3] = new Book("The Witcher: the last wish", "Sapkowski", "mavink", 2020);
  125.     base[4] = new Article("Harry Potter and the Half-Blood Prince", "Rowling", "Good Reading", 32, 2014);
  126.     base[5] = new Article("Hunger Games: Cathing Fire", "Collins", "Good Reading", 14, 2012);
  127.     base[6] = new Article("Play to live: breakdown", "DmitriRus", "Good Reading", 55, 2017);
  128.     base[7] = new Article("The Witcher: sword of destiny", "Sapkowski", "Good Reading", 23, 2015);
  129.     base[8] = new OnlineResource("Harry Potter and the Chamber of Secrets", "Rowling", "www.potter.com", "fantastic");
  130.     base[9] = new OnlineResource("Hunger Games: Mockingjay", "Collins", "www.hungergames.com", "thriller");
  131.     base[10] = new OnlineResource("Play to live: clan", "DmitriRus", "www.playtolive.com", "litrpg");
  132.     base[11] = new OnlineResource("The Witcher: baptism of fire", "Sapkowski", "www.thewitcher.com", "fantastic");
  133.  
  134.     for (int i = 0; i < n; i++) {
  135.         base[i]->info();
  136.         cout << endl;
  137.     }
  138.     string fami;
  139.     cin >> fami;
  140.     find_fam(base, fami, n);
  141.     return 0;
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement