hello,
i am trying to write the definition for a class which looks like the following and the problem i am facing is with the addition and multiplication operator. please advise.
this is the header file:
//
// VALUE SEMANTICS for the statistician class:
// Assignments and the copy constructor may be used with statistician objects.
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);
bool operator !=(const statistician& s1, const statistician& s2);
}
#endif
------------------------------------------------------------------
this is my class definition
i know i am making a mistake in the operator overloading +
but i don't know how to call a function and add them. please help.
** combine.sum() = s1.sum() + s2.sum();
**combine.mean() = s1.mean() + s2.mean();
this are the errors.
here is the details for the header file:
// CLASS PROVIDED: statistician
// (a class to keep track of statistics on a sequence of real numbers)
// This class is part of the namespace main_savitch_2C.
//
// CONSTRUCTOR for the statistician class:
// statistician( );
// Postcondition: The object has been initialized, and is ready to accept
// a sequence of numbers. Various statistics will be calculated about the
// sequence.
//
// PUBLIC MODIFICATION member functions for the statistician class:
// void next(double r)
// The number r has been given to the statistician as the next number in
// its sequence of numbers.
// void reset( );
// Postcondition: The statistician has been cleared, as if no numbers had
// yet been given to it.
//
// PUBLIC CONSTANT member functions for the statistician class:
// int length( ) const
// Postcondition: The return value is the length of the sequence that has
// been given to the statistician (i.e., the number of times that the
// next(r) function has been activated).
// double sum( ) const
// Postcondition: The return value is the sum of all the numbers in the
// statistician's sequence.
// double mean( ) const
// Precondition: length( ) > 0
// Postcondition: The return value is the arithmetic mean (i.e., the
// average of all the numbers in the statistician's sequence).
// double minimum( ) const
// Precondition: length( ) > 0
// Postcondition: The return value is the tinyest number in the
// statistician's sequence.
// double maximum( ) const
// Precondition: length( ) > 0
// Postcondition: The return value is the largest number in the
// statistician's sequence.
//
// NON-MEMBER functions for the statistician class:
// statistician operator +(const statistician& s1, const statistician& s2)
// Postcondition: The statistician that is returned contains all the
// numbers of the sequences of s1 and s2.
// statistician operator *(double scale, const statistician& s)
// Postcondition: The statistician that is returned contains the same
// numbers that s does, but each number has been multiplied by the
// scale number.
// bool operator ==(const statistician& s1, const statistician& s2)
// Postcondition: The return value is true if s1 and s2 have the zero
// length. Also, if the length is greater than zero, then s1 and s2 must
// have the same length, the same mean, the same minimum,
// the same maximum, and the same sum.
Lines 4,6: You can't put a function call on the left side of an assignment.
You have to assign the four member variables.
1 2 3 4 5 6 7 8 9 10 11
#include <algorithm>
statistician operator + (const statistician& s1, const statistician& s2)
{ statistician combine;
combine.count = s1.count + s2.count; // Add the counts together
combine.total = s1.total + s2.total; // Add the totals together
combine.tinyest = std::min(s1.tinyest,s2.tinyest); // find the smaller of the two
combine.largest = std::max(s1.largest, s2.largest); // find the larger of the two
return combine;
}
PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
The mean() should be computed from the 'total' and the 'count' rather than stored as member variable. From that follows that within the op== there is no need to compare the means.