mitkonikov

DisciplinaryAction class 2

Apr 24th, 2023
2,520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.60 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.     int sessions;
  11.  
  12. public:
  13.     DisciplinaryAction() {
  14.         name = new char[100];
  15.         reason = new char[100];
  16.         index = 0;
  17.         sessions = 0;
  18.     }
  19.  
  20.     DisciplinaryAction(char n[], int i, char r[], int s) {
  21.         name = new char[100];
  22.         strcpy(name, n); // copy the name
  23.         index = i;
  24.         reason = new char[100];
  25.         strcpy(reason, r); // copy the reason
  26.         sessions = s;
  27.     }
  28.  
  29.     DisciplinaryAction(const DisciplinaryAction& d) {
  30.         name = new char[100];
  31.         reason = new char[100];
  32.         strcpy(name, d.getName());
  33.         index = d.getIndex();
  34.         strcpy(reason, d.getReason());
  35.         sessions = d.getSessions();
  36.     }
  37.    
  38.     ~DisciplinaryAction() {
  39.         delete name;
  40.         delete reason;
  41.     }
  42.  
  43.     void print() {
  44.         cout << "Student: " << name << endl;
  45.         cout << "Student's index: " << index << endl;
  46.         cout << "Reason: " << reason << endl;
  47.         cout << "Sessions: " << sessions << endl;
  48.     }
  49.  
  50.     void setIndex(int i) {
  51.         index = i;
  52.     }
  53.     int getIndex() const {
  54.         return index;
  55.     }
  56.     char* getName() const {
  57.         return name;
  58.     }
  59.     char* getReason() const {
  60.         return reason;
  61.     }
  62.     int getSessions() const {
  63.         return sessions;
  64.     }
  65.  
  66.     void operator=(const DisciplinaryAction& d) {
  67.         strcpy(name, d.getName());
  68.         index = d.getIndex();
  69.         strcpy(reason, d.getReason());
  70.         sessions = d.getSessions();
  71.     }
  72.    
  73.     bool operator>=(const DisciplinaryAction& d) {
  74.         return (sessions >= d.getSessions());
  75.     }
  76.  
  77.     friend ostream& operator<<(ostream &os, const DisciplinaryAction &d) {
  78.         os << "Student: " << d.name << endl;
  79.         os << "Student's index: " << d.index << endl;
  80.         os << "Reason: " << d.reason << endl;
  81.         os << "Sessions: " << d.sessions << endl;
  82.         return os;
  83.     }
  84.  
  85.     DisciplinaryAction& operator++() {
  86.         ++sessions;
  87.         return *this;
  88.     }
  89. };
  90.  
  91. int main() {
  92.     int n;
  93.     cin >> n;
  94.  
  95.     /// Testing Default constructor and equal operator
  96.     /// Array input
  97.  
  98.     DisciplinaryAction arr[n];
  99.  
  100.     for (int i = 0; i < n; i++) {
  101.         char name[100];
  102.         char reason[100];
  103.         int index;
  104.         int sessions;
  105.  
  106.         cin >> name >> index >> reason >> sessions;
  107.  
  108.         arr[i] = DisciplinaryAction(name, index, reason, sessions);
  109.     }
  110.  
  111.     cout << "-- Testing operator = & operator <<  --\n";
  112.     cout << arr[0];
  113.  
  114.     /// Testing copy constructor & set index
  115.  
  116.     DisciplinaryAction merka(arr[0]);
  117.     merka.setIndex(112233);
  118.  
  119.     cout << "\n-- Testing copy constructor & set index --\n";
  120.     cout << "-------------\n";
  121.     cout << "Source:\n";
  122.     cout << "-------------\n";
  123.     cout << arr[0];
  124.  
  125.     cout << "\n-------------\n";
  126.     cout << "Copied and edited:\n";
  127.     cout << "-------------\n";
  128.     cout << merka;
  129.  
  130.     /// Testing if array is OK
  131.  
  132.     cout << "\n-- Testing if array was inputted correctly --\n";
  133.  
  134.     for (int i = 0; i < n; i++)
  135.         cout << arr[i];
  136.  
  137.  
  138.     cout << "\nTesting operator ++ and <<" << endl;
  139.     for (int i = 0; i < n; i++)
  140.         cout << (++arr[i]);
  141.  
  142.  
  143.     cout << "\nTesting operator >=" << endl;
  144.     for (int i = 0; i < n; i++) {
  145.         for (int j = 0; j < n; j++) {
  146.             if (i != j) {
  147.                 cout << i << " " << ((arr[i] >= arr[j]) ? ">= " : "< ") << j << endl;
  148.             }
  149.         }
  150.     }
  151.  
  152.     return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment