473,382 Members | 1,745 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,382 software developers and data experts.

Which way should this be done? <Theory Question>

Hello.

First off, I am not sure of the exact jargon used, so I will ask a
question regarding it. Then on to my other question.

When you use things like cout and cin from the iostream header file,
what are those called? Is cout and cin functions? What is the proper
term for them a command listed in a header file? Or is it just a
"command"?

My next question.....

We use a Deitel book in my C++ class. All of their sample code in the
book shows that they use the following format for including header
files and using the "term from above" contained in them.

#include <iostream>
using std::cout;
using std::cin;

I have also seen it in other places as

#include <iostream>
using namespace std;

What I have always done, and what my teacher says is a carry over from
C, is this....

#include <iostream.h>

so that none of the using commands are needed. When your using quite a
few header files, those using statements get a bit cumbersome.

So, what is the "proper" way to do it? I am sure everyone has their
own way of doing it and likes to stick to their way. What are the
rewards or benifits from doing it each way? Are there any or is it
strictly personal taste?

Thanks in advance for any ideas.

DV
Jul 19 '05 #1
10 2262
"da Vinci" <bl***@blank.com> wrote in message
news:pv********************************@4ax.com...
| When you use things like cout and cin from the iostream header file,
| what are those called? Is cout and cin functions? What is the proper
| term for them a command listed in a header file? Or is it just a
| "command"?

cin and cout are predifined objects from the iostream classes. There is also
clog and cerr (which are pretty much the same mostly) that write to the
error stream (usually just the screen on some systems, unless overridden as
you can do in Linux).

What do you mean by "command"? Are you referring to macros, function/class
declarations, pragmas,...? I can only assume that you are referring to
"using": it's a statement.

| We use a Deitel book in my C++ class. All of their sample code in the
| book shows that they use the following format for including header
| files and using the "term from above" contained in them.
|
| #include <iostream>
| using std::cout;
| using std::cin;

std refers to the C++ standard namespace. Everything is in this namespace. A
namespace is basically a way for names to coincide with other similar names
(that you may write for example). If you were to write a class vector<> for
example, you could distinguish between instantiating yours and the
standards:

vector<int> myVec; // Your vector type
std::vector<int> stdVec; // standard vector

| I have also seen it in other places as
|
| #include <iostream>
| using namespace std;

This brings every name, function, class, type (etc) into the global
namespace. This means that you may conflict with something from the standard
(like indentical function names w/ signatures). Your professor should cover
namespaces in your course eventually.

| What I have always done, and what my teacher says is a carry over from
| C, is this....
|
| #include <iostream.h>

You need a better teacher. iostream.h is not from C, but rather from C++
before there was a standard for it. It is archaic and only kept for
backwards compatibility (for that lazy asses that won't update their
software to reflect ANSI/ISO IEC 14882-1998). All standard headers carry no
extension (no .h). For the standard C headers, the same is true but a 'c' is
prepended to their name (stdio.h becomes cstdio).

| so that none of the using commands are needed. When your using quite a
| few header files, those using statements get a bit cumbersome.

There is nothing wrong with just writing "using namespace std". Never use
iostream.h.

| So, what is the "proper" way to do it? I am sure everyone has their
| own way of doing it and likes to stick to their way. What are the
| rewards or benifits from doing it each way? Are there any or is it
| strictly personal taste?

The proper way to do it is to qualify (as your teacher about this) anything
in the std namespace. There is no personal taste involved, just the extra
typing. I could do:

std::vector<int> vecInt;

using std::vector;
vector<int> vecInt;

using namespace std;
vector<int> vecInt;

All are the same. The only difference is the using namespace std brings
everything into scope;
Jul 19 '05 #2

"Greg P." <no@spam.sam> wrote in message
news:9%*****************@newsread4.news.pas.earthl ink.net...
"da Vinci" <bl***@blank.com> wrote in message

| So, what is the "proper" way to do it? I am sure everyone has their
| own way of doing it and likes to stick to their way. What are the
| rewards or benifits from doing it each way? Are there any or is it
| strictly personal taste?

The proper way to do it is to qualify (as your teacher about this)
anything in the std namespace. There is no personal taste involved,
just the extra typing. I could do:

std::vector<int> vecInt;

using std::vector;
vector<int> vecInt;

using namespace std;
vector<int> vecInt;

All are the same. The only difference is the using namespace std brings
everything into scope;


They're not all the same. As Stroustrup points out in The C++ Programming
Language, C.10, a using declaration adds a name to a local scope whereas a
using directive doesn't. Much of the time it may not matter but it can do.


Jul 19 '05 #3
Thanks for the knowledge so far!

But now I have something else troubling me about this. Everyone has
pretty much pointed out that using the .h form in the include
statement is not standard c++ and should not be used.

What advantage do you get over using the "namespace std;" line? What
advantage would you get using the "using std::cout;". I mean the
advantage over using just the .h under the include statement....

It seems to me that adding all of these lines of code for namespace
inclusions and such is a waste of time when you can just use the .h in
the include line.

I know that ANSI would not change something like this, make it harder
or more bulky in the code, for no reason. So, what was their reason?
Does this make programs compatible across many different platforms or
compilers? Does it save on the memory useage of the program?

Again, I am just a beginner. I like to know the whole story instead of
just "Do it this way because ANSI says so!" answer. I like having the
indepth, behind the sense, kind of knowledge.

I appreciate all the help so far.

DV
Jul 19 '05 #4

"da Vinci" <bl***@blank.com> wrote in message
news:qp********************************@4ax.com...
Thanks for the knowledge so far!

But now I have something else troubling me about this. Everyone has
pretty much pointed out that using the .h form in the include
statement is not standard c++ and should not be used.

What advantage do you get over using the "namespace std;" line? What
advantage would you get using the "using std::cout;". I mean the
advantage over using just the .h under the include statement....

It seems to me that adding all of these lines of code for namespace
inclusions and such is a waste of time when you can just use the .h in
the include line.

I know that ANSI would not change something like this, make it harder
or more bulky in the code, for no reason. So, what was their reason?
Does this make programs compatible across many different platforms or
compilers? Does it save on the memory useage of the program?

Again, I am just a beginner. I like to know the whole story instead of
just "Do it this way because ANSI says so!" answer. I like having the
indepth, behind the sense, kind of knowledge.

I appreciate all the help so far.

DV


The purpose of namespaces is to avoid name collisions in code when program
are written by many authors (I think I said that already).

Suppose the std namespace did not exist, and suppose your favourite variable
name was foo, you used it thousands of times in your program. The a new
version of the standard comes out and the standards committee have added foo
to the standard library! As a template type, the bastards! Now your program
won't compile, and you have a headache.

Because of namespaces this cannot happen. The names you choose and the names
the standards committee choose are safely separated by namespaces. That is
of course unless you write

using namespace std;

at the top of all your code. Now it can happen again, but its YOUR fault.

john
Jul 19 '05 #5
Well, first of all, the ".h"-style headers are now obsolete,
and that's a good reason for not using them. They're like
the words "thou" and "thy" -- you could use them and be
understood, but you'll do better not using them. :)

When you use the "new" headers (those without the ".h"),
you get their variables wrapped in a namespace -- believe it
or not, that's for your convenience, as John had pointed out
a post earlier. The purpose of the namespace is to let you
use *your* own variables without having to make sure there's
no such variable already in some of the headers you've included.

But to get to the variables in a namespace you have to
do either of these:

a) type the namespace qualifier everywhere, like in:
std::cout << "Testing 123" << std::endl

b) explicitly bring some variables into scope, like in:
using std::string;
which will then allow you to skip the corresponding
namespace identifiers

c) explicitly bring the whole namespace into scope, like in
using namespace std;
which is a good solution for you

d) be stubborn and stick to ".h" headers which could be
so old they've never seen a thing like a namespace,
which is not a good idea, just like using "thou" isn't.

Anyway, having to type "using namespace std;" once
per source file isn't a great disadvantage, is it?

HTH,
- J.
Jul 19 '05 #6
On Mon, 01 Sep 2003 17:22:58 GMT, da Vinci <bl***@blank.com> wrote:
Thanks for the knowledge so far!

But now I have something else troubling me about this. Everyone has
pretty much pointed out that using the .h form in the include
statement is not standard c++ and should not be used.

What advantage do you get over using the "namespace std;" line? What
advantage would you get using the "using std::cout;". I mean the
advantage over using just the .h under the include statement....
The advantage of "using namespace std;" is that you don't have to do any
more typing. The disadvantage is that everything in the namespace gets
pulled into the global namespace. Doing this in a source file is not a
big problem, but doing it in a header can be disastrous.

The advantage of "using std::cout;" is that you can then just type
"cout" instead of "std::cout" whenever you want to use it. The
disadvantage is that there are generally more such lines required, one
for each thing you want to import into your local namespace.
It seems to me that adding all of these lines of code for namespace
inclusions and such is a waste of time when you can just use the .h in
the include line.
The technique of using the .h header may or may not work. The .h
headers date from before the ANSI standard, and at that time each
vendor's implementation was different. Now, some vendors iostream.h
file might look like this:

#include <iostream>
using namespace std;

while others may still be the same as they were in the bad old days.
The only way to be sure of what you are getting is to abandon the .h
versions and use the new, standard, "no .h" include files.

So, yes, you waste about 5 seconds each time you type "using namespace
std;" in a source file, but you will likely get all that back and then
some the first time you try to compile your source with a different
compiler, or the first time you run into a situation where your
compiler's .h differs from the ANSI header which is documented in all
the good books on the subject.
I know that ANSI would not change something like this, make it harder
or more bulky in the code, for no reason. So, what was their reason?
ANSI didn't really change this, it's just that what they chose to
standardise didn't quite match what anyone had implemented. To avoid
the large problem of breaking existing code, they standardised on a new
file structure and a new namespace that nobody was using, and let
compiler vendors decide for themselves what path they would take with
their existing and now-obsolete .h headers.
Does this make programs compatible across many different platforms or
compilers? Does it save on the memory useage of the program?
ANSI standardisation has made programs compatible across many different
platforms, and this particular choice made sure that people know exactly
what they are getting when they include a standard header, rather than
using a header which is now standard but used to vary and you're never
sure which version you have.
Again, I am just a beginner. I like to know the whole story instead of
just "Do it this way because ANSI says so!" answer. I like having the
indepth, behind the sense, kind of knowledge.


Hope this helps! Of course, this is all a big simplification of the
ANSI process, and there were other good reasons for using the std
namespace, but that's outside the scope of your original question.

--
Greg Schmidt (gr***@trawna.com)
Trawna Publications (http://www.trawna.com/)
Jul 19 '05 #7
On Mon, 1 Sep 2003 22:26:47 +0200, "Jacek Dziedzic"
<ja***********@janowo.net> wrote:

d) be stubborn and stick to ".h" headers which could be
so old they've never seen a thing like a namespace,
which is not a good idea, just like using "thou" isn't.
<LAUGH>
Anyway, having to type "using namespace std;" once
per source file isn't a great disadvantage, is it?


No, not at all. From previous posts and this one, I am really
beginning to understand the reasons not to use the .h that the teacher
just couldn't explain. She doesn't care how we do it, as long as it
compiles and works. I want to do it the right way because bad habits
are hard to break when your looking for a computer engineering job.
Learn it right the first time is what I say.

I went through and changed all of my code that I have writen so far to
reflect the "using namespace std;" addition. It just threw me through
a loop trying to figure out that <math.h> is now <cmath> and not just
<math>.

DV
Jul 19 '05 #8
On Mon, 01 Sep 2003 23:56:40 GMT, Greg Schmidt <gr***@trawna.com>
wrote:
Hope this helps! Of course, this is all a big simplification of the
ANSI process, and there were other good reasons for using the std
namespace, but that's outside the scope of your original question.


Yes, it has been very valuable.

From the beginning it was a matter of me seeing three diferent ways to
do it, from three diferent sources, and none of them state that one
way is better than another. Then everyone I asked (outside of this
newsgroup) couldn't explain which was the better or "more correct" way
of doing it.

Again, appreciate the clarification on the matter. (same to all of
those who have responded.)
Jul 19 '05 #9
On Mon, 1 Sep 2003 20:16:29 +0100, "John Harrison"
<jo*************@hotmail.com> wrote:

The purpose of namespaces is to avoid name collisions in code when program
are written by many authors (I think I said that already).


Ah, ok, now I think I am starting to understand what you meant. I had
seen that you wrote that and meant to write a comment on it in my last
response but forgot. I just didnt understand what name collisions
meant.

My programming experiance was BASIC as a kid, PASCAL in high school,
and I self taught myself C++ when I was in the Marines (nothing else
to do when on deployment!). But because I self taught myself, I didn't
get alot of the intricate details on the laungage. Now that I am in
college, I am trying to pick all of that stuff up.

Thanks for explaining that out.

DV
Jul 19 '05 #10
In article <32********************************@4ax.com>,
da Vinci <bl***@blank.com> wrote:
I went through and changed all of my code that I have writen so far to
reflect the "using namespace std;" addition. It just threw me through
a loop trying to figure out that <math.h> is now <cmath> and not just
<math>.


This is because <math.h> got imported from the C library, so (like any
other C library header) it can be used either as <foo.h> (which puts the
names in the global namespace) or as <cfoo> (which puts them in namespace
std). (Unlike the pre-standard forms of C++ headers, it's perfectly valid
(though not such a good thing in new code) to leave <math.h> (or any other
C header) as is and use the names from the global namespace instead.)

Knowing about things like this is also a Good Thing if you want to be
able to write C as well as C++, since knowing what's the same between
them lets you spend more time learning the differences.
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
[Y]ou can write bad code that just barely works in both languages, or good
code that works only in one language -- so pick one, and write good code in
that language. --Chris Torek in comp.lang.c (crossposted to comp.lang.c++)
Jul 19 '05 #11

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

Similar topics

2
by: Christian Seberino | last post by:
I have a program that has Python and C code. I added Extension stuff to setup.py and all .c and .h files get into tarball fine. (I have something like Extension("foo", glob.glob("bar/*.") ) ...
16
by: ^_^ | last post by:
conversion from: a="a"; to a=0x????; If there are many unicode strings to convert, how can I do batch-conversion?
6
by: Jacek Dziedzic | last post by:
Hello! First of all please forgive me for not posting a compilable snippet, but rather a simplified piece of code with the unimportant details left out. Let's say I have two classes...
4
by: Seamus M | last post by:
It seems when I use the getLine() method of the Exception class, it returns the line number of the "throw new Exception" statement and not the line number where the error actually occured in the...
7
by: David Elliott | last post by:
I have created an application that will dynamically load other DLLs (plugins). The new plugin is a winform with an embedded IE Browser. I am wanting to have the form to run in its own thread....
5
by: matthewtec | last post by:
This may be something that is painfully easy and I'm just missing, or it could be something that I should learn a bit more in order to do. But, if I have a simple windows form application, with...
6
by: Kbalz | last post by:
Trying to run a simple query on my dataset the SQL statement looks like this: select * from person where id in (@p) When using more than one id in the "in statement", I get...
5
by: mark_aok | last post by:
Hi all, I have a situation where I have a split database. At the back end, I need to - create a new table (I will call it newTable) with the exact fields, and relationships as another table...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.