C++ only please. Create a class StudentList that contains Student objects. It ha
ID: 3585028 • Letter: C
Question
C++ only please.
Create a class StudentList that contains Student objects. It has the following data members:
class StudentList{ private: static const size_t LIST_SIZE = 150; // the maximum items in the list
Student *items; // items will point to the dynamically allocated
array size_t numItems; // the number of items currently in the list
public: // member functions to be defined by to be defined and completed by YOU! Hooray!
}
The class should have the following member functions:
- One or multiple constructors - A copy constructor
- A function that prints all the students’ names.
- A function that checks if a student exists in a list by given either the ID or the names.
- Accessing functions
- A destructor Create a driver to test the class.
Submit the files: slist.h, slist.cpp, slisttest.cpp
#####################################################################
student.h
--------------
#ifndef STUDENT_H
#define STUDENT_H
class Student{
public:
Student(char*, char*, int); // Constuctor
Student(const Student &); // Copy Constuctor
~Student(); // Destructor
void display(); // function to display student info
private:
char* fname;
char* lname;
int id;
};
#endif // STUDENT_H
########################################################
student.cpp
----------------
#include <iostream>
#include <cstring>
#include "Student.h"
using namespace std;
Student::Student(char* pF, char* pL, int idnum){
id = idnum;
fname = new char[strlen(pF)+1];
lname = new char[strlen(pL)+1];
strcpy(fname, pF);
strcpy(lname, pL);
}
// Constructor
Student::Student(const Student &stud){
id = stud.id;
fname = new char[strlen(stud.fname)+1];
lname = new char[strlen(stud.lname)+1];
strcpy(fname, stud.fname);
strcpy(lname, stud.lname);
}
Student::~Student(){
delete [] fname;
delete [] lname;
}
void Student::display(){
cout<<"Id: "<<id<<endl;
cout<<"First Name: "<<fname<<endl;
cout<<"Last Name: "<<lname<<endl<<endl;
}
##########################################
stest.cpp
------------------
#include <iostream>
#include <cstring>
#include "Student.h"
using namespace std;
int main(){
char fnmae[50], lname[50];
int id;
cout<<"Enter student id: ";
cin>>id;
cout<<"Enter first name: ";
cin>>fnmae;
cout<<"Enter last name: ";
cin>>lname;
// Creating Student Object
Student std(fnmae, lname, id);
// printing info
std.display();
return 0;
}
THANK YOU!!
Explanation / Answer
#####################################################################
StudentList.h
--------------
#ifndef STUDENTLIST_H
#define STUDENTLIST_H
class StudentList
{
public:
StudentList(Student*, int); // Constuctor
StudentList(const StudentList &); // Copy Constuctor
~StudentList(); // Destructor
void display(); // function to display all student info in list
void searchSt(Student std); //function to search student through Id
private:
//static const size_t LIST_SIZE = 150;
size_t numItems;
Student *items;
}
#endif // STUDENTLIST_H
########################################################
studentList.cpp
----------------
#include <iostream>
#include <cstring>
#include "StudentList.h"
using namespace std;
// parameterized Constructor
StudentList:: StudentList (Student *list,size_t size){
numItems=size;
Student *items=new Student[numItems];
items=list;
}
// Copy Constructor
StudentList:: StudentList (const StudentList &studL){
Student *copyItems=new Student[studL. numItems];
copyItems=studL.items;
}
StudentList::~ StudentList t(){
delete [] items;
}
void StudentList::display(){
Int i;
for(i=0;i<numItems;i++)
{
cout<<”Id of student:”<< items[i].id <<” “;
cout<<”First name:”<< items[i].fname <<” “;
cout<<”Last name:”<< items[i].lname <<” “;
}
}
bool StudentList:: searchSt(Student st)
{
Int i;
Int stuId=std.id;
char *finame=std.fname;
char *laname=std.lname;
for(i=0;i<numItems;i++)
{
If(items[i].id==stuId)
{
return 1; //true
}
else if((items[i].fname==finame )&&(items[i].lname==laname))
{
return 1; //true
}
else
return 0; //false
}
}
##########################################
slisttest.cpp
------------------
#include <iostream>
#include <cstring>
#include "StudentList.h"
using namespace std;
int main(){
char fnmae[50], lname[50];
int id,i;
size_t n;
cout<<”Enter number of student information you want to put into the list”;
cin>>n;
Student *list = new Student[n];
for(i=0;i<n;i++)
{
cout<<"Enter student id: ";
cin>>id;
list[i].id=id;
cout<<"Enter first name: ";
cin>>fnmae;
list[i].fname=fname;
cout<<"Enter last name: ";
cin>>lname;
list[i].lname=lname;
}
// Creating StudentList Object
StudentList std(list, n);
// printing info
std.display();
std. searchSt(list[3]);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.