Winter 2016 Assignment 5 Due February 16th, 2016 Expand the student class you wr
ID: 3668184 • Letter: W
Question
Winter 2016
Assignment 5
Due February 16th, 2016
Expand the student class you wrote last week, add the following variable:
classList – A dynamic array of strings used to store the names of the classes that the
student is enrolled in
Add appropriate constructor(s), destructor, and update the overloaded assignment operator:
• Functions should return any memory that is no longer needed or not being referenced.
Add a function that outputs the name and list of all courses.
Add a function called addClass that accepts a single string and adds that to the classList array. It
should increase the m_numClasses variable.
Write a main function that tests all of your functions.
Submit a zip file of all source code files.
By Using below code this is my previous assignment based on this code assignment-5 is depended
#include <cstring>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student{
private:
string m_fname;
string m_lname;
double m_gpa;
int m_numClasses;
public:
//default constructor
Student(){
}
//mutator, and accessor methods
string getFname(){
return m_fname;
}
void setFname(string name){
m_fname = name;
}
string getLname(){
return m_lname;
}
void setLname(string name){
m_lname = name;
}
double getGpa(){
return m_gpa;
}
void setGpa(double gpa){
m_gpa = gpa;
}
int getNumclasses(){
return m_numClasses;
}
void setNumclasses(int num){
m_numClasses = num;
}
void Display() {
cout << " Full Name : " << m_fname << " " << m_lname;
cout << " GPA : " << m_gpa;
cout << " Num of classes : " << m_numClasses;
cout << " _________________________________" << endl;
}
// Overloading of Assignment Operator
void operator=(const Student &Stu ) {
m_fname = Stu.m_fname;
m_lname = Stu.m_lname;
m_gpa = Stu.m_gpa;
m_numClasses = Stu.m_numClasses;
}
};
int main(int argc, char** argv) {
Student objArr[20];
Student stuObj;
// ifstream readfile ();
string line;
string fname, lname;
double gpa;
int numc;
ifstream infile;
infile.open("D:\sai.txt");// save text file in D drive.
infile >> fname >> lname >> gpa >> numc;
getline(infile, line);
int counter = 0; //num of records in file.
while(infile)
{
counter++;
stuObj.setFname(fname);
stuObj.setLname(lname);
stuObj.setGpa(gpa);
stuObj.setNumclasses(numc);
objArr[counter-1] = stuObj;
infile >> fname >> lname >> gpa >> numc;
getline(infile, line);
}
for(int i=0;i<counter;i++){
objArr[i].Display();
}
cout << "Total number of students = " << counter;
return 0;
}
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
class Student {
private:
string m_fname;
string m_lname;
double m_gpa;
int m_numClasses;
string *classList;
public:
// default constructor
Student() {
m_numClasses = 0;
classList = NULL;
}
// destructor
~Student() {}
// mutator, and accessor methods
string getFname(){
return m_fname;
}
void setFname(string name){
m_fname = name;
}
string getLname(){
return m_lname;
}
void setLname(string name){
m_lname = name;
}
double getGpa(){
return m_gpa;
}
void setGpa(double gpa){
m_gpa = gpa;
}
int getNumClasses(){
return m_numClasses;
}
void setNumClasses(int num){
m_numClasses = num;
}
void Display() {
cout << " Full Name : " << m_fname << " " << m_lname;
cout << " GPA : " << m_gpa;
cout << " Num of classes : " << m_numClasses;
cout << " _________________________________" << endl;
}
void displayClassList() {
cout << " Full Name : " << m_fname << " " << m_lname << endl;
cout << "Class List :" << endl;
// iterate and display all the available class names
for (int i = 0; i < m_numClasses; ++i) {
cout << (i + 1) << ". " << classList[i] << endl;
}
}
void addClass(string className) {
// create new list store old data plus additional value
string *updatedList;
updatedList = new string[m_numClasses + 1];
// copy content from old list
for (int i = 0; i < m_numClasses; ++i) {
updatedList[i] = classList[i];
}
// add the new member
updatedList[m_numClasses] = className;
// free memory of old list if not already allocated
if (classList != NULL) {
delete[] classList;
classList = NULL;
}
// update the member variable classList to point to new list
classList = updatedList;
// increment the m_numClasses count
m_numClasses += 1;
}
// Overloading of Assignment Operator
void operator=(const Student &Stu) {
m_fname = Stu.m_fname;
m_lname = Stu.m_lname;
m_gpa = Stu.m_gpa;
// clear memory if already allocated
if (classList != NULL) {
delete[] classList;
classList = NULL;
}
// copy content from Stu to current variable array
for (int i = 0; i < Stu.m_numClasses; ++i) {
addClass(Stu.classList[i]);
}
}
};
// helper function to read student data
void readStudentData(Student &a) {
string fname, lname, className;
double gpa;
int numc;
// read data for student a
cin >> fname >> lname >> gpa >> numc;
a.setFname(fname);
a.setLname(lname);
a.setGpa(gpa);
// read list of classes and add them
for (int i = 0; i < numc; ++i) {
cin >> className;
a.addClass(className);
}
}
int main(int argc, char** argv) {
// initializing standard input as input.txt file
freopen("input.txt", "r", stdin);
Student a;
readStudentData(a);
// display function test
a.displayClassList();
cout << "-------------------------------" << endl;
// assignment operator check
Student c = a;
c.displayClassList();
cout << "-------------------------------" << endl;
return 0;
}
/***input.txt***/
JOHN MAX 8.2 2 DS ALGO
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.