Below is the following c++ program. Add to it by following these procedures: 1.
ID: 667377 • Letter: B
Question
Below is the following c++ program. Add to it by following these procedures:
1. Two constructor methods that initialize your statistician objects: One is the default and one to initialize a statistician with the value of another statistician. The second constructor is called a copy constructor. Ex. "Statistician stat1;" declares an object as you normally do. The second one "Statistician stat2(stat1);" declares stat2 and initializes stat2 to the values (data member values) in stat1. So if you print stat2 and stat1, both would have the same values.
The copy constructor would look like
Statistician ( statistician s ) //where 's' is the argument 'stat1' above.
{ Several assignments of the data members}
2. An overloaded (<<) output method using the "friend" attribute.
The output should look something like the following where you print out the data members with some labeling:
sum = xxx
len = xxx
....
3. An overloaded (+) as a function outside the class.
4. An overloaded (==) and (!=) as methods.
5. Add a 'erase' function member that resets a Statistician object.
Use the following input to test you program.
Data for statistician1: 5.5, 6.6, 8.8, -3.4, -0.5, 4.7, 9.1
Print out: the Sum, length and average only for statistica1 (you'll need getter functions for these)
add to statistician1: 5.2 - 3.3 - 8.5 3.2 5.5
Print out: statistician1 using the << operator.
Data for statistician2: initialize statistician2 with the value of statistician1 on declaration of statistician2.
Print out: statistician 1 and statistician2 using the << operator.
add to statistician2: 13 21 71 47 12 25 24 23 24 32
Print out: the Sum, length and average only for statistica2
Print out: statistician 1 & statistician2 using the << operator.
Add two statisticians together. The results should add the sums, the lengths and determine which of the two are the largest and the smallest of the new statisticians form the two inputs. ex statistician3 largest is the largest of statistican1 and statistician 2 for the statement below
statistician3 = statistician1 + statistica2;
Print out: statistician1 & statistician2 & statistician3 using the << operator.
Erase statistician2: Reset statistican2 to its initial declaration by calling the function member 'erase'.
Print out: statistician1 & statistician2 & statistician3 using the << operator.
:---Statistician Program---:
#include "stdafx.h"
#include <iostream>
using namespace std;
class statistic
{
private:
int value;
double largest, smallest, sum;
public:
statistic()
{
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 clear()
{
sum = 0;
value = 0;
}
};
int main()
{
statistic 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 << "Data for Statistician 1: ";
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);
cout << "" << endl;
cout << "After adding numbers. " << endl;
cout << " Average = " << s1.getaverage() << " Smallest = " << s1.getsmallest() << " Largest = " << s1.getlargest() << endl << endl;
statistic s2;
cout << "Data for Statistician 2 [Empty Data]: ";
cout << " Sum = " << s2.getsum() << " Length = " << s2.getlength() << " Average = " << s2.getaverage() << endl;
cout << "" << endl;
cout << "After adding numbers. " << endl;
s2.add(103);
s2.add(821);
s2.add(871);
s2.add(487);
s2.add(312);
s2.add(245);
s2.add(224);
s2.add(623);
s2.add(424);
s2.add(432);
cout << " Sum = " << s2.getsum() << " Length = " << s2.getlength() << " Average = " << s2.getaverage() << endl;
cout << " Clearing values in statistician 2. " << endl;
s2.clear();
cout << "Data for Statistician 2 [With Data]: ";
s2.add(9.3);
s2.add(-6.3);
s2.add(8.2);
s2.add(7.2);
s2.add(8.2);
s2.add(8.8);
s2.add(7.2);
s2.add(-3.2);
s2.add(7.6);
s2.add(9.3);
cout << " Sum = " << s2.getsum() << " Length = " << s2.getlength() << " Average = " << s2.getaverage() << endl;
cout << " Average = " << s2.getaverage() << " Smallest = " << s2.getsmallest() << " Largest = " << s2.getlargest() << endl << endl;
cout << "" << endl;
system("pause");
}
Explanation / Answer
#include "stdafx.h"
#include <iostream>
using namespace std;
class statistic
{
private:
int value;
double largest, smallest, sum;
public:
statistic()
{
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 clear()
{
sum = 0;
value = 0;
}
};
int main()
{
statistic 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 << "Data for Statistician 1: ";
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);
cout << "" << endl;
cout << "After adding numbers. " << endl;
cout << " Average = " << s1.getaverage() << " Smallest = " << s1.getsmallest() << " Largest = " << s1.getlargest() << endl << endl;
statistic s2;
cout << "Data for Statistician 2 [Empty Data]: ";
cout << " Sum = " << s2.getsum() << " Length = " << s2.getlength() << " Average = " << s2.getaverage() << endl;
cout << "" << endl;
cout << "After adding numbers. " << endl;
s2.add(13);
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 << " Sum = " << s2.getsum() << " Length = " << s2.getlength() << " Average = " << s2.getaverage() << endl;
cout << " Clearing values in statistician 2. " << endl;
s2.clear();
cout << "Data for Statistician 2 [With Data]: ";
s2.add(9.3);
s2.add(-6.3);
s2.add(8.2);
s2.add(7.2);
s2.add(8.2);
s2.add(8.8);
s2.add(7.2);
s2.add(-3.2);
s2.add(7.6);
s2.add(9.3);
cout << " Sum = " << s2.getsum() << " Length = " << s2.getlength() << " Average = " << s2.getaverage() << endl;
cout << " Average = " << s2.getaverage() << " Smallest = " << s2.getsmallest() << " Largest = " << s2.getlargest() << endl << endl;
cout << "" << endl;
}
statistic s3;
{
s1.add+s2.add;
cout << "After adding s1 and s2. " << endl;
cout << " Sum = " << s3.getsum() << " Length = " << s3.getlength() << " Average = " << s4.getaverage() << endl;
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.