The purpose of this assignment is to review basic fundamentals of OOP,
class definition, constructors, destructor, accessors, mutators, for
a simple Student class definition containing a dynamic member variable.
This assignment will review on how to modify an existing class definition,
to handle memory managment by defining the default constructor, copy
constructor, destructor, and overloaded assingment operator.
Requirements
You will be given function main to leave as is. Your task is to
write the Student class definition w/ constructor, destructor, overloaded
assignment operator, and member function prototypes, and then
define the all the member functions listed below outside of the class
definition (review Scope::Resolution Operator).
Private Helper Functions
The Student class will have 4 member variables total: a static cstring for
the student's first name, a static cstring for the student's last name, an
integer pointer for tests, and an integer for the count of tests
int* allocate(const int) - this function will pass an integer for the size
of the integer array to be allocated, returning the address of the newly
allocated array. Validate that the passed parameter is positive and allocate
accordingly, otherwise return nullptr. This will be a private helper function
void deallocateTests() - this function will deallocate all memory allocated
to tests, sets tests to nullptr, and then count to zero.
Constructors
Student() - this constructor will assign empty strings to the first and
last name, nullptr to the tests pointer, and zero to count.
Student(const Student&) - this copy constructor will pass
a const Student Object by reference. This constructor will copy the first and
last name of the passed object into the newly instatiated object, copy the value of count, and call the function allocate passing the count. Traverse both
tests arrays and copy the passed object's tests array into the newly
instantiated object.
Overloaded Assignment Operator
Student& operator=(const Student&) - this overloaded operator
will copy the contents of the passed object, however since our Student
Class Definition has a dynamic member variable, we have to write our own
assignment operator definition and not use the compiler's 'memberwise
assignment'. First, check to see if the address of the passed object is
equal to this object, if so return this object. Otherwise, call the
deallocateTests function, copy the first and last name from the passed object
into this object, copy the value of count into this object. If
the passed object's tests array is not nullptr, then call allocate passing the
passed object's count and assign the returned value into this's tests.
Then, traverse both arrays, and copy the passed objects test array values
into this's tests array. Otherwise, set tests to nullptr. Finally, return
this object;
Destructor
~Student - this destructor will call the deallocateTests function.
Optionally, you may zero out the first and last names.
Mutator Member Functions
void setFname(const char*) - this function will copy the passed cstring
into this object's first name.
void setLname(const char*) - this function will copy the passed cstring
into this object's last name.
void setTestAtIndex(const int, const int) - this function will pass two
constant integers by value, the first being the score, and the second being
at which index to set the score. First validate that the passed index is
not within the bounds of the tests array,
if so, print an error message and return.
Otherwise, validate that the tests array is allocated and that the
passed score is within 0..100, access the
tests array at index and assign the passed score. Otherwise, print an
error message and return.
Accessor Member Functions
const char* getFname() - this function will return the first name
const char* getLname() - this function will return the last name
const int getTestAtIndex(int) - this function will pass an integer by
value, being the index for the score value to return within the tests array.
Validate that the index is in bounds and tests array is not nullptr, then
return the tests score stored at index, otherwise, print an error message
and return -1.
const float getAverage() - this function will calculate and return the
average of the tests array if the tests array is not nullptr,
otherwise return 0.
void inputStudent() - this function will prompt the user for the
student's first and last name, a count of how many test scores to allocate.
Then loop for each test score, prompting the user to enter a test score,
validate, re enter if invalid score, then call setTestAtIndex passing the
entered test score and the index at which to set.
void printStudent() - this function will call the corresponding accessor
functions to print the student's first and last name, each test score, and
the average for the entered scores up to two places of precision.
Testing Default Constructor, input and print
Enter student's fname: John
Enter student's lname: Doe
How many tests? 3
Enter test score #1: 88
Enter test score #2: 87
Enter test score #3: 86
Student s
John Doe
Test #1: 88
Test #2: 87
Test #3: 86
Test Average: 87.00
Testing Copy Constructor
Student m
John Doe
Test #1: 88
Test #2: 87
Test #3: 86
Test Average: 87.00
Testing m.setTestAtIndex(99,0)
Student m **changed
John Doe
Test #1: 99
Test #2: 87
Test #3: 86
Test Average: 90.67
Student s **unchanged
John Doe
Test #1: 88
Test #2: 87
Test #3: 86
Test Average: 87.00
Testing Overloaded Assignment Operator
Student n
Sally Sue
Test #1: 44
Test #2: 87
Test #3: 86
Test Average: 72.33
Student m
John Doe
Test #1: 99
Test #2: 87
Test #3: 86
Test Average: 90.67
Once completed, submit to your depository directory 2020_S19/wk2/hw2.cpp.
If you have any questions,
ask me during office hours, before class, after class,
email me, call me, or find me and I'd be happy to help. Also, there's the
walk-in lab (SCI III rm 324) where there are tutors there to help if you
cannot reach me.