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

Create a class named Student that has three member variables: name %u2013 A stri

ID: 3538459 • Letter: C

Question



Create a class named Student that has three member variables: name %u2013 A string that stores the name of the student numClasses %u2013 An integer that tracks how many courses the student is currently enrolled in classList %u2013 A dynamic array of strings used to store the names of the classes that the student is enrolled in Write appropriate constructor(s), mutator, and accessor methods for the class along with the following: %uF0B7 A method 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. %uF0B7 A method that outputs the name and list of all courses. %uF0B7 A method that resets the number of classes to 0 and the classList to an empty list. %uF0B7 An overloaded assignment operator that correctly makes a new copy of the list of courses. %uF0B7 A destructor that releases all memory that has been allocated. Write a main method that tests all of your functions.

Hint: Recall that cin >> variable leaves a newline in the buffer. This can be a problem if you are mixing cin >> variable and getline. Use cin.ignore to discard the newline.

Part of the correct Code

//student.cpp

//

//This program defines a class for storing the names of classes

// that a student has enrolled in.

#include

#include

#include

using namespace std;

class Student

{

public: Student();

~Student();

void InputData(); // Input all data from user

void OutputData(); // Output class list to console

void ResetClasses(); // Reset class list

tudent& operator =(const Student& rightSide);// Assignment operator

private: string name;

int numClasses;

string *classList;

};

// --------------------------------

// ----- ENTER YOUR CODE HERE -----

// --------------------------------

// ======================

// Student::Student

// The default constructor initialized variables to empty.

// The dynamic array is intialized to NULL.

// ======================

Student::Student()

{

Add Your code here

}

// ======================

// Student::~Student

// The destructor frees up any memory allocated to

// the dynamic array.

// ======================

Student::~Student()

{

Add Your code here

}

// ======================

// Student::ResetClasses

// This method deletes the class list

// ======================

void Student::ResetClasses()

{


Add Your code here }

}

// ======================

// Student::InputData

// This method inputs all data from the user.

// It allows the user to enter an arbitrary number of classes

// using a dynamic array to store the strings.

// ======================

void Student::InputData()

{

int i;

// Reset the class list just in case this method

// was called again and the dynamic array wasn't cleared

ResetClasses();

cout << "Enter student name." << endl;

getline(cin, name);

cout << "Enter number of classes." << endl;

cin >> numClasses;

cin.ignore(2,' '); // Discard extra newline

if (numClasses > 0)

{

// Dynamically construct array large enough to hold the

// number of classes

classList = new string[numClasses];

// Loop through the number of classes, inputting

// the name of each one into our dynamic array

for (i=0; i;>

{

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

getline(cin, classList[i]);

}

}

cout << endl;

}

// ======================

// Student::OutputData

// This method outputs the data entered by the user.

// ======================

void Student::OutputData()

{


Add Your code here }

// ======================

// Student::=

// This method copies a new classList to

// the target of the assignment. Without overloading this

// operator, we would end up with two references to the same

// class list if the assignment operator is used.

// ======================

Student& Student::operator =(const Student& rightSide)

{

Add Your code here

}

// ======================

// main function

// ======================

int main()

{

// Test our code with two student classes

Student s1, s2;

s1.InputData(); // Input data for student 1

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

s1.OutputData(); // Output data for student 1

cout << endl;

s2 = s1;

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

s2.OutputData(); // Should output same data as for student 1

s1.ResetClasses();

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

s1.OutputData(); // Should have no classes

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

s2.OutputData(); // Should still have original classes

cout << endl;

return 0;

}

Explanation / Answer

//COMPLETE CODE


//


//This program defines a class for storing the names of classes


// that a student has enrolled in.


#include <iostream>


#include <cstdlib>


#include <string>


using namespace std;


class Student


{


public: Student();


~Student();


void InputData(); // Input all data from user


void OutputData(); // Output class list to console


void ResetClasses(); // Reset class list


//accessors


string getName() const;


int getNumClasses() const;


string* getClassList() const;


//mutators


void setName(string n);


void setNumClasses(int num);


void setClassList(string* list);


Student& operator =(const Student& rightSide);// Assignment operator


private: string name;


int numClasses;


string *classList;


};


// ======================


// Student::Student


// The default constructor initialized variables to empty.


// The dynamic array is intialized to NULL.


// ======================


Student::Student()


{


name="";


numClasses=0;


classList = NULL;


}


// ======================


// Student::~Student


// The destructor frees up any memory allocated to


// the dynamic array.


// ======================


Student::~Student()


{



delete[] classList;


}


//accessors


string Student::getName() const{


return name; }


int Student::getNumClasses() const{


return numClasses; }


string* Student::getClassList() const{


return classList; }


//mutators


void Student::setName(string n)


{ name=n; }


void Student::setNumClasses(int num)


{ numClasses=num;; }


void Student::setClassList(string* list)


{ classList=list; }



// ======================


// Student::ResetClasses


// This method deletes the class list


// ======================


void Student::ResetClasses()


{


numClasses=0;


delete[] classList;


}


// ======================


// Student::InputData


// This method inputs all data from the user.


// It allows the user to enter an arbitrary number of classes


// using a dynamic array to store the strings.


// ======================


void Student::InputData()


{


int i;


// Reset the class list just in case this method


// was called again and the dynamic array wasn't cleared


ResetClasses();


cout << "Enter student name." << endl;


getline(cin, name);


cout << "Enter number of classes." << endl;


cin >> numClasses;


cin.ignore(2,' '); // Discard extra newline


if (numClasses > 0)


{


// Dynamically construct array large enough to hold the


// number of classes


classList = new string[numClasses];


// Loop through the number of classes, inputting


// the name of each one into our dynamic array


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


{


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


getline(cin, classList[i]);


}


}


cout << endl;


}


// ======================


// Student::OutputData


// This method outputs the data entered by the user.


// ======================


void Student::OutputData()


{


cout<<"Student name: "<<name<<endl;


cout<<"Number of Classes: "<<numClasses<<endl;


cout<<"Class List: "<<endl;


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


{


cout << "Class " << (i+1) <<": "<<classList[i]<< endl;



}


}


// ======================


// Student::=


// This method copies a new classList to


// the target of the assignment. Without overloading this


// operator, we would end up with two references to the same


// class list if the assignment operator is used.


// ======================


Student& Student::operator =(const Student& rightSide)


{


name=rightSide.getName();


numClasses=rightSide.getNumClasses();


classList = new string[numClasses];


string* tmp=rightSide.getClassList();


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


{


classList[i]=tmp[i];



}


}



// ======================


// main function


// ======================


int main()


{


// Test our code with two student classes


Student s1, s2;


s1.InputData(); // Input data for student 1


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


s1.OutputData(); // Output data for student 1


cout << endl;


s2 = s1;


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


s2.OutputData(); // Should output same data as for student 1


s1.ResetClasses();


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


s1.OutputData(); // Should have no classes


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


s2.OutputData(); // Should still have original classes


cout << endl;


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