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

Given code: class Course { private: int id; string name; int * student_ids; int

ID: 3540603 • Letter: G

Question

Given code:

class Course

{

private:

int id;

string name;

int * student_ids;

int num_students;

public:

/********************** Declare functions/constructors/destructors  here ***************************/








};


a) The class does not have a declared constructor (so C++ will automatically define a default constructor). Modify the class to add a constructor that takes an id, name, and number of students . Implement the constructor and initialize the values of the members based on the input. student-ids should be initialized to a new dynamically allocated array of size num-students and the values in the array should be set to 0.

b) Add the function void setStudentId( int num, int id) to the class and provide the implementation below.

c) Add a destructor to the class and provide the impementation below.

Explanation / Answer

#include <iostream>

using namespace std;


class Course
{
private:
int id;
string name;
int * student_ids;
int num_students;
public:
/********************** Declare functions/constructors/destructors here ***************************/
Course(int id,string name,int no_of_students)
{
this->id = id;
this->name = name;
num_students = no_of_students;
student_ids = new int[num_students];
for(int i=0; i<num_students; i++)
student_ids[i] = 0;
}
void setStudentId(int num, int id)
{
student_ids[num] = id;
}
~Course()
{
delete student_ids;
}
string getName()
{
    return name;
}
};

int main()
{
Course C1(1,"ravi",5);
C1.setStudentId(2,45);
cout << C1.getName() << 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