Question: This exercise consists of making a programmer defined copy constructor for the Student class below. The copy constructor below copies non-pointer fields and then, using
This exercise consists of making a programmer defined copy constructor for the Student class below.
- The copy constructor below copies non-pointer fields and then, using the new operator dynamically allocates memory for the testScores array.
//copy constructor
Lab8A_StudentTestScores(const Lab8A_StudentTestScores &obj)
{
studentName = obj.studentName;
numTestScores = obj.numTestScores;
testScores = new double[numTestScores];
for (int i=0; i // complete the for loop body to copy individual array elements } #ifndef STUDENTTESTSCORES_H #define STUDENTTESTSCORES_H #include using namespace std; const double DEFAULT_SCORE = 0.0; class Lab8A_StudentTestScores { private: string studentName; double *testScores; int numTestScores; //private member function to create an array of test scores void createTestScoresArray(int size) { numTestScores = size; testScores = new double[size]; for (int i=0; i { testScores[i] = DEFAULT_SCORE; } } public: //constructor Lab8A_StudentTestScores(string name, int numScores) { studentName = name; createTestScoresArray(numScores); } // Insert Copy constructor here // Insert Copy constructor here // Insert Copy constructor here // Insert Copy constructor here // Insert Copy constructor here // Insert Copy constructor here //Destructor ~Lab8A_StudentTestScores() { delete [] testScores;} void setTestScore(double score, int index) {testScores[index] = score;} void setStudentName(string name) {studentName = name;} string getStudentName() const {return studentName;} int getNumTestScores() const {return numTestScores;} double getTestScore(int index) const {return testScores[index];} void displayStudent() { cout << "Name: " << endl; cout << getStudentName() << endl; cout << "Test Scores:" << endl; for (int i=0; i { cout << getTestScore(i) << " "; } cout << endl << "=============" << endl; }; }; #endif #include #include #include "Lab8A_StudentTestScores.h" using namespace std; int main () { Lab8A_StudentTestScores student1("Payton Hall", 2); student1.setTestScore(100.0, 0); student1.setTestScore(95.0, 1); student1.displayStudent(); Lab8A_StudentTestScores student2 = student1; student2.setStudentName("Mary Baker"); student2.setTestScore(80.0, 0); student2.setTestScore(75.0, 1); cout << "Student 1" << endl; student1.displayStudent(); cout << "Student 2" << endl; student2.displayStudent(); system ("PAUSE"); //you need this for windows system return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
