Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- using namespace std;
- class DisciplinaryAction {
- private:
- char* name;
- int index;
- char* reason;
- public:
- DisciplinaryAction() {
- name = new char[100];
- reason = new char[100];
- index = 0;
- }
- DisciplinaryAction(char n[], int i, char r[]) {
- name = new char[100];
- strcpy(name, n); // copy the name
- index = i;
- reason = new char[100];
- strcpy(reason, r); // copy the reason
- }
- DisciplinaryAction(const DisciplinaryAction& d) {
- name = new char[100];
- reason = new char[100];
- strcpy(name, d.getName());
- index = d.getIndex();
- strcpy(reason, d.getReason());
- }
- ~DisciplinaryAction() {
- delete name;
- delete reason;
- }
- void print() {
- cout << "Student: " << name << endl;
- cout << "Student's index: " << index << endl;
- cout << "Reason: " << reason << endl;
- }
- void setIndex(int i) {
- index = i;
- }
- int getIndex() const {
- return index;
- }
- char* getName() const {
- return name;
- }
- char* getReason() const {
- return reason;
- }
- void operator=(const DisciplinaryAction& d) {
- strcpy(name, d.getName());
- index = d.getIndex();
- strcpy(reason, d.getReason());
- }
- };
- int main() {
- int n;
- cin >> n;
- /// Testing Default constructor and equal operator
- /// Array input
- DisciplinaryAction arr[n];
- for(int i = 0; i < n; i++) {
- char name[100];
- char reason[100];
- int index;
- cin >> name >> index >> reason;
- arr[i] = DisciplinaryAction(name, index, reason);
- }
- cout << "-- Testing operator = & print() --\n";
- arr[0].print();
- /// Testing copy constructor & set index
- DisciplinaryAction merka(arr[0]);
- merka.setIndex(112233);
- cout << "\n-- Testing copy constructor & set index --\n";
- cout << "-------------\n";
- cout << "Source:\n";
- cout << "-------------\n";
- arr[0].print();
- cout << "\n-------------\n";
- cout << "Copied and edited:\n";
- cout << "-------------\n";
- merka.print();
- /// Testing if array is OK
- cout << "\n-- Testing if array was inputted correctly --\n";
- for(int i = 0; i < n; i++)
- arr[i].print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment