Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#1 Consider the following class called Section that stores the final grades of a

ID: 3883599 • Letter: #

Question

#1

Consider the following class called Section that stores the final grades of all the students enrolled in a specific section of a class.

class Section {

public:

Section() { // constructor

   nST = 0;

}

void addGrade(float grade) {

   ST[nST] = grade;

   nST++;

}

private:

float ST[26];

int nST; // number of final grades stored

};

The ST member variable can store at most 26 students. Change the code so that ST can store as many final grades as needed when a Section object is created. (Hint: use dynamic memory). Change/add constructor and destructors as needed.

Add a copy constructor for the above case.

Explanation / Answer

/*Modified code snippet is given below;

Changes: 1. ST member variable is changed to a pointer type variable;

Changes: 2. a member variable of type int is added, and name of that variable is size;

Changes: 3. three constructors are defined accordingly: default,parametric and copy constructor;

Changes: 4. a destructor function is added*/

class Section {

public:

Section() { // constructor

nST = 0;

/*when Section object is created with this default constructor

then ST member variable can store 26 grades by default;

size member variable is initialized with 26 */

size = 26;

ST = new float[size];

}

Section(int size) { // parametric constructor

nST = 0;

size = size;

/*when Section object is created with this parametric constructor

then ST member variable can store as many final grades as needed;

that required number of final grade should be passed as parameter

at the time of Section object creation*/

ST = new float[size];

}

Section(const Section &obj) { //copy constructor

nST = 0;

/*when Section object is created with this copy constructor

then size member variable would be initialized with the size value of the given Section object;

ST member variable can store final grades of number equal to the value of size*/

size = obj.size;

ST = new float[size];

}

void addGrade(float grade) {

ST[nST] = grade;

nST++;

}

~Section() { // destructor

delete[] ST;

}

private:

float *ST = NULL; //declaring a floating type pointer for dynamic memory

int size;//number of final grade need to be stored

int nST; // number of final grades stored

};

/*If this helps you, please let me know by giving a positive thumbs up. In case you have any queries, do let me know. I will revert back to you. Thank you!!*/