473,659 Members | 3,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2286
"da Vinci" <bl***@blank.co m> wrote in message
news:pv******** *************** *********@4ax.c om...
| 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%******** *********@newsr ead4.news.pas.e arthlink.net...
"da Vinci" <bl***@blank.co m> 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.co m> wrote in message
news:qp******** *************** *********@4ax.c om...
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.co m> 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.c om)
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.c om>
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

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

Similar topics

2
2305
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/*.") ) When I try to INSTALL the package it says header (.h) file is an unknown type. How fix this???
16
637
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
2443
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 'box_shape' and 'cylinder_shape' that derive from a common base class 'shape', more or less like this: namespace Shapes {
4
2791
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 source file. Am I doing something wrong? - Seamus
7
3985
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. This would allow for other plugins and the main application to be free to do other work. I have written a little TestDriver for the plugin and am having some difficulty. If I don't use threads everything works just fine. If I do use threads, upon...
5
1122
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 just one form with basic buttons, labels, etc, . At a point in this little app I want the user to be able to 'start over'. I'm wondering two things: 1) Can I do this by somehow calling the original Form1_Load ?
6
1840
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 nothing! Using Visual Studio interface, there is the Execute Query button in Query Builder. Here I can set @p = 1, and the test execute returns the row where id = 1.. and I can do the same for @p = 2.
5
1890
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 (let's call it oldTable, and I need to copy everything EXCEPT the data). - Then I need to delete oldTable, and rename newTable 'oldTable'
0
8747
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
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
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.