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

c++// Write a full class definition for a class named Averager, and containing t

ID: 3808627 • Letter: C

Question

c++//

Write a full class definition for a class  named  Averager, and containing the following members:

An data member named  sum  of type  integer .

An data member named  count of type  integer .

A constructor with no parameters . The constructor  initializes the data members sum  and the data member count to 0.

A function named  getSum that accepts no parameters and returns an integer . getSum returns the value of sum .

A function named  add that accepts an integer  parameter and returns no value . add increases the value of sum  by the value of theparameter , and increments the value of count by one.

A function named  getCount that accepts no parameters and returns an integer . getCount returns the value of the count data member, that is, the number of values added to sum .

A function named  getAverage that accepts no parameters and returns a double . getAverage returns the average of the values added tosum . The value returned should be a value of type  double (and therefore you must cast the data members to double prior to performing the division).

Explanation / Answer

As I'm using Codeblocks 13.12, here count is a keyword so, instead of count I've used count1 to do the task.

This question demands for Class definition but I'm also providing a main() function to work with the Class objects to build a menu driven program.

#include<iostream>
using namespace std;

class Averager{
    int sum, count1; //Declaring integers.
public:
    Averager(){//Constructor to initialize data members.
        sum = count1 = 0;
    }
    int getSum(){//Returns sum.
        return sum;
    }
    void add(int num){//Adds num to sum.
        sum += num;
        count1++;
    }
    int getCount(){//Returns count1.
        return count1;
    }
    double getAverage(){//Returns average.
        double avg = (double)sum / (double)count1;
        return avg;
    }
};

int main()
{
    Averager a; //Declaring object.
    int ch;
    //Creating menu driven program.
    do{
        cout<<" 1. Add a number.";
        cout<<" 2. Display Sum.";
        cout<<" 3. Display Count.";
        cout<<" 4. Display Average.";
        cout<<" 5. Exit.";
        cout<<" Enter your choice: ";
        cin>>ch;
        switch(ch){
        case 1:
            int num;
            cout<<" Enter the number: ";
            cin>>num;
            a.add(num);//Calling add function.
            break;
        case 2:
            cout<<" Sum: "<<a.getSum()<<endl;//Calling getSum function.
            break;
        case 3:
            cout<<" Count: "<<a.getCount()<<endl;//Calling getCount function.
            break;
        case 4:
            cout<<" Average: "<<a.getAverage()<<endl;//Calling getAverage function.
            break;
        case 5://To exit program.
            cout<<" Exiting... ";
            return 0;
            break;
        default://For wrong choice.
            cout<<" Wrong Choice. Try Again. ";
            break;
        }
    }while(1);//For infinite loop.
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote