-2

I am currently doing a programming project where I have declared 2 objects of a class called Statistician. The objects are called s1 and s2. Each object uses a function to read in 3 values of type double into the sequence. Then, for each sequence I compute and print results for: length of sequence, last number entered, sum of sequence, arithmetic mean, smallest number, largest number (each computation has it's own function, which returns the value.

I am currently trying to overload the + operator so that I can add both sequences (each with 3 numbers) together, into a 3rd class object which will have all 6 values. Then, I hope to be able to perform the computations I listed above on this new class. How do I overload the + operator to perform this task?

What I have so far:

Statistician operator +(const Statistician&)
{
//Postcondition: the sum of all numbers in sequences s1 and s2 is returned
}

Also, I have been told that I should make this a friend function to have access to variables I declared in the class. How do I declare the friend function correctly so it has access to do this?

mcdito13
  • 191
  • 1
  • 3
  • 13

1 Answers1

3

You should just look up the Internet (using your preferred search engine) to figure out how a friend function is declared: http://en.cppreference.com/w/cpp/language/friend

class Statistician
{
   friend Statistician operator+(const Statistician&, const Statistician&);
   // other stuff
}

Furthermore, I can only give a skeleton. Would be helpful if you would add your class definition and implementation of Statistician to your question.

Statistician operator+(const Statistician& left, const Statistician& right)
{
    Statistician result;
    // put your code here to add data from "left" and "right" into "result"        
    return result;
}
Matthias J. Sax
  • 59,682
  • 7
  • 117
  • 137