0Dima_0

Зукыщт / Згзшд сдфыыуы 3

Oct 17th, 2021 (edited)
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. class Person{
  7.   public:
  8.     string firstName, secondName, thirdName;
  9.  
  10.     void print(){
  11.       cout << "Person: { First Name: " << firstName << ", Second Name: " << secondName << ", Third Name: " << thirdName << "}";
  12.     }
  13.  
  14.     string getString(){
  15.       return "First Name: " + firstName + ", Second Name: " + secondName + ", Third Name: " + thirdName;
  16.     }
  17.  
  18.     Person(string firstName, string secondName, string thirdName){
  19.       this->firstName = firstName;
  20.       this->secondName = secondName;
  21.       this->thirdName = thirdName;
  22.     }
  23. };
  24.  
  25. class Teacher : public Person{
  26.   public:
  27.     string category;
  28.     string *classes;
  29.  
  30.     Teacher(string firstName, string secondName, string thirdName, string category, string classes[], int classesLength) : Person(firstName, secondName, thirdName){
  31.       this->category = category;
  32.       this->classes = new string[classesLength];
  33.       for(int i = 0; i < classesLength; i++) this->classes[i] = classes[i];
  34.     }
  35.  
  36.     void next(){
  37.       int l = sizeof(classes) / sizeof(classes[0]);
  38.       string *newClasses = new string[l]; //array of classes length
  39.       int index = 0;
  40.       for(int i = 0; i < l; i++){
  41.         string element = classes[i];
  42.         int c = stoi(element.substr(0, element.length() - 1)) + 1;
  43.         if(c <= 11){
  44.           newClasses[index] = to_string(c) + element.substr(0, 1);
  45.         }
  46.       }
  47.       classes = newClasses;
  48.     }
  49.  
  50.     void print(){
  51.       cout << "Teacher: {" << getString() << ", Category: " << category << ", Classes: ";
  52.       for(int i = 0; i < sizeof(classes) / sizeof(classes[0]); i++) cout << classes[i];
  53.       cout << "}";
  54.     }
  55. };
  56.  
  57. class Pupil : public Person{
  58.   public:
  59.     string grade, project;
  60.  
  61.     Pupil(string firstName, string secondName, string thirdName, string grade, string project) : Person(firstName, secondName, thirdName){
  62.       this->grade = grade;
  63.       this->project = project;
  64.     }
  65.  
  66.     void print(){
  67.       cout << "Pupil: {" << getString() << ", Class: " << grade << ", Project: " << project;
  68.     }
  69. };
  70.  
  71. void info(Person people[], int aomountToPint){
  72.   for(int i = 0; i < aomountToPint; i++) people[i].print();
  73. }
  74.  
  75. int main() {
  76.   string classes[] = { "10a", "11b", "1c" };
  77.   Teacher* teacher = new Teacher("A", "B", "C", "Biology", classes, sizeof(classes) / sizeof(classes[0]));
  78.   cout << "Teacher before next()" << endl;
  79.   teacher->print();
  80.   cout << endl << "Teacher after next()" << endl;
  81.   teacher->next();
  82.   teacher->print();
  83.  
  84.   Person* people[] = {new Pupil("A", "B", "C", "class", "project"), teacher};
  85.   info(*people, 2);
  86. }
Add Comment
Please, Sign In to add comment