473,583 Members | 3,072 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to implement "informatio n hiding" into my code?

Hi,

I'm a newbie to C++ (2 weeks into the course). We were given this
assignment to write some code that reads a set of integers (grades)
from a file (filename passed by console), outputs them in reverse
order, calculates and prints their average.

=> We have been told that we need to make sure that our code meets
"informatio n hiding" guidelines. Could anyone help me get started with
this (any ideas?)

Thank you for your help,
Dave

Here is my code (functionality-wise it's correct, it also compiles):

#include <iostream>
#include <fstream>
using namespace std;

int main (int argc, char* argv[]) {
int numItems = 0;
int counter;
double sum = 0;

if (argc <= 1 ) {
cerr << "Usage: ./q2 \"filename\" " << endl;
exit (1);
}

ifstream is_rawGrades (argv[1]);

if (is_rawGrades == NULL) {
cerr << "Couldn't open the file " << argv[1] << endl;
exit (1);
}

is_rawGrades >> numItems;

//case where there is only a 0 in the file
if (numItems == 0){
cout << "No items in the file."
<< "NaN";
}

//declare array and initialize counter
int items[numItems];
counter = 0;

//loops till finds number of items specified
while (true) {
int grade;
is_rawGrades >> grade;

if (is_rawGrades.f ail()) {
break; // out of while loop
}

items[counter] = grade;
sum += grade;
counter++;
}

//outputs array contents in reverse
for (int i = numItems; i>0; i--){
cout << items[i-1] << ";";
}
//calculates and prints out average
double avg = sum / numItems;
cout << endl << avg << endl;
}
Jul 22 '05 #1
5 2177
ak
On 24 May 2004 07:19:32 -0700, am********@yaho o.com (Amir S.) wrote:
Hi,

I'm a newbie to C++ (2 weeks into the course). We were given this
assignment to write some code that reads a set of integers (grades)
from a file (filename passed by console), outputs them in reverse
order, calculates and prints their average.

=> We have been told that we need to make sure that our code meets
"informatio n hiding" guidelines. Could anyone help me get started with
this (any ideas?)

Thank you for your help,
Dave

Here is my code (functionality-wise it's correct, it also compiles):

#include <iostream>
#include <fstream>
using namespace std;

int main (int argc, char* argv[]) {
int numItems = 0;
int counter;
double sum = 0;

if (argc <= 1 ) {
cerr << "Usage: ./q2 \"filename\" " << endl;
exit (1);
}

ifstream is_rawGrades (argv[1]);

if (is_rawGrades == NULL) {
cerr << "Couldn't open the file " << argv[1] << endl;
exit (1);
}

is_rawGrades >> numItems;

//case where there is only a 0 in the file
if (numItems == 0){
cout << "No items in the file."
<< "NaN";
}

//declare array and initialize counter
int items[numItems];
counter = 0;

//loops till finds number of items specified
while (true) {
int grade;
is_rawGrades >> grade;

if (is_rawGrades.f ail()) {
break; // out of while loop
}

items[counter] = grade;
sum += grade;
counter++;
}

//outputs array contents in reverse
for (int i = numItems; i>0; i--){
cout << items[i-1] << ";";
}
//calculates and prints out average
double avg = sum / numItems;
cout << endl << avg << endl;
}


well i am not sure what you mean but i interpret
it like this:

data hiding is vs. the user of your code

In your example above you have determined that
the grade should be an integer. instead, try an
approach where you don't assume that it is an integer.

This can be accomplished by creating a class called
CGrade. In the CGrade class you can overload the <<
operator to read a grade from the stream.

The end effect is that the user of your class will
not know - and doesnt need to know that grades are
stored as integers.

try also looking up vector from the Standard template
library to hold your vector of CGrade objects - it is
pretty easy to use and elimates some code above like
keeping a counter.

hth
ak
Jul 22 '05 #2

"Amir S." <am********@yah oo.com> wrote in message
news:2c******** *************** ***@posting.goo gle.com...
Hi,

I'm a newbie to C++ (2 weeks into the course). We were given this
assignment to write some code that reads a set of integers (grades)
from a file (filename passed by console), outputs them in reverse
order, calculates and prints their average.

=> We have been told that we need to make sure that our code meets
"informatio n hiding" guidelines. Could anyone help me get started with
this (any ideas?)

Information hiding is the OO principle where a class only exposes the
interface necessary to get it job done, and hides everything else.

So in your case you might design a class called GradeSet and have three
methods, input, output and calculate_avera ge. I.e.

class GradeSet
{
public:
// read grades from a file (or other stream)
void input(istream&) ;

// output grades in reverse order
void output(ostream& ) const;

// calculate average
double calculate_avera ge() const;

private:
...
};

Now either your on the wrong course or its very badly run. I wouldn't expect
a two week old C++ newbie to be writing classes like the above. I'd buy a
C++ text book and read up on classes, pay special attention to the
distinction between interface and implementation. What you are being asked
to do is write an interface but hide the implementation.
Thank you for your help,
Dave

Here is my code (functionality-wise it's correct, it also compiles):

Incidentally I expect that output grades in reverse order means not output
then in the reverse order from which they were entered, but output them in
reverse order after sorting them, i.e. output in descending order. But I
could be wrong.

Also incidentally
//declare array and initialise counter
int items[numItems];


The above is not legal C++. Array bounds must be compile time constants in
C++. You are obviously using a compiler that allows you to break this rule.

john
Jul 22 '05 #3
ak
On 24 May 2004 07:19:32 -0700, am********@yaho o.com (Amir S.) wrote:

Hi,

I'm a newbie to C++ (2 weeks into the course). We were given this
assignment to write some code that reads a set of integers (grades)
from a file (filename passed by console), outputs them in reverse

order, calculates and prints their average.

=> We have been told that we need to make sure that our code meets
"informati on hiding" guidelines. Could anyone help me get started with
this (any ideas?)

Thank you for your help,
Dave

[snip]


well i am not sure what you mean but i interpret
it like this:

data hiding is vs. the user of your code

In your example above you have determined that
the grade should be an integer. instead, try an
approach where you don't assume that it is an integer.

This can be accomplished by creating a class called
CGrade. In the CGrade class you can overload the <<
operator to read a grade from the stream. I don't understand why the class name has to be prefixed
with a C, as in CStupidClassNam e.

What does the C stand for?
Should structures be prepended with 'S'?
Would this be incompatible with Borland's Library that
starts every class with 'T'?

http://www.jelovic.com/articles/stupid_naming.htm

The end effect is that the user of your class will
not know - and doesnt need to know that grades are
stored as integers.

try also looking up vector from the Standard template
library to hold your vector of CGrade objects - it is
pretty easy to use and elimates some code above like
keeping a counter.

hth
ak

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #4
ak
On Mon, 24 May 2004 16:25:59 GMT, Thomas Matthews
<Th************ *************** *@sbcglobal.net > wrote:
I don't understand why the class name has to be prefixed
with a C, as in CStupidClassNam e.


umm I didn't say that classes /have to/ be prefixed with
a C.

Whether or not to prefix variable names, classes enums
etc is more of a personal preference than anything.

I personally find it helps understanding somebody else's
program faster when getting a hint of what a variable is
if the programmer has been to lazy to write long descriptive
variable names.

in the case of prefixing structures.. i dont do that, but
I do postfix typedefs with _t. muhahaha
/ak
Jul 22 '05 #5
In article <2c************ **************@ posting.google. com>,
am********@yaho o.com (Amir S.) wrote:
I'm a newbie to C++ (2 weeks into the course). We were given this
assignment to write some code that reads a set of integers (grades)
from a file (filename passed by console), outputs them in reverse
order, calculates and prints their average.

=> We have been told that we need to make sure that our code meets
"informatio n hiding" guidelines. Could anyone help me get started with
this (any ideas?)


My first thought is that if the instructor cannot provide an example
that *requires* you to use the feature provided, then he probably
doesn't understand the feature. On the other hand, this particular
feature is used to guard against mistakes, we hide infomration so that
others won't use it inapproprately. ..

Let me ask you, what are these "informatio n hiding guidelines" that you
teacher speaks of?
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2059
by: Picho | last post by:
Hi all, Lets say I have a "secret" I wish to "hide", lets say a database password. For the more detailed problem, a web application/service that uses a connection string. all the solutions I came up with (embedding in code, encrypting-decrypting) involve embedding the/another secret in the code. since my problem cannot request a user...
10
31684
by: FX | last post by:
I wanna publish a script on my site which allows me to hide image source. i have rough idea abt it. i`ll point src to some php page like: <img src="image.php"> & in tht php wat exactly shud be done so tht user doesnt come to know the real source location of image file upon clicking its properties. I've seen websites doing this. can somebody...
3
6561
by: Nicolas Castagne | last post by:
Hi all, I have been wondering for a while why function hiding (in a derived class) exists in C++, e.g. why when writing class Base { void foo( int ) {} }; class Derived: public Base { void foo( char const ) {} };
10
32850
by: phforum | last post by:
Hi, I wrote a PHP page for user input the information to search the database. And the database data will update every second. I want to set the auto refresh to get the data from database every minute. But the page always display the dialog box ask me to resend the information. How to disable this warning message. I using POST and...
11
5633
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 to find out if I've done anything wrong. Also, I am working on this for an embedded environment, so if there are great inefficiencies with this...
0
7826
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8182
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8327
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...
1
7935
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6579
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...
0
5374
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...
1
2333
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
1
1433
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1157
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.