# C++: Stat implementation (class defintion)



## soulfly (May 29, 2004)

_Could anyone here help me for the class declaration of this code?_

Here's the header file:

#ifndef STATS_H // Prevent duplicate definition
#define STATS_H
#include <iostream>
using namespace std;

class statistician
{
public:
// CONSTRUCTOR
statistician( );

// MODIFICATION MEMBER FUNCTIONS
void next(double r);
void reset( );

// CONSTANT MEMBER FUNCTIONS
int length( ) const;
double sum( ) const;
double mean( ) const;
double minimum( ) const;
double maximum( ) const;
// FRIEND FUNCTIONS

friend 
statistician operator + (const statistician& s1, const statistician& s2);
friend 
statistician operator * (double scale, const statistician& s);

private:

int count; // How many numbers in the sequence
double total; // The sum of all the numbers in the sequence
double tinyest; // The smallest number in the sequence
double largest; // The largest number in the sequence
};

// NON-MEMBER functions for the statistician class
bool operator ==(const statistician& s1, const statistician& s2);

#endif

----------------------------------------------------

Here's the main program

int main( )
{
statistician s1, s2, s3; // Three statisticians for us to play with
char choice; // A command character entered by the user
double x; // Value for multiplication x*s1

cout << "Three statisticians s1, s2, and s3 are ready to test." << endl;

do
{
cout << endl;
print_menu( );
choice = toupper(get_user_command( ));
switch (choice)
{
case 'R': cout << "Which one should I reset (1, 2, 3) " << endl;
choice = get_user_command( );
switch (choice)
{
case '1': s1.reset( );
break;
case '2': s2.reset( );
break;
case '3': s3.reset( );
break;
}
cout << "Reset activated for s" << choice << "." << endl;
break;
case '1': s1.next(get_number( ));
break;
case '2': s2.next(get_number( ));
break;
case '3': s3.next(get_number( ));
break;
case 'T': cout << "The values are given in this table:" << endl;
cout << " LENGTH SUM"
<< " MINIMUM MEAN MAXIMUM" << endl;
cout << " s1";
print_values(s1);
cout << " s2";
print_values(s2);
cout << " s3";
print_values(s3);
break;
case 'E': if (s1 == s2)
cout << "s1 and s2 are equal." << endl;
else
cout << "s1 and s2 are not equal." << endl;
break;
case '+': s3 = s1 + s2;
cout << "s3 has been set to s1 + s2" << endl;
break;
case '*': cout << "Please type a value for x: ";
cin >> x;
s3 = x * s1;
cout << "s3 has been set to " << x << " * s1" << endl;
break;
case 'Q': cout << "Ridicule is the best test of truth." << endl;
break;
default: cout << choice << " is invalid. Sorry." << endl;
}
}
while ((choice != 'Q'));

return EXIT_SUCCESS;

}

void print_menu( )
{
cout << endl;
cout << "The following choices are available: " << endl;
cout << " R Activate one of the reset( ) functions" << endl;
cout << " 1 Add a new number to the 1st statistician s1" << endl;
cout << " 2 Add a new number to the 2nd statistician s2" << endl;
cout << " 3 Add a new number to the 3rd statistician s3" << endl;
cout << " T Print a table of values from the statisticians" << endl;
 cout << " E Test whether s1 == s2" << endl;
cout << " + Set the third statistician s3 equal to s1 + s2" << endl;
cout << " * Set the third statistician s3 equal to x*s1" << endl;
cout << " Q Quit this test program" << endl;
}

char get_user_command( )
// Library facilties used: iostream.h
{
char command;

cout << "Enter choice: ";
cin >> command;

return command;
}

double get_number( )
// Library facilties used: iostream.h
{
double result;

cout << "Please enter the next real number for the sequence: ";
cin >> result;
cout << result << " has been read." << endl;
return result;
}

void print_values(const statistician& s)
// Library facilties used: iostream.h
{
cout << setw(10) << s.length( );
cout << setw(10) << s.sum( );
if (s.length( ) != 0)
{
cout << setw(10) << s.minimum( );
cout << setw(10) << s.mean( );
cout << setw(10) << s.maximum( );
}
else
cout << "END";
cout << endl;
}

---------------------------
_What would be the right class declaration to run this program?_


----------



## CoolerMaster (Jun 22, 2004)

Im just a beginnning c++ programmer, so I wouldn't know the answer to your question. I do have a c++ manual though and the author provides an email adress for you to contact him. It is:

[email protected]

and his website is jeffcogswell.com. You can see if he is interested in helping you out. He seems like a nice guy.

Adios,

CoolerMaster


----------

