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

Define a class named Student that has three member variables: Member variables n

ID: 675152 • Letter: D

Question

Define a class named Student that has three member variables:

Member variables

name – A string that stores the name of the student.

numClasses – An integer that tracks how many courses the student is currently enrolled in.

classList – A pointer to a dynamic array of strings used to store the names of the classes that the student is enrolled in.

Member functions:

Write an appropriate constructor for the class along with the following:

A method that resets the number of classes to 0 and the classList to an empty list.

An overloaded assignment operator that correctly assigns a Student object to another.

A copy constructor that allows creating a new Student object and initializing it with an existing Student object.

An overloaded istream operator that inputs all values from the user, including the list of class names. This method will have to support input for an arbitrary number of classes.

An overloaded ostream method that outputs the name and list of all courses.

A destructor that releases all memory that has been allocated.

Good news! The main is given as below. The running page also follows.

#include <iostream>

#include <cstdlib>

#include <string>

using namespace std;

……

int main()

{

       // Test your code with two student classes

       Student s1, s2;

       cin >> s1;                 // Input data for student 1, refer to output page

       cout << "Student 1's data:" << endl;

       cout << s1<<endl;          // Output data for student 1

       cout << endl;

       s2 = s1;

       cout << "Student 2's data after assignment from student 1:" << endl;

       cout<<s2<<endl;            // Should output same data as for student 1

       s1.ResetClasses();

       cout << "Student 1's data after reset:" << endl;

       cout<<s1<<endl;            // Should have no classes

       cout << "Student 2's data, should still have original classes:" << endl;

       cout<<s2<<endl;            // Should still have original classes

       Student s3 = Student(s2);

       cout << "Student 3's data after being created and initialized with student 2:" << endl;

       cout << s3<<endl;          // Should output same data as for student 2

       s2.ResetClasses();

       cout << "Student 2's data after reset:" << endl;

       cout << s2 << endl;        // Should have no classes

       cout << "Student 3's data, should still have original classes:" << endl;

       cout << s3 << endl;        // Should still have original classes

       return 0;

Output:

Enter student name:

John Smith

Enter number of classes:

4

Enter name of class 1

COMP1210 Introduction to Computing

Enter name of class 2

COMP2140 Programming I

Enter name of class 3

COMP4100 Operating System

Enter name of class 4

COMP4500 Senior Project

Student 1's data:

Name: John Smith

Number of classes: 4

Class 1:COMP1210 Introduction to Computing

Class 2:COMP2140 Programming I

Class 3:COMP4100 Operating System

Class 4:COMP4500 Senior Project

Student 2's data after assignment from student 1:

Name: John Smith

Number of classes: 4

Class 1:COMP1210 Introduction to Computing

Class 2:COMP2140 Programming I

Class 3:COMP4100 Operating System

Class 4:COMP4500 Senior Project

Student 1's data after reset:

Name: John Smith

Number of classes: 0

Student 2's data, should still have original classes:

Name: John Smith

Number of classes: 4

Class 1:COMP1210 Introduction to Computing

Class 2:COMP2140 Programming I

Class 3:COMP4100 Operating System

Class 4:COMP4500 Senior Project

Student 3's data after being created and initialized with student 2:

Name: John Smith

Number of classes: 4

Class 1:COMP1210 Introduction to Computing

Class 2:COMP2140 Programming I

Class 3:COMP4100 Operating System

Class 4:COMP4500 Senior Project

Student 2's data after reset:

Name: John Smith

Number of classes: 0

Student 3's data, should still have original classes:

Name: John Smith

Number of classes: 4

Class 1:COMP1210 Introduction to Computing

Class 2:COMP2140 Programming I

Class 3:COMP4100 Operating System

Class 4:COMP4500 Senior Project

Press any key to continue . . .

Explanation / Answer

#include <iostream>

#include <cstdlib>

#include <string>

using namespace std;

class Student {

string name;

int numClasses;

string *classList;

  

public:

Student() {

numClasses=0;

classList = NULL;

}

  

Student(string n, int nc, string *cl) {

name = n;

numClasses = nc;

  

classList = (string *)malloc(sizeof(string)*nc);

for (int i=0; i<nc; i++) {

classList[i] = cl[i];

}

}

  

void ResetClasses() {

numClasses = 0;

classList = NULL;

}

  

void operator=(Student &a) {

name = a.name;

numClasses = a.numClasses;

  

classList = (string *)malloc(sizeof(string)*numClasses);

for (int i=0; i<numClasses; i++) {

classList[i] = a.classList[i];

}

}

  

friend ostream &operator<<( ostream &output,

   const Student &D )

{

output<<"Name: "<<D.name<<" ";

output<<"Number of classes: "<<D.numClasses<<" ";

for (int i = 0; i<D.numClasses; i++) {

output<<"Class "<<(i+1)<<":"<<D.classList[i]<<" ";

}

return output;

}

  

friend istream &operator>>( istream &input, Student &D )

{

cout<<"Enter student name: ";

input>>D.name;

cout<<"Enter number of classes: ";

input>>D.numClasses;

for (int i=0; i<D.numClasses; i++) {

cout<<"Enter name of class "<<(i+1)<<" ";

string *temp = (string *)realloc(D.classList, sizeof(string)*(i+1));

input>>temp[i];

D.classList = temp;

}

return input;

}

};

int main()

{

// Test your code with two student classes

Student s1, s2;

cin >> s1; // Input data for student 1, refer to output page

cout << "Student 1's data:" << endl;

cout << s1<<endl; // Output data for student 1

cout << endl;

  

s2 = s1;

cout << "Student 2's data after assignment from student 1:" << endl;

cout<<s2<<endl; // Should output same data as for student 1

  

s1.ResetClasses();

cout << "Student 1's data after reset:" << endl;

cout<<s1<<endl; // Should have no classes

  

cout << "Student 2's data, should still have original classes:" << endl;

cout<<s2<<endl; // Should still have original classes

  

Student s3 = Student(s2);

cout << "Student 3's data after being created and initialized with student 2:" << endl;

cout << s3<<endl; // Should output same data as for student 2

  

s2.ResetClasses();

cout << "Student 2's data after reset:" << endl;

cout << s2 << endl; // Should have no classes

  

cout << "Student 3's data, should still have original classes:" << endl;

cout << s3 << endl; // Should still have original classes

  

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote