How do I implement RetieveData?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BEllis
    New Member
    • Nov 2009
    • 2

    How do I implement RetieveData?

    I need to write a statserver.cpp that implements the items in this header file.


    Code:
    #ifndef STATSERVER_H                                                            
    #define STATSERVER_H                                                            
                                                                                    
    #include <cstdlib>                                                              
    #include <iostream>                                                             
                                                                                    
    class StatServer;                                                               
                                                                                    
    void ReadData    (StatServer& s);                                               
    // reads data from standard input and puts the read data into s                 
                                                                                    
    void DisplayData (const StatServer& s, std::ostream& os, char ofc = ' ');       
    // writes the data in s to the stream os with ofc preceding each data item      
                                                                                    
    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 ) ; // sets internal dat\
    a                                                                               
      void   RetrieveData ( int * data ) const ; // gives client a copy of internal 
                                                 // data                            
                                                                                    
    private:                                                                        
                                                                                    
      size_t size_;                                                                 
      int *  data_; 
      bool   sorted_;                                                               
                                                                                    
      static void  Swap   (int& x, int& y);                                         
    };                                                                              
                                                                                    
    #endif
    I am currently stuck on how to write my implementation for RetrieveData. Any help would be greatly apprieciated. I am on my second so around to fix this program.
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    Warning!! Warning!! Will Robinson

    First of all, the RetrieveData method is an example of what is called breaking encapsulation. This is a big OO no-no. You want to protect the data inside an object and don't want to allow the public world access to your internal data structure.

    The reason not to do this simple. It causes all kinds of problems. You are allowing the public to access and change the values without your knowledge. Also, returning a int * does not tell the public user how much data is available. They still need to extract the size value.

    What to do? A good solution is to have the object limit access to the internal data. You can do this by adding a method:
    Code:
        int getData( ind index) const
        {    return data_[index]; }
    Now the public world can access values, but can't change them. Also (I haven't done it here), you could add code to check the value of index which could throw an exception or return an error code value on an error condition.

    Note: Your SetData method has similar issues.

    If you feel compelled to ignore all of this information, the following will work:
    Code:
    void RetrieveData ( int & size, int * & data ) const 
    {
        size = size_;  
        data = data_; 
    }
    In this case, the function const will do nothing to protect the data values. Size is okay because it is copied to the outside world

    Comment

    • BEllis
      New Member
      • Nov 2009
      • 2

      #3
      Thank you

      Thank you for your help. I am all sorted now.

      Comment

      Working...