mitkonikov

DisciplinaryAction class

Apr 3rd, 2023
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4.  
  5. class DisciplinaryAction {
  6. private:
  7.     char* name;
  8.     int index;
  9.     char* reason;
  10.  
  11. public:
  12.     DisciplinaryAction() {
  13.         name = new char[100];
  14.         reason = new char[100];
  15.         index = 0;
  16.     }
  17.  
  18.     DisciplinaryAction(char n[], int i, char r[]) {
  19.         name = new char[100];
  20.         strcpy(name, n); // copy the name
  21.         index = i;
  22.         reason = new char[100];
  23.         strcpy(reason, r); // copy the reason
  24.     }
  25.  
  26.     DisciplinaryAction(const DisciplinaryAction& d) {
  27.         name = new char[100];
  28.         reason = new char[100];
  29.         strcpy(name, d.getName());
  30.         index = d.getIndex();
  31.         strcpy(reason, d.getReason());
  32.     }
  33.    
  34.     ~DisciplinaryAction() {
  35.         delete name;
  36.         delete reason;
  37.     }
  38.  
  39.     void print() {
  40.         cout << "Student: " << name << endl;
  41.         cout << "Student's index: " << index << endl;
  42.         cout << "Reason: " << reason << endl;
  43.     }
  44.  
  45.     void setIndex(int i) {
  46.         index = i;
  47.     }
  48.     int getIndex() const {
  49.         return index;
  50.     }
  51.     char* getName() const {
  52.         return name;
  53.     }
  54.     char* getReason() const {
  55.         return reason;
  56.     }
  57.  
  58.     void operator=(const DisciplinaryAction& d) {
  59.         strcpy(name, d.getName());
  60.         index = d.getIndex();
  61.         strcpy(reason, d.getReason());
  62.     }
  63. };
  64.  
  65. int main() {
  66.     int n;
  67.     cin >> n;
  68.  
  69.     /// Testing Default constructor and equal operator
  70.     /// Array input
  71.  
  72.     DisciplinaryAction arr[n];
  73.  
  74.     for(int i = 0; i < n; i++) {
  75.         char name[100];
  76.         char reason[100];
  77.         int index;
  78.  
  79.         cin >> name >> index >> reason;
  80.  
  81.         arr[i] = DisciplinaryAction(name, index, reason);
  82.     }
  83.  
  84.     cout << "-- Testing operator = & print() --\n";
  85.     arr[0].print();
  86.  
  87.     /// Testing copy constructor & set index
  88.     DisciplinaryAction merka(arr[0]);
  89.     merka.setIndex(112233);
  90.  
  91.     cout << "\n-- Testing copy constructor & set index --\n";
  92.     cout << "-------------\n";
  93.     cout << "Source:\n";
  94.     cout << "-------------\n";
  95.     arr[0].print();
  96.  
  97.     cout << "\n-------------\n";
  98.     cout << "Copied and edited:\n";
  99.     cout << "-------------\n";
  100.     merka.print();
  101.  
  102.     /// Testing if array is OK
  103.  
  104.     cout << "\n-- Testing if array was inputted correctly --\n";
  105.  
  106.     for(int i = 0; i < n; i++)
  107.         arr[i].print();
  108.  
  109.     return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment