By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,131 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,131 IT Pros & Developers. It's quick & easy.

How to implement "information hiding" into my code?

P: n/a
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
"information 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.fail()) {
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
Share this Question
Share on Google+
5 Replies


P: n/a
ak
On 24 May 2004 07:19:32 -0700, am********@yahoo.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
"information 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.fail()) {
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

P: n/a

"Amir S." <am********@yahoo.com> wrote in message
news:2c**************************@posting.google.c om...
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
"information 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_average. 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_average() 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

P: n/a
ak
On 24 May 2004 07:19:32 -0700, am********@yahoo.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
"information 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 CStupidClassName.

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.learn.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

P: n/a
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 CStupidClassName.


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

P: n/a
In article <2c**************************@posting.google.com >,
am********@yahoo.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
"information 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 "information hiding guidelines" that you
teacher speaks of?
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.