In: Computer Science
Write a class Roster that is identified by the course name, course code, number of credits, instructor name, and contains a list of students stored in an array. For now you can use stack allocated array of MAX_CAPACITY=10 (define MAX_CAPACITY as a macro in the .h file). Later we will make this array dynamic to allow it to grow. Provide necessary constructors, accessor functions for each member, and mutator functions for each member. Provide a function to add a student to a Roster, delete student from a Roster, and search for a student. Provide a function to output all Students from a roster to console in a nice format.
For now you should not worry about the order of Students in your Roster. They do not need to be sorted.
Write a driver program to test your Roster class.
Use declare/define/use approach
#include<bits/stdc++.h>
#define MAX_CAPACITY 10
using namespace std;
class Roaster{
private:
string name;
string code;
int numOfCredits;
string instructorName;
string studentName[MAX_CAPACITY];
int index;
public:
Roaster(string name,string code,int numOfCredits,string instructorName)
{
this->name=name;
this->code=code;
this->numOfCredits=numOfCredits;
this->instructorName=instructorName;
this->index=-1;
}
void setName(string name)
{
this->name=name;
}
void setCode(string code)
{
this->code=code;
}
void setNumberOfCredit(int credit)
{
this->numOfCredits=credit;
}
void setInstructorName(string name)
{
this->instructorName=name;
}
string getName()
{
return this->name;
}
string getCode()
{
return this->code;
}
int getNumberOfCredits()
{
return this->numOfCredits;
}
string getInstructorName()
{
return this->instructorName;
}
void addStrudent(string name)
{
this->studentName[++index]=name;
}
int searchStudent(string name)
{
int i;
for (i = 0; i <=index; i++)
if (studentName[i] == name)
return i;
return -1;
}
void deleteStudent(string name)
{
int pos = searchStudent(name);
if (pos == - 1)
{
cout << "Student not found";
}
// Deleting element
int i;
for (i = pos; i <=index-1; i++)
studentName[i] = studentName[i + 1];
index=index-1;
}
void printStudent()
{
for(int i=0;i<=index;i++)
{
cout<<studentName[i]<<endl;
}
}
};
int main()
{
Roaster roaster("CSE","CSE101",12,"Anirban");
roaster.addStrudent("chandan");
roaster.addStrudent("Alex");
roaster.addStrudent("David");
roaster.addStrudent("Malan");
roaster.printStudent();
cout<<"\n\n";
roaster.deleteStudent("Malan");
roaster.printStudent();
cout<<roaster.searchStudent("David")<<endl;
}