Can someone help me with this in C++? Using the above skeleton for your specific
ID: 3689187 • Letter: C
Question
Can someone help me with this in C++?
Using the above skeleton for your specification and the method descriptions below, implement a class named Students.h. The class has a dynamically allocated array of Student data types. The Student data type is declared as a nested structure within the class itself.
constructor - specifies the size of the array using the parameter. The initializes the object by creating the array, setting the capacity using the parameter, and initializing the array element's used field to false.
destructor - responsible for deallocating the array.
clear() - is responsible for assigning false to the used field of all the elements.
insert() - is responsible for setting s[i]'s name and gpa fields. Also sets used to true. Returns -1 without setting anything if index i is out of bounds. Sets the fields and returns 0 otherwise.
remove() - is responsible for "removing" the student from the array. Sets s[i].used to false. Returns -1 without setting anything if index is out of bounds. Sets used to false and returns 0 otherwise.
find() - returns true if an element containing name exists in the array with it's used field set to true, false otherwise.
isFull() - returns true if every element of the array has it's used field set to true, returns false otherwise.
isEmpty() - returns true if every element of the array has it's used field set to false.
print() - displays the name and gpa field for each element in the array whose used field is set to true.
Explanation / Answer
Hi I have implemented youe all functions.
#ifndef STUDENTS_H
#define STUDENTS_H
#include <string>
using std::string;
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
using std::left;
using std::setprecision;
using std::fixed;
class Students
{
private:
struct Student
{
string name;
double gpa;
bool used;
};
Student* s;
int capacity;
public:
Students( int );
~Students();
void clear();
int insert( int, string, double );
int remove( int );
bool find( string ) const;
bool isEmpty() const;
bool isFull() const;
void print() const;
};
Students::Students(int size){
capacity = size;
s = new Student[size];
for(int i=0; i<size; i++)
s[i].used = false;
}
void Students::clear(){
for(int i=0; i<capacity; i++)
s[i].used = false;
}
Students::~Students(){
delete []s;
}
int Students::remove( int index){
if(index < 0 || index>=capacity)
return -1;
s[index].used = false;
return 0;
}
bool Students::find( string name) const{
for(int i=0; i<capacity; i++){
if((name.compare(s[i].name) == 0) && (s[i].used == true))
return true;
}
return false;
}
bool Students::isEmpty() const{
for(int i=0; i<capacity; i++)
if(s[i].used == true)
return false;
return true;
}
bool Students::isFull() const{
for(int i=0; i<capacity; i++)
if(s[i].used == false)
return false;
return true;
}
int Students::insert( int index, string n, double g){
if(index < 0 || index>=capacity)
return -1;
s[index].name = n;
s[index].gpa = g;
s[index].used = true;
return 0;
}
void Students::print() const{
for(int i=0; i<capacity; i++){
if(s[i].used == true){
cout<<"Name: "<<s[i].name<<", GPS: "<<s[i].gpa<<endl;
}
}
}
#endif