Create a class named Student that has three member variables: name - A string th
ID: 3667509 • Letter: C
Question
Create a class named Student that has three 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 dynamic array of strings used to store names of the classes that the student is enrolled in.
a. Include a two argument constructor that accepts the name of the student and the number of enrolled courses as input.
b. A method that sets all course names to "ECGR"
c. A method that outputs the names of all courses.
d. A destructor that releases all memory that has been allocated.
Below is the testing program -
int main()
{
Student stud1("Alice", 6), stud2("Bob", 4);
stud1.getInput();
stud1.printClasses();
cout << endl;
stud2.getInput();
stud2.printClasses();
cout << endl;
return 0;
}
Explanation / Answer
Answer -
#include <iostream>
#include<string>
using namespace std;
class Student
{
public:
string name;
int numClasses;
string classList[10];
Student(string Cname, int CnumClasses)
{
this->name=Cname;
this->numClasses=CnumClasses;
string classList[this->numClasses];
}
void getInput()
{
for(int i=1;i<=this->numClasses;i++)
{
this->classList[i]="ECGR";
}
}
void printClasses()
{
cout<<" Student Name :"<<this->name;
cout<<" Subjects enrolled :"<<this->numClasses;
for(int i=1;i<=this->numClasses;i++)
{
cout<<" Subject "<<i<<" :"<<this->classList[i];
}
}
~Student()
{
}
};
int main()
{
Student stud1("Alice", 6), stud2("Bob", 4);
stud1.getInput();
stud1.printClasses();
cout << endl;
stud2.getInput();
stud2.printClasses();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.