Advertisement
Guest User

dav 44 N1

a guest
May 25th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.33 KB | None | 0 0
  1. //amocana1
  2. /*
  3. class Book  
  4. {
  5. protected:
  6.     string author,name;
  7.     int year;
  8.  
  9. public:
  10.    
  11.     Book (){}
  12.     Book (string author,int year,string name)  
  13.     {
  14.         this->author=author;
  15.         this->year=year;
  16.         this->name=name;
  17.     }
  18.     ~Book (){   }
  19.     void printBook ()
  20.     {
  21.         cout<<"Name: "<<name<<endl;
  22.         cout<<"Year: "<<year<<endl;
  23.         cout<<"Author: "<<author<<endl;
  24.     }
  25.     int get_year ()
  26.     {
  27.         return year;
  28.     }
  29.  
  30. };
  31.  
  32.  
  33. class Textbook : public Book
  34. {
  35.     protected:
  36.     int grade;
  37.  
  38. public:
  39.     Textbook (string author,int year,string name,int grade) : Book (author,year,name)
  40.     {
  41.         this->grade=grade;
  42.     }
  43.  
  44.     void printText ()
  45.     {
  46.         cout<<"Name: "<<name<<endl;
  47.         cout<<"Year: "<<year<<endl;
  48.         cout<<"Author: "<<author<<endl;
  49.         cout<<"Grade: "<<grade<<endl;
  50.     }
  51.  
  52.  
  53. };
  54.  
  55. bool sort_by_year (Book a, Book b)
  56. {
  57.     return a.get_year() > b.get_year();
  58. }
  59.  
  60. int main ()
  61. {
  62.     string name,author;
  63.     int grade,year;
  64.  
  65.     int n;
  66.     cin>>n;
  67.  
  68.     vector<Book>Books;
  69.  
  70.     for (int i=0;i<n;i++)
  71.     {
  72.         cin>>author>>year>>name>>grade;
  73.         Book b(author,year,name);
  74.         Textbook t (author,year,name,grade);
  75.         Book *b_ptr=&b;
  76.         Textbook *t_ptr=&t;
  77.         b_ptr=t_ptr;
  78.         t_ptr=(Textbook*)&b;
  79.         Books.push_back(*b_ptr);
  80.         Books.push_back(*t_ptr);
  81.     }
  82.  
  83. sort (Books.begin(),Books.end(),sort_by_year);
  84.  
  85.  
  86. for (int i = 0; i < Books.size(); ++i)
  87. {
  88.     Books.at(i).printBook();
  89. }
  90.  
  91. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement