473,505 Members | 14,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Data encapsulation

424 Contributor
I have a method which needs to pass some data to the constructors of two other objects. The data is of different types, like a string, an int, etc. I have 12 fields in total.

Rather than write inextensible constructors taking 12 arguments, I decided to make a new class of "DataPacket" objects which will hold the output of the method. These DataPacket objects are then passed to constructors of the other objects. Now, I came up with two prototypes for DataPacket. They both work fine but strike me messy.

The first is const-correct. It is initialized using a 12-argument constructor and the data it contains is subsequentally accessed using its public methods f1(), etc.
Expand|Select|Wrap|Line Numbers
  1. class DataPacket
  2. {
  3. public:
  4.    DataPacket(const string& field1, const int& field2, ... const int field12) 
  5.       : _field1(field1), _field2(field2), ... _field12(field12) {}
  6.  
  7.    string f1() const { return _field1;}
  8.    int    f2() const { return _field2;}
  9.    ...
  10.    double f12() const { return _field12;}
  11.  
  12. private:
  13.    const string _field1;
  14.    const int    _field2;
  15.    ...
  16.    const double _field12;
  17. }
  18.  
  19. // To make packet:
  20. DataPacket data("Hello", 12, ...,  5.4351);
  21.  
  22. // To read packet: 
  23. string s = data.f1();
  24. ...
  25.  
The second is basically a struct. It members are explicitly set and can be subsequently accessed directly, but they can also be changed after its inception which is not desired behaviour.

Expand|Select|Wrap|Line Numbers
  1. class DataPacket
  2. {
  3. public:
  4.    string field1;
  5.    int    field2;
  6.    ...
  7.    double field12;
  8. }
  9.  
  10. // To make packet:
  11. DataPacket dp = { "Hello", 12, ..., 5.4351 };
  12.  
  13. // To read packet: 
  14. string s = dp.field1
  15. ...
  16.  
I like the simplicity of the second but not the fact that its contents can be later modified. This is prevented in the first version, but that contains a number of trivial functions which strike me as reduntant and I don't like its 12-argument constructor.

Passing many arguments to a function must be a common problem, does anybody have nice solutions which work like passing and casting into meaningful types a pointer to void in C, but using proper C++ techniques, passing data-encapsulating objects instead?
May 6 '08 #1
2 1947
weaknessforcats
9,208 Recognized Expert Moderator Expert
The DataPacket approach is the wrong approach. That design looks very like Microsoft's VARIANT. I wouldn't use it.

Instead use objects to replace arguments. For example, suppose your class has a Date object as a member. Rather than have three arguments for the month, day and year, have one Date argument.

Then create a Date, pass it by reference to your constrcutor and let the Date copy constructor take care of making a copy for your object.

Expand|Select|Wrap|Line Numbers
  1. class MyClass
  2. {
  3.     private:
  4.          Date dt;
  5.  
  6.    public:
  7.          MyClass(const Date& arg);
  8. };
  9.  
  10. MyClass::MyClass(const Date& arg) : dt(arg)
  11. {
  12.  
  13. }
  14.  
  15. int main()
  16. {
  17.    Date data(7,4,1776);
  18.    MyClass obj(data);
  19. }
  20.  

This is especially important shouild the Date class get re-designed where the three int arguiments get replaced by a string. If you have a Date& argument in your MyClass constructor, you don't care about the change. Only the code in main() will be affected. If you have three int arguments in MyClass, then you have welded MyClass to a particular version of the Date class. And that means you are on your way to monolithic code.
May 6 '08 #2
arnaudk
424 Contributor
Thanks for your reply!
Well, yes, DataPacket is the very object which I shall pass as an argument. So I think you're saying I should group the contents of DataPacket itself into smaller objects, basically smaller versions of DataPacket?

But what about the large number of get/set functions, is this normal?

At the moment, I have settled with an object encapsulating constant members which are set in the constructor's initialisation list. Then I can access them simply using the member operator "." and not get() functions, yet they can not be inadvertently changed, which is what I want.
May 7 '08 #3

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

Similar topics

6
4102
by: blueblueblue2005 | last post by:
here is a friend function of Class Array, which has two private data member: int size, int *ptr // Array's public member function to return size int getSize() const { return size; } friend...
12
2067
by: Alex Hunsley | last post by:
There's no really specific questions in this post, but I'm looking for people's thought on the issues within... The two main versions I've encountered for data pseudo-hiding (encapsulation) in...
47
3290
by: Roger Lakner | last post by:
I often see operator implemented something like this: class Foo { ... }; class FooList { public: const Foo& operator (unsigned index) const {return array;}; Foo& operator (unsigned index) ...
2
1421
by: Neo | last post by:
I was written this code; in VS2k5 class Program { private: int a;
63
2612
by: time.swift | last post by:
Coming from a C++ / C# background, the lack of emphasis on private data seems weird to me. I've often found wrapping private data useful to prevent bugs and enforce error checking.. It appears...
32
4165
by: bluejack | last post by:
Ahoy: For as long as I've been using C, I've vacillated on the optimal degree of encapsulation in my designs. At a minimum, I aggregate data and code that operate on that data into classlike...
11
5623
by: sofeng | last post by:
I'm not sure if "data hiding" is the correct term, but I'm trying to emulate this object-oriented technique. I know C++ probably provides much more than my example, but I'd just like some feedback...
1
14213
by: subramanian100in | last post by:
I am a beginner in C++. I come across the terms data abstraction and encapsulation in C++. I am unable to understand the definitions. Kindly explain these terms with a simple example in C++ ...
2
7612
by: subramanian100in | last post by:
Is my following understanding correct ? Data abstraction means providing the interface - that is, the set of functions that can be called by the user of a class. Information hiding means...
162
10065
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
0
7216
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
7098
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
7303
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
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...
0
5613
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
4699
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
3187
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...
1
754
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.