operator overloading

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.

#ifndef STATS_H // Prevent duplicate definition
#define STATS_H
#include <iostream>

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

// MODIFICATION MEMBER FUNCTIONS
void next_number(double nextNum);
void reset();

// CONSTANT MEMBER FUNCTIONS
int length() const {return count; }
double sum() const { return total; }
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);
bool operator !=(const statistician& s1, const statistician& s2);
}

#endif
------------------------------------------------------------------
this is my class definition

//#include "stdafx.h"
#include "stats.h"
#include <cassert>

namespace main_stats
{
statistician::statistician()
{
reset();
}

void statistician::next_number(double nextNum)
{
if (count <= 0)
{
count = 1;
total = nextNum;
tinyest = nextNum;
largest = nextNum;
return;
}

else
{
count += 1;
total += nextNum;

if (nextNum < tinyest)
tinyest = nextNum;

if (nextNum > largest)
largest = nextNum;
}

mean();
}

void statistician::reset()
{
count = 0;
total = 0;
tinyest = 0;
largest = 0;
}

double statistician::mean() const
{
assert(count > 0);
return total / count;
}

double statistician::minimum() const
{
assert(count > 0);
return tinyest;
}

double statistician::maximum() const
{
assert(count > 0);
return largest;

}
statistician operator + (const statistician& s1, const statistician& s2)
{
statistician combine;

combine.sum() = s1.sum() + s2.sum();
combine.tinyest = s1.tinyest + s2.tinyest;
combine.mean() = s1.mean() + s2.mean();
combine.largest = s1.largest + s2.largest;

return combine;

}

}
bool operator ==(const statistician& s1, const statistician& s2)
{

return (s1.sum( ) == s2.sum( ));
}

bool operator !=(const statistician& s1, const statistician& s2)
{
return !(s1 == s2);
}

statistician operator *(double scale, const statistician& s)
{
statistician result(s);
result.total *= scale;
result.tinyest *= scale;
result.largest *= scale;
return result;
}
}
-------------------------------------------

Last edited on
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.
Take this advice and edit your posts :
http://www.cplusplus.com/articles/jEywvCM9/
1
2
3
4
5
6
7
8
9
statistician operator + (const statistician& s1, const statistician& s2)
{   statistician combine;

    combine.sum() = s1.sum() + s2.sum();
    combine.tinyest = s1.tinyest + s2.tinyest;
    combine.mean() = s1.mean() + s2.mean();
    combine.largest = s1.largest + s2.largest;
    return combine;
}


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.
Last edited on
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.

The next_number() should not call the mean().
thankyou a lot.
Topic archived. No new replies allowed.