Design a TestScores class that has member variables to hold three test scores.

QUESTION

Design a TestScores class that has member variables to hold three test scores. The class should have a constructor, accessor, and mutator functions for the test score fields, and a member function that returns the average of the test scores. Demonstrate the class by creating an instance of the class, the program should ask the user to enter three test scores, which are stored in the TestScores object. Then the program should display the average of the scores, as reported by the TestScores object.

 

SUMMARY

A certain scenario of test scores is implemented. A student’s scores of three tests is taken and stored in a class object named TestScores. Each member of this class has a mutator function and an accessor. A function called avgScore function calculates the average score of the three scores of the tests and prints it as the output to the console.

 

EXPLANATION

A class named TestScores has score1, score2, and score3 of a student as its private members. A constructor is also defined. For every member, an accessor and mutator are defined.

The function getScore1 is an accessor for the score 1 and the setScore1 method is a mutator for the score 1.

Such setter and getter functions are also defined for score 2 and score 3.

A method named avgScore is used to calculate the average score of the three scores of the student. An object of TestScores class is declared in the main function.

The getdata method is called in the main and gets the three scores from the user.

Finally, the avgScore function is called and the average score is printed as output.

 

CODE

#include <iostream>
using namespace std;
class TestScores 
{
    private:
    /* the 3 scores */
    int sc1,sc2,sc3;
    public:
    /* default constructor */
    TestScores(){}
    /* parametrized constructor */
    TestScores(int s1,int s2,int s3){
        sc1=s1; sc2=s2; sc3=s3;
    }
    /* for score 1 */
    int getScore1(){
        return sc1;
    }
    /* for score 2 */
    int getScore2(){
        return sc2;
    }
    /* for score 3 */
    int getScore3(){
        return sc3;
    }
    /* mutator function for score 1 */
    void setScore1(int score){
        sc1=s;
    }
    /* mutator function for score 2 */
    void setScore2(int score){
        sc2=s;
    }
    /* mutator function for score 3 */
    void setScore3(int score){
        sc3=s;
    }
    /* calculating the average of the three scores */
    float avgScore(){
        return (sc1+sc2+sc3)/3.0;
    }
    void getdata(){
        cout<<"Enter score-1: "; cin>>sc1;
        cout<<"Enter score-2: "; cin>>sc2;
        cout<<"Enter score-3: "; cin>>sc3;
    }
};
int main()
{
    TestScores te;
    te.getdata();
    cout<<"The average of three scores: "<<te.avgScore()<<endl;
}

 

OUTPUT

 

 

Also Read: Class c is a child class of B which is class of A. A, B, and C all have destructor and constructor functions.

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *