Join the social network of Tech Nerds, increase skill rank, get work, manage projects...

• # How to Implement Two Virtual Function Methods?

• 0
• 0
• 0
• 0
• 0
• 0
• 0
• 273

So what I've been given: Mean.cc is a class that inherits polymorphically from Statistic.h (the only things defined in that class are virtual void Collect(double) = 0; and virtual double Calculate() const = 0;)

So the mean.cc is defined as:

``````/* Stores data (datum) such than an average may be calculated.
* - NOTE: You do not necessarily need to store each datum.
*/
void Collect(double datum);

/* Returns the mean of the data (datum) from the Collect method.
*/
double Calculate() const;``````

And the methods need to be able to pass these tests:

``````  void TestMeanCollect(Statistic* stat)
{
const double kData[] = {34, 54, 99, 102, 43};
const unsigned int kSize = sizeof(kData) / sizeof(double);

for (unsigned int i = 0; i < kSize; ++i)
stat->Collect(kData[i]);
}`

bool TestMeanCalculate(Statistic* stat)
{
const double kData[] = {34, 54, 99, 102, 43};
const unsigned int kSize = sizeof(kData) / sizeof(double);
const double kExpected = (34 + 54 + 99 + 102 + 43) / 5.0;

for (unsigned int i = 0; i < kSize; ++i)
stat->Collect(kData[i]);

double actual = stat->Calculate();
if (kExpected != actual) {
cout << "  Expected: " << kExpected << ", Actual: " << actual <<
endl;
return false;
}

return true;
}``````

What I'm confused about is, can someone please explain the logic of how to do either of these methods? I'm not requesting straight code, I just need an idea of where to start because I'm drawing a complete blank. The way I tried it before is as shown below, but I've been told that that ignores the functions parameters/negates what the actual method is being asked to do.

``````void Collect(double datum)
{
double datum = 0;
int kSize = 5;
double kData[5];
cout << "You can enter 5 values" << endl;
for (int i = 0; i < kSize; ++i)
{
cin >> kData[i];
datum += kData[i];
}
datum = datum / kSize;
}``````

OR
OR
Register

OR
Fill out the form below and instructions to reset your password will be emailed to you:

• Hire
• Post Projects

### Post Projects

• All at 0 Cost ....
• Post Tech Job
• Select Best Bidder
• Track the Project
• Approve Work and Pay safely
• Browse Nerds
• Work
• Find Projects Find Projects
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
View more...
View less...
• Manage
• Company Company

### Manage Company

• All at 0 Cost ....
• Manage Company and Employee Profiles
• Company wide Employee Productivity Reports
• Knowledge Sharing and Collaboration Tools
• Get Sales Lead and Bid for Tech Projects
• Send Invoices and Receive Payment Safely
• Learn
• Nerd Digest Nerd Digest
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
View more...
View less...
• Tech Q & A Tech Q & A
• UI Design and UX
• Software Engineering
View more...
View less...
• Marketing
• General
View more...
View less...