473,385 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to implement "information 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
"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
5 2155
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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
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...
10
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...
3
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...
10
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...
11
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.