Hello all it has been a long time since I have asked a question on here, but I am having trouble with this overloaded + operator. I am trying to combine the two objects and return a new object. when my code acesses this function it fails. What did I do wrong? Sorry about my bad spelling
I'm curious - what led you to the conclusion that not giving us detailed, specific information about how it's failing, was the best way to get our help?
sorry about that its been about a year since I have posted anything on here. It fails meaning I do not get a new object with the length of s1 + s2. I truly thought the problem was in the function that I posted.
//FILE: stats.cpp
// CLASS IMPLEMENTED: stats
#include "stats.h" // provides the stats class definition
namespace jesseb303
{
statistician::statistician() : count(0), total(0), tinyest(0), largest(0)
{} //initializing everything to 0
/*-----------------------------------------------------------------------+
| next(double r)
|
| Give the statistician a new number
|
| Post: the count of the numbers statistician has seen has been
| incremented
| the sum of all the numbers statistician has seen has been
| increment by r
| the values of tinyest and largest have been adjusted if
| necessary
*/
void statistician::next(double r)
{
if (count <= 0) { // update the number of entries statistician has
count = 1; // this is the first data item of the statistician
total = r;
tinyest = r;
largest = r;
return;
}
count += 1;
total += r; // adjust the sum
if (r < tinyest) { // update the min if necessary
tinyest = r;
}
if (largest < r) { // update the max if necessary
largest = r;
}
}
void statistician::reset( )
{
count = 0; // resets all values back to 0
tinyest = 0;
largest = 0;
total = 0;
}
/*-------------------------------------------------------------------+
| int length( ) const
|
| RETURN count which 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).
*/
int statistician::length() const
{
return count;
}
double statistician::sum() const
{
return total;
}
double statistician::mean() const
{
return total/count; // mean = total divided by the number of numbers
}
double statistician::minimum() const
{ // minimum = tinyest number
return tinyest;
}
double statistician::maximum() const
{
return largest; // maximum = largest number
}
booloperator ==(const statistician& s1, const statistician& s2)
{
if (s1.length()==s2.length())
{
return (1);
}
elsereturn (0);
}
statistician operator + (const statistician & s1, const statistician & s2)
{
statistician s3;
s3.next(s1.sum() + s2.sum());
return s3;
}
statistician operator * (const statistician& s1, const statistician& s2)
{
statistician s3;
s3.next(s1.sum() * s2.sum());
return s3;
}
}
the last two functions the ones with the overloaded operators dont work right
I apologize if I did something wrong. I tried to be as clear as possible that's why originally I only posted the function that I thought was wrong. My code does compile and run with no errors. The two overloaded operators are not returning anything, hence the title of this thread. I'm sure I implemented them wrong.
The only way to learn programming is to program and I want to learn.
It fails meaning I do not get a new object with the length of s1 + s2.
What do you mean length?
If you mean this length
1 2 3 4
int statistician::length() const
{
return count;
}
Then it works as it supposed to work, the length() of new object returned by operator "+" or "*" would be 1, no matter what are values of this objects...
If you want to change it, don't use your void next(double r); function.
And could you tell us how exactly should your operators work? Give examples...