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

I need to add the following statements to this program. How would I go about doi

ID: 662580 • Letter: I

Question

I need to add the following statements to this program. How would I go about doing it?

- An overloaded (<<) output method using the "friend" attribute.

- An overloaded (+) as a function outside the class.

- An overloaded (==) and (!=) as methods.

- Add a 'erase' function member that resets a Statistician object.

Below is the program:

#include "stdafx.h"
#include <iostream>

using namespace std;

class statistician
{
private:
   int value;
   double largest, smallest, sum;
public:
   statistician()
   {
       sum = 0;
       value = 0;
   }
   void add(double num)
   {
       if(value == 0)
       {
           largest = num;
           smallest = num;
       }
       else
       {
           if(largest < num) largest = num;
           if(smallest > num) smallest = num;
       }
       sum += num;
       value++;
   }
   int getlength()
   {
       return value;
   }
   double getsum()
   {
       return sum;
   }
   double getaverage()
   {
       return sum / value;
   }
   double getlargest()
   {
       return largest;
   }
   double getsmallest()
   {
       return smallest;
   }
   void initstat()
   {
       sum = 0;
       value = 0;
   }
   void erase()
   {
    value = 0;
    sum = 0;
    smallest = 100;
    largest = 0;
   }
};
int main(){
   statistician s1;

   s1.add(5.5);
   s1.add(6.6);
   s1.add(8.8);
   s1.add(-3.4);
   s1.add(-0.5);
   s1.add(4.7);
   s1.add(9.1);

   cout << "Stat 1 Results: ";
   cout << " Sum = " << s1.getsum() << " Length = " << s1.getlength() << " Average = " << s1.getaverage() << " " << endl;

   s1.add(5.2);
   s1.add(-3.3);
   s1.add(-8.5);
   s1.add(3.2);
   s1.add(5.5);

   //Print out s1 using the << operator.

   //Erase here before preparing copy constructor.

      statistician s2(s1);

   //Print out s1 and s2 using the << operator.

   s2.add(12);
   s2.add(21);
   s2.add(71);
   s2.add(47);
   s2.add(12);
   s2.add(25);
   s2.add(24);
   s2.add(23);
   s2.add(24);
   s2.add(32);

   cout << "Stat 2 Results: ";
   cout << " Sum = " << s2.getsum() << " Length = " << s2.getlength() << " Average = " << s2.getaverage() << " " << endl;

   s2.add(5.2);
   s2.add(-3.3);
   s2.add(-8.5);
   s2.add(3.2);
   s2.add(5.5);

system("pause");
}

Explanation / Answer

The erase function is already defined.

The code after adding the specified statements is as follows:

//#include "stdafx.h"
#include <iostream>

using namespace std;

class statistician
{
private:
    int value;
    double largest, smallest, sum;
public:
    statistician()
    {
        sum = 0;
        value = 0;
    }
    void add(double num)
    {
        if(value == 0)
        {
            largest = num;
            smallest = num;
        }
        else
        {
            if(largest < num) largest = num;
            if(smallest > num) smallest = num;
        }
        sum += num;
        value++;
    }
    int getlength()
    {
        return value;
    }
    double getsum()
    {
        return sum;
    }
    double getaverage()
    {
        return sum / value;
    }
    double getlargest()
    {
        return largest;
    }
    double getsmallest()
    {
        return smallest;
    }
    void initstat()
    {
        sum = 0;
        value = 0;
    }
    void erase()
    {
     value = 0;
     sum = 0;
     smallest = 100;
     largest = 0;
    }
     friend ostream &operator<<( ostream &output,
                                       const statistician &st )
      {
         output << "value : " << st.value << "sum : "<<st.sum;
         return output;           
      }
    friend bool operator== (statistician &cP1, statistician &cP2);
    friend bool operator!= (statistician &cP1, statistician &cP2);


};
bool operator== (statistician &cP1, statistician &cP2)
{
    return (cP1.value == cP2.value &&
            cP1.sum == cP2.sum);
}

bool operator!= (statistician &cP1, statistician &cP2)
{
    return !(cP1 == cP2);
}

int main(){
    statistician s1;

   s1.add(5.5);
    s1.add(6.6);
    s1.add(8.8);
    s1.add(-3.4);
    s1.add(-0.5);
    s1.add(4.7);
    s1.add(9.1);

   cout << "Stat 1 Results: ";
    cout << " Sum = " << s1.getsum() << " Length = " << s1.getlength() << " Average = " << s1.getaverage() << " " << endl;

   s1.add(5.2);
    s1.add(-3.3);
    s1.add(-8.5);
    s1.add(3.2);
    s1.add(5.5);

     
   //Print out s1 using the << operator.
    cout<<"printing using operator overloading "<<endl;
      cout<<s1<<endl;

   //Erase here before preparing copy constructor.

      statistician s2(s1);

   //Print out s1 and s2 using the << operator.
      cout<<"printing s1"<<endl;
   cout<<s1<<endl;
     
     
   s2.add(12);
    s2.add(21);
    s2.add(71);
    s2.add(47);
    s2.add(12);
    s2.add(25);
    s2.add(24);
    s2.add(23);
    s2.add(24);
    s2.add(32);

   cout << "Stat 2 Results: ";
    cout << " Sum = " << s2.getsum() << " Length = " << s2.getlength() << " Average = " << s2.getaverage() << " " << endl;

   s2.add(5.2);
    s2.add(-3.3);
    s2.add(-8.5);
    s2.add(3.2);
    s2.add(5.5);
cout<<"printing s2"<<endl;
   cout<<s2<<endl;
system("pause");
}

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