473,671 Members | 2,511 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 1291
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
3820
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 handler? For example, let's say that we have to implement the ISO 7-layer protocol, or something like an Allen-Bradley master-slave protocol. At the lowest layer we might only need to top and tail the data being transported, such as DLE STX DLE...
2
4820
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. Since one of it's members (SqlConnection) implements this interface, I'm thinking maybe I ought to. But on the other hand, those unmanaged resources has already been wrapped in the SqlConnection class... If so, how would my Dispose method look...
4
3704
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 IDictionary interface. The StringDictionary class does implements all the methods of ICollection and IDictionary yet it does not list these interfaces. Am I missing something here or was it an oversight on the part of the class developer. Maybe I have...
4
17183
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 like? (The background is I don't know (I don't care indeed) all members in DataGrid, so I don't want to copy all members in DataGrid one by one.) public class GridEx : DataGrid { public GridEx()
13
2688
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 following didn't compile
3
2720
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 the basics of what I need to implement? I've seen all the MSDN articles on implementing events in managed C++ and I've gotten events to work without issue when implementing all the constructs
5
19584
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 having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return an Array instead of an ArrayList. Now I get the message when I try to run just my webservice...
7
15695
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. Thanks. -- moondaddy@nospam.nospam
0
2823
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 magic, I wrote the following module. It is mainly useful as a light weight tool to help programmers catch mistakes at definition time (e.g., forgetting to implement a method required by the given interface). This is handy when unit tests or...
5
2405
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 that inherits IMyInterface but MyDerivedClass doesn't implement method DoSomething() it inherits it from the base class MyBaseClass. So the statement that a class must implement all method in an interface that
0
8483
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8824
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8673
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7444
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6236
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5703
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4416
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2818
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2060
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.