Advertisement
0Dima_0

Person / Pupil classes

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