question archive Write a program that creates Student Objects using data from the attached “students

Write a program that creates Student Objects using data from the attached “students

Subject:Computer SciencePrice:3.87 Bought8

Write a program that creates Student Objects using data from the attached “students.txt” file. Add code to limit the number of students created to 24.  At a minimum, each Student object should contain the following attributes: 

  • First name (string and const)  - 

  • Last name (string and const)  -

  • Social Security Number (string) – 

  • Four test Grades (array of type double) –

  • Student Average test Grade (double) – 

  • Student Number (int and static) – Initially set to zero. Each time a Student object is created, the Student Number is incremented by one and when destroyed, decremented by one.  

Add member functions to the Student class to: 

  • Function to return the average test Grade of a student: (Sum of Four test grades divided by four).   

  • Getter and Setter Functions for each attribute

  • Function to display associated attributes of the Student

Create a single ClassRoom object with the following attributes  

  • Name of the ClassRoom object (string ) – Choose any name (i.e. CSC134)

  • Count of the number of students in the ClassRoom (int)

  • Dynamic Array of Student Objects (maximum size is 24) – Represented as an array of pointers.

Add member functions to the ClassRoom class to: 

  • ClassRoom Constructor Function – 

  • ClassRoom Destructor Function – 

  • Read the input data file and create the Student objects. – 

  • Sort list of students by student average – 

  • Sort list of students by student last name – 

  • Return the average grade of all students (Sum of Student Average divided by number of students). – 

  • Display a summary of all the students in the ClassRoom object and the associated attributes of each student attributes. – 

  • Return  the count of student objects created – 

Write the main() driver to demonstrate the following: 

  1. Create a ClassRoom Object  

  1. Use the ClassRoom Object to read the Student Objects from the input file ”students.txt” 

  1. Use the ClassRoom Object to sort list of students by student average 

  1. Display the list of students by student average 

  1. Use the ClassRoom Object to sort list of students by student last name 

  1. Display the list of students by student last name 

  1. Use the ClassRoom Object to display the average grade of all students. 

  1. Use the ClassRoom Object to display the number of Student objects created 

NOTE: No global non-constant variables should be used. You can add more data members or member functions for Student and ClassRoom Class Definitions if needed. C++

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

Answer:

CODE-

student.h

#include<iostream>
#include<string>
using namespace std;


//the student class in header file, with all the
//other fields
class Student
{
private:
string fname;
string lname;
string ssn;
double grades[4];
double avg;


public:
static int num;
//constructor to set all fields
Student();
Student(string fname,string lname,string ssn,double grades[4]);
string getFName();
string getLName();
string getSSN();
double * getGrades();
double getAvg();
void setFName(string arg);
void setLName(string arg);
void setSSN(string arg);
void setGrades(double a[4]);
void print();
void calAvg();

};

student.cpp

#include<iostream>
#include<string>
#include"student.h"
using namespace std;

int Student::num=0;
Student::Student()
{
this->fname="";
this->lname="";
this->ssn="";
for(int i=0;i<4;++i)
{
this->grades[i]=0;
}

}
//parameterized constructor
Student::Student(string fname,string lname,string ssn,double grades[4])
{

this->fname=fname;
this->lname=lname;
this->ssn=ssn;
for(int i=0;i<4;++i)
{
this->grades[i]=grades[i];
}

}
string Student::getFName()
{
return fname;
}
string Student::getLName()
{
return lname;
}
string Student::getSSN()
{
return ssn;
}
double * Student::getGrades()
{
return grades;
}

double Student::getAvg()
{
return avg;
}
void Student::setFName(string arg)
{
fname=arg;
}
void Student::setLName(string arg)
{
lname=arg;
}
void Student::setSSN(string arg)
{
ssn=arg;
}
void Student::setGrades(double a[4])
{
for(int i=0;i<4;++i)
{
grades[i]=a[i];
}
}

//to calulate the average of student
void Student::calAvg()
{
double sum=0;
for(int i=0;i<4;++i)
{
sum+=grades[i];
}
avg=sum/4;
}

void Student::print()
{
cout<<fname<<" "<<lname<<" "<<ssn;
for(int i=0;i<4;++i)
cout<<" "<<grades[i];
cout<<endl;
}

ClassRoom.h

#include "student.cpp"
class ClassRoom
{
public:
string name;
int count;
Student s[24];
void read();
void print();
void avg();
void sortbyAvg();
void sortbylastname();
};

ClassRoom.cpp

#include<iostream>
#include<string>
#include<fstream>
#include"ClassRoom.h"
using namespace std;


void ClassRoom::read()
{
ifstream fin("student.txt");
int size=0;
string fname;
string lname;
string ssn;
double grades[4];
double avg;
int i=0;

while(fin>>fname)
{
fin>>lname;
fin>>ssn;
fin>>grades[0];
fin>>grades[1];
fin>>grades[2];
fin>>grades[3];
s[i].setFName(fname);
s[i].setLName(lname);
s[i].setSSN(ssn);
s[i].setGrades(grades);
s[i].calAvg();
i++;
Student::num++;
}
count=Student::num;
}
void ClassRoom::print()
{
for(int i=0;i<count;++i)
{
cout<<"STUDENT "<<i+1<<" DATA: \n";
s[i].print();
}
}
void ClassRoom::avg()
{
double sum=0;
for(int i=0;i<count;++i)
{
sum+=s[i].getAvg();
}
cout<<"AVERAGE IS: "<<sum/count<<endl;
}
void ClassRoom::sortbyAvg()
{
for(int i=0;i<count;++i)
{
for(int j=0;j<count-i-1;++j)
if (s[j].getAvg() > s[j+1].getAvg())
{
Student temp = s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
void ClassRoom::sortbylastname()
{
for(int i=0;i<count;++i)
{
for(int j=0;j<count-i-1;++j)
if (s[j].getLName().compare(s[j+1].getLName())<0)
{
Student temp = s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}

Main.cpp

#include"ClassRoom.cpp"
using namespace std;

int main()
{
//create class room object
ClassRoom C;
C.read();
cout<<"SORTED BY LAST NAME\n";
C.sortbylastname();
C.print();
return 0;
}

OUTPUT :

PFA

Related Questions