How to implement "information hiding" into my code?

A

Amir S.

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;
}
 
A

ak

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
 
J

John Harrison

Amir S. said:
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
 
T

Thomas Matthews

ak
[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
 
A

ak

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
 
D

Daniel T.

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top