-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStudent_Task1.cpp
140 lines (118 loc) · 4.17 KB
/
Student_Task1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "Student_Task1.h" // class implemented
#include<cstring> // prototypes for strcmp
#include<string.h>
using namespace std;
// File scope starts here
/////////////////////////////// PUBLIC ///////////////////////////////////////
//============================= LIFECYCLE ====================================
// Student Default+Overloaded Constructor
Student::Student(char* aName, int aAge, char aGender, char* aDesignation, char* aProgram, int aStudyYear) : Person(aName, aAge, aGender, aDesignation), mStudyYear(aStudyYear) {
this->SetStudent(aStudyYear, aProgram);
this->SetDesignation(aDesignation);
}
// end Student constructor
// Student Copy Constructor
Student::Student(const Student& rhs) : Person(rhs.GetPerson()), mStudyYear(rhs.GetStudyYear()) {
this->SetStudent(rhs.GetStudyYear(), rhs.GetProgram());
this->SetDesignation(rhs.mDesignation);
}
// end Student constructor
// Student Destructor
Student::~Student() {
if (this->mProgram)
delete[] this->mProgram;
}
// end Student Destructor
// Student assignment operator.
Student& Student:: operator =(const Student& rhs) {
this->SetStudent(rhs.GetStudent());
this->SetDesignation(rhs.mDesignation);
return *this;
}
// end Student assignment operator.
//============================= OPERATIONS ===================================
// function that depicts study of Student.
void Student::Study() {
cout << this->GetName() << " is studying." << endl;
}
// end function Study
// function that depicts exams of Student.
void Student::HeldExam() {
cout << this->GetName() << " is taking exam." << endl;
}
// end function HeldExam
//============================= ACESS ===================================
// function that sets program of Student
void Student::SetProgram(char* aProgram) {
// freeing the previously alloted memory
if (this->mProgram)
delete[] this->mProgram;
// reserving new memory space for updated program
if (aProgram != NULL) {
int len = strlen(aProgram) + 1;
this->mProgram = new char[len];
strcpy_s(this->mProgram, len, aProgram);
}
else {
this->mProgram = new char;
strcpy_s(this->mProgram, 1, "\0");
}
}
// end function SetProgram
// function that sets study year of Student
void Student::SetStudyYear(int aStudyYear) {
if (aStudyYear > 1980 && aStudyYear<2025)
this->mStudyYear = aStudyYear;
else {
this->mStudyYear = 0;
cout << "Error: Study year out of range." << endl;
}
}
// end function SetStudyYear
// Pure virtual function that sets designation in (Parent)Person class
void Student::SetDesignation(char* aDesignation) {
if (aDesignation == NULL);
else if (!(strcmp(aDesignation, "student") == 0 || strcmp(aDesignation, "Student") == 0))
cout << "Invalid designation entered. Setting it to Student." << endl;
// freeing the previously alloted memory
if (this->mDesignation)
delete[] this->mDesignation;
// reserving new memory space for updated designation
int len = strlen("Student") + 1;
this->mDesignation = new char[len];
strcpy_s(this->mDesignation, len, "Student");
}
// end function SetDesignation
// function that sets Student
void Student::SetStudent(char* aName, int aAge, char aGender, char* aProgram, int aStudyYear) {
this->SetPerson(aName, aAge, aGender);
this->SetStudent(aStudyYear, aProgram);
}
// end function SetStudent
// overloaded function that sets the Student
void Student::SetStudent(int aStudyYear, char* aProgram) {
this->SetProgram(aProgram);
this->SetStudyYear(aStudyYear);
}
// end function SetStudent
// overloaded function that sets the Student
void Student::SetStudent(const Student& aStudent) {
this->SetPerson(aStudent.GetPerson());
this->SetStudent(aStudent.GetStudyYear(), aStudent.GetProgram());
}
// end function SetStudent
// function that gets program of Student
char* Student::GetProgram()const {
return this->mProgram;
}
// end function GetProgram
// function that gets stufy year of Student
int Student::GetStudyYear()const {
return this->mStudyYear;
}
// end function GetStudyYear
// function that gets the Student
const Student& Student::GetStudent()const {
return *this;
}
// end function GetStudent