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

// (a) complete the body of this copy constructor // .... } // destructor ~examT

ID: 3694428 • Letter: #

Question

// (a) complete the body of this copy constructor // .... } // destructor ~examType()

{ // (b) complete the body of this destructor // .... } int getNoOfStudents() { return noOfStudents; } void setGrade(int thePosition, double newGrade) { grades[thePosition] = newGrade; } double getGrade(int thePosition) { return grades[thePosition]; } double getAverage() { double total = 0; for (int i=0; i < noOfStudents; i ++) { total +=grades[i]; } return total/noOfStudents; } }; int main() { int size; // ask number of students taking the exam cout << "Enter no of students took the midterm: "; cin >> size; examType midterm1(size); //----------------------------- // get grades from the user for (int i = 0; i< size; i ++) { double newGrade; cout << "Enter grade for student " << (i+1) << ": "; cin >> newGrade; midterm1.setGrade(i, newGrade); } // print the grades cout << endl << "Printing grades " << endl; for (int i =0; i< size; i ++) { cout << "Student " << (i+1) << ": " << midterm1.getGrade(i) << endl; } // print Average cout << "The midterm1 average is: " << midterm1.getAverage() << endl; // Declaring another examType object called midterm2 // and initialize midterm2 by using the value of midterm1 object // NOTE THAT, the line below automatically invokes the copy constructor of the class examType // for midterm2. That is, midterm1 becomes theOtherExam in the copy constructor. examType midterm2 (midterm1);

// (c) print average for midterm2 (hint: the average for midterm1 and midterm2 should be the same if you have written // the copy constructor for examType class correctly). //... system("pause"); return 0; }

Explanation / Answer


#include <iostream>
#include <string>

using namespace std;

// this class is implemented to hold the grades for an exam
class examType
{

private:
   int maxNoOfStudents; // this is the max size of the dynamic list
   int noOfStudents; // this is the current number of elements in the dynamic array
   double *grades; // grades is a pointer the the dynamic array

public:
   // constructor with parameters
   examType(int maxSize)
   {
       noOfStudents = 0;
       maxNoOfStudents = maxSize;
       grades = new double[maxNoOfStudents];
   }

   ~examType()
   {
       delete[] grades;
       grades = NULL;

   }

   //syntax on 879
   examType& operator=(const examType&);
   // overload the assignment operator = to allow deep copy of the grades pointer!
   //...

   int getNoOfStudents()
   {
       return noOfStudents;
   }

   int getMaxNoOfStudents()
   {
       return maxNoOfStudents;
   }

   void append(double newGrade)
   {
       if (noOfStudents < maxNoOfStudents)
       {
           grades[noOfStudents] = newGrade;
           noOfStudents++;
       }
       else
       {
           cout << "Error: This list cannot get more elements. " << endl;
       }
   }

   double getGrade(int thePosition)
   {
       return grades[thePosition];
   }

   void print()
   {
       cout << "Number of Students in the Exam: " << noOfStudents << endl;

       for (int i = 0; i < noOfStudents; i++)
       {
           cout << grades[i] << " ";
       }
       cout << endl;
   }

};
//noOfStudents ~~   length
//maxNoOfStudents ~~   maxSize
//grades ~~ p
examType& examType::operator=(const examType& examObject){
   //syntax of overloading the assignment operator is on page 879
   if (this != &examObject){
           this->maxNoOfStudents = examObject.maxNoOfStudents;
           noOfStudents = examObject.noOfStudents;
           delete[] this->grades;

           grades = new double[maxNoOfStudents];

           for (int i = 0; i < noOfStudents; i++)
           grades[i] = examObject.grades[i];
       }
   return *this;
}


int main()
{

   examType midterm(10);
   examType copyOfMidterm(4);


   // enter grades for midterm1
   midterm.append(50);
   midterm.append(100);
   midterm.append(80);

   // print results for midterm 1  
   midterm.print();

   copyOfMidterm = midterm; // by default assignment operator = does shallow copy here.
   // your job is to overload the assignment operator = in the class above, so that deep copying is done!

   copyOfMidterm.print();


   copyOfMidterm.append(55); // add a new grade to copyOf Midterm  

   // *****hint: if you overloaded the = operator correctly and did deep copying you
   // should see that the copyOfMidterm object prints the additional grade!
   midterm.print();
   copyOfMidterm.print();


   system("pause");
   return 0;
}

sample output


Number of Students in the Exam: 3                                                                                                                           
50      100     80                                                                                                                                          
Number of Students in the Exam: 3                                                                                                                           
50      100     80                                                                                                                                          
Number of Students in the Exam: 3                                                                                                                           
50      100     80                                                                                                                                          
Number of Students in the Exam: 4                                                                                                                           
50      100     80      55