Question

#ifndef STUDENTTESTSCORES_H #define STUDENTTESTSCORES_H #include <string> using namespace std; const double DEFAULT_SCORE = 0.0; class StudentTestScores...

#ifndef STUDENTTESTSCORES_H
#define STUDENTTESTSCORES_H
#include <string>
using namespace std;

const double DEFAULT_SCORE = 0.0;

class StudentTestScores
{
private:
   string studentName;  // The student's name
   double *testScores;  // Points to array of test scores
   int numTestScores;   // Number of test scores

    // Private member function to create an
    // array of test scores.
   void createTestScoresArray(int size)
   { numTestScores = size;
     testScores = new double[size]; 
     for (int i = 0; i < size; i++)
        testScores[i] = DEFAULT_SCORE; }
        
public:
    // Constructor
   StudentTestScores(string name, int numScores)
   { studentName = name;
     createTestScoresArray(numScores); }

    // Destructor
   ~StudentTestScores()
   { delete [] testScores; }

    // The setTestScore function sets a specific
    // test score's value.
   void setTestScore(double score, int index)
   { testScores[index] = score; }

    // Set the student's name.
   void setStudentName(string name)
   { studentName = name; }

    // Get the student's name.
   string getStudentName() const
   { return studentName; }
   
   // Get the number of test scores.
   int getNumTestScores()
   { return numTestScores; }

    // Get a specific test score.
   double getTestScore(int index) const
   { return testScores[index]; }
};
#endif

Download the header file “StudentTestScores.h” - Carefully read the class declaration and understand how its inline functions work. -

Design a program that uses the class and allows the user to enter and read multiple students’ names and grades.

Add the following functions to the class:

1. A copy constructor. Test your program for how the constructor will be used.

For example: The statement StudentTestScores stu2 = stu1;

Will call the copy constructor that will initialize stu2 with all the member data in stu1 including values in the testScores member array of stu1.

2. Overload the assignment operator so that the statement stu3 = stu4; copies all the values from stu4 into stu3 (you may need to resize testScores array)

3. A member function that returns the average test score of a student.

Sample call: cout<<stu5.avg()<<endl;

4. A member function that displays the minimum test score along with the test number. Sample output: Test 2 has the minimum score; 73.

5. Overload the “>” operator. For example: stu6 > stu7 compares the average test scores for both students and returns true if stu6 average is greater than stu7 average, false otherwise

Notes:

§ DO NOT make any changes in StudentTestScores class other than adding the listed functions

. § DO NOT add a default constructor

§ You do not have to use vectors

§ Design the output in any way you want so that you fully demonstrate the class, all its member functions, and the copy constructor for multiple of students

§ Hint: check how to define an array of pointers.

Sample Output (as a guide)

Enter number of students: 3

Enter name of student 1: John Doe

Enter number of test scores for John Doe: 4

Test Score 1: 70

Test Score 2: 90

Test Score 3: 80

Test Score 4: 98

Enter name of student 2: Allan Smith

Enter number of test scores for Allan Smith: 3

Test Score 1: 90

Test Score 2: 83

Test Score 3: 72

Enter name of student 3: Tom Allen

Enter number of test scores for Tom Allen: 5

Test Score 1: 81

Test Score 2: 74

Test Score 3: 61

Test Score 4: 90

Test Score 5: 56

The above is not the full output. You should demonstrate the operations of other member functions and the copy constructor in the class. You may define individual objects for testing the operations.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:

StudentTestScores.h

#ifndef STUDENTTESTSCORES_H
#define STUDENTTESTSCORES_H
#include <string>
#include <iostream>
using namespace std;

const double DEFAULT_SCORE = 0.0;

class StudentTestScores
{
private:
   string studentName; // The student's name
   double *testScores; // Points to array of test scores
   int numTestScores; // Number of test scores

   // Private member function to create an
   // array of test scores.
   void createTestScoresArray(int size)
   { numTestScores = size;
   testScores = new double[size];
   for (int i = 0; i < size; i++)
       testScores[i] = DEFAULT_SCORE; }

public:
   // Constructor
   StudentTestScores(string name, int numScores)
   { studentName = name;
   createTestScoresArray(numScores); }


   StudentTestScores()
       { }


   // Destructor
   ~StudentTestScores()
   { delete [] testScores; }

   void getUserInput(){
       for(int j=0;j<numTestScores;j++){
           cout<<"Test Score "<<j+1<<" : ";
           cin>>testScores[j];
       }
   }

   /* Copy constructor */
   StudentTestScores (const StudentTestScores &sam){
       studentName = sam.studentName;
       numTestScores = sam.numTestScores;
       testScores = new double[numTestScores];
       for (int i = 0; i < sam.numTestScores; i++)
           testScores[i] = sam.testScores[i];
   }

   StudentTestScores & operator = (const StudentTestScores &sam)
   {
       studentName = sam.studentName;
       numTestScores = sam.numTestScores;
       testScores = new double[numTestScores];
       for (int i = 0; i < sam.numTestScores; i++)
           testScores[i] = sam.testScores[i];
       return *this;
   }


   double avg(){
       double total = 0;
       for (int i = 0; i < numTestScores; i++)
           total += testScores[i];
       return total*1.0/numTestScores;
   }

   void displayMinReport(){
       double min = 99999;
       int index ;
       for (int i = 0; i < numTestScores; i++){
           if(min>testScores[i]){
               min=testScores[i];
               index = i;
           }
       }
       cout<<"Test "<<index+1<<" has the minimum score;"<<min<< ".";
   }

   bool operator > (StudentTestScores &sam){
       return this->avg()>sam.avg();
   }
   // The setTestScore function sets a specific
   // test score's value.
   void setTestScore(double score, int index)
   { testScores[index] = score; }

   // Set the student's name.
   void setStudentName(string name)
   { studentName = name; }

   // Get the student's name.
   string getStudentName() const
   { return studentName; }

   // Get the number of test scores.
   int getNumTestScores()
   { return numTestScores; }

   // Get a specific test score.
   double getTestScore(int index) const
   { return testScores[index]; }
};
#endif

main.cpp

#include "StudentTestScores.h"

using namespace std;
int main()
{
   int num;
   cout<<"Enter number of students: ";
   cin>>num;
   StudentTestScores students[num];
   string studentName;
   int numTestScores;
   for(int i=0;i<num;i++){
       cout<<"Enter name of student "<<i+1<<" : ";
       getline(cin,studentName);
       while(studentName=="" || studentName=="\n"){
               getline(cin,studentName);
       }
       cout<<"Enter number of test scores for "<<studentName<<" : ";
       cin>>numTestScores;
       StudentTestScores s(studentName,numTestScores);
       students[i] = s;
       students[i].getUserInput();
   }


   for(int i=0;i<num;i++){
       cout<<"Result for "<<students[i].getStudentName()<<endl;
       cout<<"Average : "<<students[i].avg()<<endl;
       students[i].displayMinReport();
       cout<<"***************"<<endl;
   }
   return 0;
}

WORKSPACE\c_language_workspace CPP\student\main.cpp - [Executing] - Dev-C++ 5.11 Edit Search View Project Execute Tools Style

Add a comment
Know the answer?
Add Answer to:
#ifndef STUDENTTESTSCORES_H #define STUDENTTESTSCORES_H #include <string> using namespace std; const double DEFAULT_SCORE = 0.0; class StudentTestScores...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: //...

    #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: // default constructor procReqRec() {} // constructor procReqRec(const string& nm, int p); // access functions int getPriority(); string getName(); // update functions void setPriority(int p); void setName(const string& nm); // for maintenance of a minimum priority queue friend bool operator< (const procReqRec& left, const procReqRec& right); // output a process request record in the format // name: priority friend ostream& operator<< (ostream& ostr, const procReqRec&...

  • In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double...

    In C++ ***//Cat.h//*** #ifndef __Cat_h__ #define __Cat_h__ #include <string> using namespace std; struct Cat { double length; double height; double tailLength; string eyeColour; string furClassification; //long, medium, short, none string furColours[5]; }; void initCat (Cat&, double, double, double, string, string, const string[]); void readCat (Cat&); void printCat (const Cat&); bool isCalico (const Cat&); bool isTaller (const Cat&, const Cat&); #endif ***//Cat.cpp//*** #include "Cat.h" #include <iostream> using namespace std; void initCat (Cat& cat, double l, double h, double tL, string eC,...

  • A hard c++ problem ◎Write a c++ program”Student.h”include Private data member, string firstName; string lastName; double...

    A hard c++ problem ◎Write a c++ program”Student.h”include Private data member, string firstName; string lastName; double GPA(=(4.0*NumberOfAs+3.0*NumberOfBs+2.0*NumberOfCs+1.0*NumberOfDs)/( As+Bs+Cs+Ds+Fs)); Public data member, void setFirstName(string name); string getFirstName() const; void printFirstName() const; void getLastName(string name); string getLastName() const; void printLastName() const; void computeGPA(int NumberOfAs,int NumberOfBs,int NumberOfCs,int NumberOfDs,int NumberOfFs); double getGPA() const; double printGPA() const; A destructor and an explicit default constructor initializing GPA=0.0 and checking if GPA>=0.0 by try{}catch{}. Add member function, bool operator<(const Student); The comparison in this function based on...

  • Here is the code from the previous three steps: #include <iostream> using namespace std; class Student...

    Here is the code from the previous three steps: #include <iostream> using namespace std; class Student { private: //class variables int ID; string firstName,lastName; public: Student(int ID,string firstName,string lastName) //constructor { this->ID=ID; this->firstName=firstName; this->lastName=lastName; } int getID() //getter method { return ID; } virtual string getType() = 0; //pure virtual function virtual void printInfo() //virtual function to print basic details of a student { cout << "Student type: " << getType() << endl; cout << "Student ID: " << ID...

  • #ifndef HEAP_H_ #define HEAP_H_ namespace cse { template<typename dtype> class heap { public: /** TO DO::...

    #ifndef HEAP_H_ #define HEAP_H_ namespace cse { template<typename dtype> class heap { public: /** TO DO:: default constructor perform new to reserve memory of size INITIAL_CAPACITY. set num_items = 0; */ heap() { // write your code here }; /** Create a heap from a given array */ heap(dtype *other, size_t len) { // write your code here }; /** copy constructor copy another heap to this heap. */ heap(const heap<dtype> &other) { //write your code here }; /** Accessor...

  • Answer this in c++ #include <iostream> #include <fstream> #include <string> using namespace std; class Person {...

    Answer this in c++ #include <iostream> #include <fstream> #include <string> using namespace std; class Person { public: Person() { setData("unknown-first", "unknown-last"); } Person(string first, string last) { setData(first, last); } void setData(string first, string last) { firstName = first; lastName = last; } void printData() const { cout << "\nName: " << firstName << " " << lastName << endl; } private: string firstName; string lastName; }; class Musician : public Person { public: Musician() { // TODO: set this...

  • Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string;...

    Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string; class Account { public: Account(); Account(string, double); void deposit(double); bool withdraw(double); string getName() const; double getBalance() const; private: string name; double balance; }; #endif ////////////////////////////////////////////// //AccountManager.hpp #ifndef _ACCOUNT_MANAGER_HPP_ #define _ACCOUNT_MANAGER_HPP_ #include "Account.hpp" #include <string> using std::string; class AccountManager { public: AccountManager(); AccountManager(const AccountManager&); //copy constructor void open(string); void close(string); void depositByName(string,double); bool withdrawByName(string,double); double getBalanceByName(string); Account getAccountByName(string); void openSuperVipAccount(Account&); void closeSuperVipAccount(); bool getBalanceOfSuperVipAccount(double&) const;...

  • Write a cpp program Server.h #ifndef SERVER_H #define SERVER_H #include #include #include #include using namespace std;...

    Write a cpp program Server.h #ifndef SERVER_H #define SERVER_H #include #include #include #include using namespace std; class Server { public:    Server();    Server(string, int);    ~Server();    string getPiece(int); private:    string *ascii;    mutex access; }; #endif -------------------------------------------------------------------------------------------------------------------------- Server.cpp #include "Server.h" #include #include Server::Server(){} Server::Server(string filename, int threads) {    vector loaded;    ascii = new string[threads];    ifstream in;    string line;    in.open(filename);    if (!in.is_open())    {        cout << "Could not open file...

  • please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName();...

    please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName(); double getNumberExams(); double getScoresAndCalculateTotal(double E); double calculateAverage(double n, double t); char determineLetterGrade(); void displayAverageGrade(); int main() { string StudentName; double NumberExam, Average, ScoresAndCalculateTotal; char LetterGrade; StudentName = getStudentName(); NumberExam = getNumberExams(); ScoresAndCalculateTotal= getScoresAndCalculateTotal(NumberExam); Average = calculateAverage(NumberExam, ScoresAndCalculateTotal); return 0; } string getStudentName() { string StudentName; cout << "\n\nEnter Student Name:"; getline(cin, StudentName); return StudentName; } double getNumberExams() { double NumberExam; cout << "\n\n Enter...

  • / Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false,...

    / Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false, bool predator=false); std::string getName() const; bool isDomestic() const; bool isPredator() const; void setName(std::string); void setDomestic(); void setPredator(); protected: // protected so that derived class can directly access them std::string name_; bool domestic_; bool predator_; }; #endif /* ANIMAL_H_ */ //end of Animal.h // /////////////////////////////////////////////////////////////////Animal.cpp #include "Animal.h" Animal::Animal(): name_(""),domestic_(false), predator_(false){ } Animal::Animal(std::string name, bool domestic, bool predator): name_(name),domestic_(domestic), predator_(predator) { } std::string Animal::getName() const{ return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT