Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

our job in this exercise is to implement a copy constructor for the student class. Since the student class contains a pointer, by default C++

our job in this exercise is to implement a copy constructor for the student class. Since the student class contains a pointer, by default C++ will only copy the pointer when a copy is needed. To properly copy a student object, both the pointer and the array it points to needs to be copied. This is called a deep copy.

A main( ) program is provided that creates student objects and tests the various member functions. The program starts by creating 3 test students: pooja, drago, and sam. Here's the correct output:

pooja (12345): gpa is 2.75 drago (88425): gpa is 0 sam (99999): gpa is 2.2

#include #include

#include "student.h"

using namespace std;

// // testPassByValue: // // if copy constructor works properly, then the changes made by // this function will not impact the actual parameter in main(). // void testPassByValue(student S2) { for (int i = 0; i < 33; ++i) S2.addGrade('A');

cout << "** inside function 'testPassByValue' **" << endl;

cout << " " << S2.getName() << " (" << S2.getUIN() << "):" << " gpa is " << S2.GPA() << endl;

return; }

// // testcases // // A couple hard-coded test cases to start. // void testcases() { student pooja("pooja", 12345); pooja.addGrade('A'); pooja.addGrade('B'); pooja.addGrade('A'); pooja.addGrade('F');

cout << pooja.getName() << " (" << pooja.getUIN() << "):" << " gpa is " << pooja.GPA() << endl;

student drago("drago", 88425);

cout << drago.getName() << " (" << drago.getUIN() << "):" << " gpa is " << drago.GPA() << endl;

student grow("grow", 99999); char* originalArray = grow.Grades; grow.addGrade('A'); grow.addGrade('B'); grow.addGrade('A'); grow.addGrade('F'); grow.addGrade('W');

cout << grow.getName() << " (" << grow.getUIN() << "):" << " gpa is " << grow.GPA() << endl;

if (grow.Grades == originalArray) cout << "**Error: addGrade did not properly grow the Grades array?!" << endl; if (grow.N != 5) cout << "**Error: addGrade did not properly increase N?!" << endl; if (grow.ArrayCap != 8) cout << "**Error: addGrade did not properly increase Capacity?!" << endl;

cout << endl; }

// ############################################################## // // main // int main() { string name; int uin; char grade; int count = 0; int cap = 4;

testcases();

cout << "Enter a student's name (one word)> "; cin >> name; cout << "Enter their UIN> "; cin >> uin;

student S(name, uin);

char* originalArray = S.Grades;

cout << "Now enter 0 or more grades, one at a time" << endl; cout << "grade or #> "; cin >> grade;

while (grade != '#') { if (count == cap) originalArray = S.Grades;

S.addGrade(grade); count++;

if (count == (cap+1)) cap = cap * 2;

cout << "grade or #> "; cin >> grade; }

cout << endl;

cout << S.getName() << " (" << S.getUIN() << "):" << " gpa is " << S.GPA() << endl;

// // test that array grew properly if need be: // if (cap > 4) // array had to grow: { if (S.Grades == originalArray) cout << "**Error: addGrade did not properly grow the Grades array?!" << endl; } if (S.N != count) cout << "**Error: addGrade did not properly increase N?!" << endl; if (S.ArrayCap != cap) cout << "**Error: addGrade did not properly increase Capacity?!" << endl;

// // now let's test the copy constructor, which should allow us to // call this function with no changes to S: // originalArray = S.Grades; int originalN = S.N; int originalArrayCap = S.ArrayCap;

testPassByValue(S);

cout << S.getName() << " (" << S.getUIN() << "):" << " gpa is " << S.GPA() << endl;

if (S.Grades != originalArray) cout << "**Error: copy constructor incorrect, Grades array changed" << endl; if (S.N != originalN) cout << "**Error: copy constructor incorrect, N changed" << endl; if (S.ArrayCap != originalArrayCap) cout << "**Error: copy constructor incorrect, ArrayCap changed" << endl;

cout << endl; return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases New Opportunities For Connected Data

Authors: Ian Robinson, Jim Webber, Emil Eifrem

2nd Edition

1491930896, 978-1491930892

More Books

Students also viewed these Databases questions