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

c++ problem, need .h and .cpp and main.cpp; here is start code: ****************

ID: 3753289 • Letter: C

Question

c++ problem, need .h and .cpp and main.cpp;

here is start code:

************************************************************************************

student.h

#ifndef STUDENT_H

#define STUDENT_H

/**

* Definition of the Student class.

* Provided as part of the Composition of Objects lab.

*/

#include <iostream>

#include <string>

#include <vector>

class Student {

public:

Student( ) = default;

Student( std::string name, int id );

~Student( );

std::string get_name( ) const;

int get_id( ) const;

double get_exam_score( int exam_number ) const;

double get_average( ) const;

double get_exam_count( ) const;

char get_letter_grade( ) const;

void set_name( std::string name );

void set_id( int id );

void set_exam_score( int exam_number, double score );

void add_exam_score( double score );

std::ostream& write( std::ostream& strm = std::cout ) const;

private:

std::string _name;

int _id{0};

std::vector<double> _exams;

};

#endif

*************************************************************************************************

student.cpp

/**

* Implementation for the Student class,

* provided as part of the Composition of Objects lab.

*/

#include "Student.h"

#include <iomanip>

/**

* Construct a student given the name and ID number

*

* @param name student name

* @param id student ID number

*/

Student::Student( std::string name, int id ) : _name{name}, _id{id} {}

/**

* Provides output on stderr when student leaves scope for

* debugging purposes only.

*/

Student::~Student( ) {

std::cerr << "Student " << _name << " (" << _id << ") leaving scope."

<< std::endl;

}

/**

* Accessor for student's name.

*

* @return student's name

*/

std::string Student::get_name( ) const {

return _name;

}

/**

* Accessor for student ID.

*

* @return student's ID

*/

int Student::get_id( ) const {

return _id;

}

/**

* Accessor for a particular exam score, given the exam number,

* which starts counting from 1.

*

* @param exam_number Exam number (Natural number, starts at 1.)

* @return the score of exam number `exam_number`

*/

double Student::get_exam_score( int exam_number ) const {

return _exams.at( exam_number - 1 );

}

/**

* Mutator allowing the student name to be changed.

*

* @param name new name for the student

*/

void Student::set_name( std::string name ) {

_name = name;

}

/**

* Mutator allowing the student ID to be changed.

*

* @param id new ID number for the student

*/

void Student::set_id( int id ) {

_id = id;

}

/**

* Mutator allowing a selected exam score to be updated, given

* the exam number (starting from 1) and the new score.

*

* @param exam_number Exam number (Natural number, starts at 1.)

* @param score the updated score to store

*/

void Student::set_exam_score( int exam_number, double score ) {

_exams.at( exam_number - 1 ) = score;

}

/**

* Adds a new exam score for the student, as the last of their

* exams.

*

* @param score value of new score to add

*/

void Student::add_exam_score( double score ) {

_exams.push_back( score );

}

/**

* Accessor for the student's overall exam average.

*

* @remarks The exam average is assumed to be 100% if

* no exams have been entered yet.

*

* @return student's current exam average

*/

double Student::get_average( ) const {

auto sum{0.0};

for ( const auto& score : _exams ) { sum += score; }

return _exams.size( ) > 0 ? sum / _exams.size( ) : 100.0;

}

/**

* Accessor for the number of exams the student has

* taken.

*

* @return the number of exams stored for this student

*/

double Student::get_exam_count( ) const {

return _exams.size( );

}

/**

* Accessor for the student's current letter grade.

*

* @remarks Uses the scale:

* >= 90 : A

* [80, 90) : B

* [70, 80) : B

* [60, 70) : B

* < 60 : F

*

* @return [description]

*/

char Student::get_letter_grade( ) const {

auto grade = 'F';

auto avg = get_average( );

if ( avg >= 90.0 )

grade = 'A';

else if ( avg >= 80.0 )

grade = 'B';

else if ( avg >= 70.0 )

grade = 'C';

else if ( avg >= 60.0 )

grade = 'D';

return grade;

}

/**

* Writes a string describing the student (name, ID, average, and letter

* grade) to the stream specified by `strm`. The default is stdout.

*

* @param strm stream to write into (default = stdout)

* @return the modified stream `strm` is returned

*/

std::ostream& Student::write( std::ostream& strm ) const {

strm << _name << " (" << _id << ") : " << std::fixed

   << std::setprecision( 2 ) << get_average( ) << " : "

   << get_letter_grade( );

return strm;

}

*****************************************************************************************************************

Begin by adding the empty.txt files listed below to the project oop04in.txt oop04out.txt Create studentArrayv4 methods read (),write ), and add from) as follows: VO int ostream& void add const Student& new_student read( istream& infile write ( add from( istream& infile ostream& out file=cout ) const; The add() method shown here is an overloaded add (). It will take a student object [by const reference) and add it to the array, using the same rules as the zero-parameter add () That is, if there is room left in the physical array, the new student is added at the end. If the physical array is full, its size should be increased by 3 (by allocating a new array and copying over the existing pointers) and then the new student should be added at the end of the logical array. (Refer back to the material from the in-lab if needed.) When you finish this version of add. you might be able to re-factor the version of add) you wrote in lab to make use of it so that there is no repeated code. The read () method invokes method add from), creating new Student objects one at a time, until the end of the input stream is reached; the number of Students that were successfully read should be returned. The add from() method will accept the input file as a parameter, and is responsible for extracting all available data for the student, creating that student, and adding the student to the StudentArrayv4 collection using the overloaded add() method described above. This method should add a student only if there is enough valid information in the stream to constitute a student record. For our purposes, if the stream contains at least a valid name and id number, the record is valid [see the input file specification below for more specifics). The write () method should replace the write() method you created in lab. It will direct the printing of each Student object's data to the indicated stream (the format is shown later in this text). Write the test data file oop04in.txt and make sure the methods work correctly with it. Example contents might include the following. Able: Baker 777 71 73 75 Charles: 888 81 83 85 Dunlap: 999 91 93 95 666 61 63 65 You do not know in advance how many students may be in the input file, or how many exam scores each student will have (and not all students need have the same number of exams). The format does guarantee the following: Each line represents one student. The student name is first, followed by a colon. Name is required. e The student ID number is next, possibly preceded by some amount of whitespace, which must be ignored. Student ID is required. Following the student ID will be zero or more exam scores, separated by some amount of whitespace. Scores are optional. Function main() should prompt the user for the names of files to be read and written. Add code to main() to open the input and output files, and perform testing for the new functionality you are adding.

Explanation / Answer

// student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
#include <string>
#include <vector>
using namespace std;

// Class Student definition
class Student
{
public:
// Prototype of member function
Student();
Student(string name, int id );
~Student( );
string get_name( ) const;
int get_id( ) const;
double get_exam_score( int exam_number ) const;
double get_average( ) const;
double get_exam_count( ) const;
char get_letter_grade( ) const;
void set_name(string name );
void set_id(int id );
void set_exam_score( int exam_number, double score );
void add_exam_score( double score );
ostream& write(ostream& strm) const;
private:
// Data member to store data
string name;
int id;
vector<double> exams;
};// End of class
#endif

--------------------------------------------------------------------------------------------------------------

// student.cpp

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

/**
* Default construct to initialize student data member
*/
Student::Student()
{
name = "";
id = 0;
}// End of default constructor

/**
* Construct a student given the name and ID number
*
* @param na student name
* @param i student ID number
*/
Student::Student(string na, int i)
{
name = na;
id = i;
}// End of parameterized constructor

/**
* Provides output on stderr when student leaves scope for
* debugging purposes only.
*/
Student::~Student( )
{
cerr << "Student " << name << " (" << id << ") leaving scope."<< std::endl;
}// End of destructor

/**
* Accessor for student's name.
*
* @return student's name
*/
string Student::get_name( ) const
{
return name;
}// End of function

/**
* Accessor for student ID.
*
* @return student's ID
*/
int Student::get_id( ) const
{
return id;
}// End of function

/**
* Accessor for a particular exam score, given the exam number,
* which starts counting from 1.
*
* @param exam_number Exam number (Natural number, starts at 1.)
* @return the score of exam number `exam_number`
*/
double Student::get_exam_score(int exam_number) const
{
return exams.at(exam_number - 1);
}// End of function

/**
* Mutator allowing the student name to be changed.
*
* @param na new name for the student
*/
void Student::set_name(string na)
{
name = na;
}// End of function

/**
* Mutator allowing the student ID to be changed.
*
* @param i new ID number for the student
*/
void Student::set_id(int i)
{
id = i;
}// End of function

/**
* Mutator allowing a selected exam score to be updated, given
* the exam number (starting from 1) and the new score.
*
* @param exam_number Exam number (Natural number, starts at 1.)
* @param score the updated score to store
*/
void Student::set_exam_score(int exam_number, double score)
{
exams.at(exam_number - 1) = score;
//exams.push_back(score);
}// End of function

/**
* Adds a new exam score for the student, as the last of their
* exams.
*
* @param score value of new score to add
*/
void Student::add_exam_score( double score )
{
exams.push_back( score );
}// End of function

/**
* Accessor for the student's overall exam average.
*
* @remarks The exam average is assumed to be 100% if
* no exams have been entered yet.
*
* @return student's current exam average
*/
double Student::get_average() const
{
double sum = 0.0;
for (int x = 0; x < exams.size(); x++)
sum += exams.at(x);

return exams.size() > 0 ? sum / exams.size( ) : 100.0;
}// End of function

/**
* Accessor for the number of exams the student has
* taken.
*
* @return the number of exams stored for this student
*/
double Student::get_exam_count( ) const
{
return exams.size( );
}// End of function

/**
* Accessor for the student's current letter grade.
*
* @remarks Uses the scale:
* >= 90 : A
* [80, 90) : B
* [70, 80) : B
* [60, 70) : B
* < 60 : F
*
* @return [description]
*/
char Student::get_letter_grade( ) const
{
char grade = 'F';
double avg = get_average( );
if ( avg >= 90.0 )
grade = 'A';
else if ( avg >= 80.0 )
grade = 'B';
else if ( avg >= 70.0 )
grade = 'C';
else if ( avg >= 60.0 )
grade = 'D';
return grade;
}// End of function

/**
* Writes a string describing the student (name, ID, average, and letter
* grade) to the stream specified by `strm`. The default is stdout.
*
* @param strm stream to write into (default = stdout)
* @return the modified stream `strm` is returned
*/
std::ostream& Student::write( std::ostream& strm ) const
{
strm << name << " (" << id << ") : " << std::fixed
<< std::setprecision( 2 ) << get_average( ) << " : "
<< get_letter_grade();

return strm;
}// End of function

-------------------------------------------------------------------------------------------------

// StudentArrayV4.cpp
#include "student.cpp"
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;

// Class StudentArrayV4 definition
class StudentArrayV4
{
// Dara member to store data
Student *student;
int current;
int capacity;
public:
// Prototype of member functions
StudentArrayV4();
void add(Student &new_student);
int read(ifstream &infile);
ofstream & write(ofstream &outfile) const;
void add_from(ifstream & infile);
void add();
void display();
};// End of class

/**
* Default constructor to allocate memory to student pointer
* Initializes the current index counter to 0
* Initializes the capacity to 3
*/
StudentArrayV4::StudentArrayV4()
{
student = new Student[3];
current = 0;
capacity = 3;
}// End of default constructor

/**
* Function to increase the capacity of he pointer by 3
*/
void StudentArrayV4::add()
{
// Loop variable
int x;
// Dynamically allocate memory of existing student capacity
Student *temp = new Student[capacity];
cout<<" Current size: "<<capacity;

// Loops till number of records
for(x = 0; x < capacity; x++)
// Assigns each student record to temp
temp[x] = student[x];

// Increase the capacity by 3
capacity += 3;
// Dynamically allocate memory of current capacity
student = new Student[x+3];
cout<<" Increased size: "<<capacity<<endl;
// Loops till number of records
for(int y = 0; y < x; y++)
// Assigns each student record from temp
student[y] = temp[y];
}// End of function

/**
* Function to add a new student record
* @param &new_student reference type student object
*/
void StudentArrayV4::add(Student &new_student)
{
// Checks if the current record counter is equals to capacity
if(current == capacity)
{
// Call the function to increase the size
add();
// Adds the new student object at current index position
student[current] = new_student;
// Increase the current index counter by one
current++;
}// End of if condition
// Otherwise free size available
else
{
// Adds the new student object at current index position
student[current] = new_student;
// Increase the counter by one
current++;
}// End of else
}// End of function

/**
* Function to read the student record from file
* @param &infile ifstream object
* @return number of records read from file
*/
int StudentArrayV4::read(ifstream &infile)
{
// Checks if the file unable to open for reading display's error message with file name
if(!infile)
{
cout<<" ERROR: Unable to open the file for reading.";
exit(0);
}// End of if condition

// Loops till end of the file
while(!infile.eof())
{
// Calls the function to read the record from file
add_from(infile);
}// End of while loop
// Closes the file
infile.close();
// Returns the record counter
return current;
}// End of function

/**
* Writes a string describing the student (name, ID, mark1, mark2, mark3, average and letter
* grade) to the stream specified by `outfile`. The default is stdout.
*
* @param outfile stream to write into (default = stdout)
* @return the modified stream `outfile` is returned
*/
ofstream & StudentArrayV4::write(ofstream &outfile) const
{
// Checks if the file unable to open for reading display's error message with file name
if(!outfile)
{
cout<<" ERROR: Unable to open the file for writing.";
exit(0);
}// End of if condition

// Writes heading to file
outfile<<" Scores ";
outfile<<" Name ID# 1 2 3 Avg Grade ";

// Loops till number of records
for(int x = 0; x < current; x++)
// Writes each record data to file
outfile<<student[x].get_name()<<" "<<student[x].get_id()<<" "<<student[x].get_exam_score(1)
<<" "<<student[x].get_exam_score(2)<<" "<<student[x].get_exam_score(3)
<<" "<<student[x].get_average()<<" "<<student[x].get_letter_grade()<<endl;
return outfile;
}// End of function

/**
* Function to read the student record from file
* @param &infile ifstream object
*/
void StudentArrayV4::add_from(ifstream &infile)
{
// To store the data read from file
string na;
int id;
double mark;
// Declares a student object
Student st;
// Reads the name from file
infile>>na;
// Calls the function to set the name
st.set_name(na);

// Reads the id from file
infile>>id;
// Calls the function to set the id
st.set_id(id);

// Reads the first subject mark from file
infile>>mark;
// Calls the function to set the first subject mark
st.add_exam_score(mark);

// Reads the second subject mark from file
infile>>mark;
// Calls the function to set the second subject mark
st.add_exam_score(mark);

// Reads the third subject mark from file
infile>>mark;
// Calls the function to set the third subject mark
st.add_exam_score(mark);

// Calls the function to add the student object to array
add(st);
}// End of function


/**
* Function to display all student information
*/
void StudentArrayV4::display()
{
// Displays the heading
cout<<" Scores ";
cout<<" Name ID# 1 2 3 Avg Grade ";

// Loops till number of records
for(int x = 0; x < current; x++)
// Displays each record information
cout<<student[x].get_name()<<" "<<student[x].get_id()<<" "<<fixed<<setprecision(2)<<student[x].get_exam_score(1)
<<" "<<student[x].get_exam_score(2)<<" "<<student[x].get_exam_score(3)
<<" "<<student[x].get_average()<<" "<<student[x].get_letter_grade()<<endl;
}// End of function

// main function definition
int main()
{
// Declares ifstream and ofstream object
ifstream infile;
ofstream outfile;

// Declares an object of class StudentArrayV4
StudentArrayV4 stu;

// Opens the file for reading
infile.open("oop04in.txt");

// Calls the function to read file contents
stu.read(infile);

// Calls the function to displays student information
stu.display();

// Opens the file for writing
outfile.open("oop04out.txt");

// Calls the function to write the student information to file
stu.write(outfile);
}// End of main function

Sample Output:

Student Able: (666) leaving scope.
Student Baker: (777) leaving scope.
Student Charle: (888) leaving scope.

Current size: 3
Increased size: 6
Student Dunlap: (999) leaving scope.
Student Anil: (111) leaving scope.
Scores

Name ID# 1 2 3 Avg Grade
Able: 666 61.00 63.00 65.00 63.00 D
Baker: 777 71.00 73.00 75.00 73.00 C
Charle: 888 81.00 83.00 85.00 83.00 B
Dunlap: 999 91.00 93.00 95.00 93.00 A
Anil: 111 11.00 13.00 15.00 13.00 F

oop04in.txt file contents

Able: 666 61 63 65
Baker: 777 71 73 75
Charle: 888 81 83 85
Dunlap: 999 91 93 95
Anil: 111 11 13 15

oop04out.txt file contents

Scores

Name ID# 1 2 3 Avg Grade

Able: 666 61 63 65 63 D

Baker: 777 71 73 75 73 C

Charle: 888 81 83 85 83 B

Dunlap: 999 91 93 95 93 A

Anil: 111 11 13 15 13 F

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