473,511 Members | 16,769 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I implement RetieveData?

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


Expand|Select|Wrap|Line Numbers
  1. #ifndef STATSERVER_H                                                            
  2. #define STATSERVER_H                                                            
  3.  
  4. #include <cstdlib>                                                              
  5. #include <iostream>                                                             
  6.  
  7. class StatServer;                                                               
  8.  
  9. void ReadData    (StatServer& s);                                               
  10. // reads data from standard input and puts the read data into s                 
  11.  
  12. void DisplayData (const StatServer& s, std::ostream& os, char ofc = ' ');       
  13. // writes the data in s to the stream os with ofc preceding each data item      
  14.  
  15. class StatServer                                                                
  16. {                                                                               
  17. public:                                                                     
  18.  
  19.   StatServer  () ;                                                              
  20.   ~StatServer () ;                                                              
  21.   StatServer  ( const StatServer& ) ;                                           
  22.  
  23.   StatServer& operator =   ( const StatServer& ) ;                              
  24.  
  25.   double Mean         () const ;                                                
  26.   double Median       () ;                                                      
  27.   void   Sort         () ;                                                      
  28.  
  29.   size_t Size         () const ; 
  30.   void   SetData      ( const int * data , size_t size ) ; // sets internal dat\
  31. a                                                                               
  32.   void   RetrieveData ( int * data ) const ; // gives client a copy of internal 
  33.                                              // data                            
  34.  
  35. private:                                                                        
  36.  
  37.   size_t size_;                                                                 
  38.   int *  data_; 
  39.   bool   sorted_;                                                               
  40.  
  41.   static void  Swap   (int& x, int& y);                                         
  42. };                                                                              
  43.  
  44. #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.
Nov 15 '09 #1
2 1281
RRick
463 Recognized Expert Contributor
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:
Expand|Select|Wrap|Line Numbers
  1.     int getData( ind index) const
  2.     {    return data_[index]; }
  3.  
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:
Expand|Select|Wrap|Line Numbers
  1. void RetrieveData ( int & size, int * & data ) const 
  2. {
  3.     size = size_;  
  4.     data = data_; 
  5. }
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
Nov 15 '09 #2
BEllis
2 New Member
Thank you for your help. I am all sorted now.
Nov 15 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
3800
by: Charles Law | last post by:
This is going to seem like a basic OO question, but it comes up and bites me every now and again. Suppose we have a multi-tiered protocol to implement, what is the logical, OO way to design the...
2
4810
by: Billy Porter | last post by:
Greetings, I got a class that wraps the System.Data.SqlClient.SqlConnection class (no COM interaction). I'm not sure if I'm supposed to implement the IDisposable pattern for this wrapper or not....
4
3692
by: Sanjay Vyas | last post by:
Sorry, forgot to cross post this one.. This is rather unusual as we would expect any Collection class to implement ICollection interface and furthermore a Dictionary class should implement...
4
17166
by: Peter | last post by:
I want to copy a parent class instance's all datas to a child's. It's actually a C++'s copy constructor. But why the following code does not work - there is a compile error! How it should look...
13
2647
by: Sherif ElMetainy | last post by:
Hello I was just got VS 2005 preview, and was trying generics. I tried the following code int intArray = new int; IList<int> intList = (IList<int>) intArray; it didn't compile, also the...
3
2700
by: Brett Hall | last post by:
I have a VB.NET interface that my managed C++ code is to implement. I seem to be stuck implementing an event defined in that interface. Does anyone have a simple code snippet that will show me...
5
19560
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
7
15637
by: moondaddy | last post by:
If I'm in a class that inherits an interface, is there a shortcut key that will write the implementation of the interface into the class? I remember seeing something like this in vb.net. ...
0
2812
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
5
2380
by: Tony Johansson | last post by:
Hello! Assume you have the following interface and classes shown below. It is said that a class must implement all the methods in the interface it inherits. Below we have class MyDerivedClass...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.