Connecting Tech Pros Worldwide Forums | Help | Site Map

Class Implementation

Newbie
 
Join Date: Oct 2009
Posts: 17
#1: 4 Weeks Ago
A while back had a program to write that had a user enter integers. I would then calulate the Mean, Median, and sort the array into ascending order. Int Main() did the cout<< of the data. Int Main() called function Mean and Median. Function Median called function Sort (to sort numerically), and function Sort called function Swap (to place the lesser integers in the beginning of the array).I now have to do the same but instead of functions, I have to use a class.

I have the following class definition that I have to use:
#ifndef STATSERVER_H
#define STATSERVER_H

#include <cstdlib>
#include <iostream>

class StatServer;

void ReadData (StatServer& s);
void DisplayData (const StatServer& s, std::ostream& os, char ofc = ' ');

class StatServer
{
public:

StatServer () ;
~StatServer () ;
StatServer ( const StatServer& ) ;

StatServer& operator = ( const StatServer& ) ;

double Mean () const ;
double Median () ;
void Sort () ;

size_t Size () const ;
void SetData ( const int * data , size_t size ) ;
void RetrieveData ( int * data ) const ;

private:

size_t size_;
int * data_;
bool sorted_;

static void Swap (int& x, int& y);
};

#endif

The problem I have is where to start and how. All of the functions of the first program took the array and size as arguments. The class functions do not except SetData and RetrieveData. When I attempt the SetData implementation:
void StatServer::SetData(const int* data, size_t size)
{
size_ = size;
data_ = data[size_];
}

I get the error: invalid conversion from const int to int*.
How else could I get around this?
Also, how would I calculate the Mean if StatServer::Mean() takes no args?

Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,171
#2: 4 Weeks Ago

re: Class Implementation


You class contains the array in data_ this means that every function that operates on the array, Mean for instance, does not need parameters because it already has the data available to it in the class through the this pointer.

This isn't true for SetData and RetrieveData because they do not operate on the array, the initiaise/create it from supplied data.

Looking at your attempt at SetData you appear to be trying to copy a pointer (sort of). You need to treat data_ and data as if they where arrays when you copy between them.

However data_ is not an array it is a (possibly uninitialised or already containing an array of a different size) pointer. You will need to deallocate any data it already points to and allocate a new array of the correct size.

Arrays Revealed may help.
Newbie
 
Join Date: Oct 2009
Posts: 17
#3: 4 Weeks Ago

re: Class Implementation


Am I on the right path for having SetData(const int * data, size_t size) get the array from the call
StatServer.SetData(numbers, size) in my Int Main()?
//const int * numbers[100];
//size increments up to 100 based on how many integers are entered.
Then the rest of the class knows what to work with (data_)
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,171
#4: 4 Weeks Ago

re: Class Implementation


Yes you are along the right lines but

//const int * numbers[100];

should probably be

//const int numbers[100];
Newbie
 
Join Date: Oct 2009
Posts: 17
#5: 4 Weeks Ago

re: Class Implementation


Where am I going wrong?
The errors:
statserver.cpp:16: error: expected constructor, destructor, or type conversion before ';' token
statserver.cpp:18: error: expected constructor, destructor, or type conversion before ';' token
statserver.cpp:20: error: expected constructor, destructor, or type conversion before '(' token
statserver.cpp:20: error: expected `,' or `;' before '(' token

The Code:

StatServer data_, size_;

StatServer(numbers);

~StatServer();

ReadData(StatServer& numbers);

DisplayData(numbers, std::cout, " ");


void StatServer::SetData(const int * data, size_t size)
{
size_ = size;
data_[size_] = data[size_];
}

double StatServer::Mean() const
{
int sum = 0; //This block add up all of the integers and
for (unsigned int j = 0; j < size_; j++)
sum += data_[j];

return sum/size_;
}


double StatServer::Median()
{


int middle = 0; //This block determines if the array has an odd or even number of elements and then determines the median from there
int first = 0;
int second = 0;
if((data_[size_] % 2) == 1) //Determines if array is even then calculates the average of the two middle terms
{
first = data_[(size_) - 1];
second = data_[size_/2];
middle = (first + second) /2;
}
else
{
middle = data_[(size_-1)/2]; //Array is odd and calulates the median
}
return middle; //Returns median for output
}

void StatServer::Sort()
{
for (int t = 0; t < (int)size_; t++) //This block compares two elements at a time to see which one is less than the other
{
int r = t;
unsigned int h = 0;
for (h = t; h < size_; h++)
{
if (data_[h] < data_[r])
{
r = h;
}
}
StatServer::Swap(r, t); //Calls function Swap to place array in ascending order
}
}

void StatServer::Swap(int& x, int& y)
{
int temp = x; //This block swaps the elements into ascending order
x = y;
y = temp;
}
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 383
#6: 4 Weeks Ago

re: Class Implementation


StatServer data_, size_;
StatServer(numbers);
~StatServer();
ReadData(StatServer& numbers);
DisplayData(numbers, std::cout, " ");

remove these lines you copied from .h file. You don't need them in .cpp.
Newbie
 
Join Date: Oct 2009
Posts: 17
#7: 4 Weeks Ago

re: Class Implementation


I need to implement everything that is statserver.h. Otherwise I get compile errors. I have those taken care of now. My posted cpp has been totally overhauled and I'm not home to post it. I will do it later.
Reply