"Accelerated C++" beginner question.

T

TonyJeffs

Great book - I like the way that unlike other books, AC++ explains as
much as possible about every piece of code discussed, so I'm not left
thinking, "well...OK... I get line 12, but I wonder what the rest of
it means...".

Still, I have some questions, that are frustrating me:-
Grateful for any comments.

1. What is the difference between
#include <iostream> // (or any include file) which is used in this
book, and
#include <iostream.h> // which is used in other books I've looked at.

2. Does the absence of the '.h' have something to do with the need
for std:: before cout ?

3. std, the book esplains, is a namesace. But what is a namespace,
particularly in this case?. Is it something I can open and look at in
Notepad?

.....................................
I'm up to chapter 4, and relativaly comfortable with it, but these
early questions are still nagging me.

Thanks
 
P

Pelle Beckman

TonyJeffs skrev:
Great book - I like the way that unlike other books, AC++ explains as
much as possible about every piece of code discussed, so I'm not left
thinking, "well...OK... I get line 12, but I wonder what the rest of
it means...".

Still, I have some questions, that are frustrating me:-
Grateful for any comments.

1. What is the difference between
#include <iostream> // (or any include file) which is used in this
book, and
#include <iostream.h> // which is used in other books I've looked at.
#include <iostream> is the suggested way of including files
in c++ (at least for standard libraries)
#include <iostream.h> is just about the same, except most
compilers will give you a warning telling you its deprecated.
When writing your own header files - select what suits you the best.
Personally, I like .h or .hpp
2. Does the absence of the '.h' have something to do with the need
for std:: before cout ?
No.

3. std, the book esplains, is a namesace. But what is a namespace,
particularly in this case?. Is it something I can open and look at in
Notepad?
A namespace is a concept of grouping things - like functions, classes
and constants - that belong together.
Using namespaces makes your code more readable and more easily
maintained.
The standard libraries, such as iostream, use a namespace called
std - that's why you see things like 'std::cout << "blah" << std::endl;'
or 'using namespace std;'

Google it.

-- Pelle
 
S

Sharad Kala

TonyJeffs said:
Great book - I like the way that unlike other books, AC++ explains as
much as possible about every piece of code discussed, so I'm not left

I haven't read anyone saying otherwise about the book :)
thinking, "well...OK... I get line 12, but I wonder what the rest of
it means...".

Still, I have some questions, that are frustrating me:-
Grateful for any comments.

1. What is the difference between
#include <iostream> // (or any include file) which is used in this
book, and
#include <iostream.h> // which is used in other books I've looked at.

The other books you have looked at are outdated. Standard headers are
extensionless.

2. Does the absence of the '.h' have something to do with the need
for std:: before cout ?

Yes, everything inside standard headers is wrapped inside std namespace. So
cout needs to be addressed as std::cout.
3. std, the book esplains, is a namesace. But what is a namespace,
particularly in this case?. Is it something I can open and look at in
Notepad?

What have namespaces to do with Notepad ?? Namespaces allow us to group a
set of global classes, objects and/or functions under a name. This helps in
avoiding name conflicts, and redefinition errors.

namespace a {
int i; // OK
}

namespace b {
int i; // OK
}

Sharad
 
S

Sharad Kala

Pelle Beckman said:
#include <iostream> is the suggested way of including files

Not suggested, but correct way of including files in ISO C++.
in c++ (at least for standard libraries)
#include <iostream.h> is just about the same, except most
compilers will give you a warning telling you its deprecated.
When writing your own header files - select what suits you the best.
Personally, I like .h or .hpp

No.

Why ?

A namespace is a concept of grouping things - like functions, classes
and constants - that belong together.
Using namespaces makes your code more readable and more easily
maintained.

Is that the only goal of namespaces ?

Sharad
 
I

Ioannis Vranos

TonyJeffs said:
Great book - I like the way that unlike other books, AC++ explains as
much as possible about every piece of code discussed, so I'm not left
thinking, "well...OK... I get line 12, but I wonder what the rest of
it means...".

Still, I have some questions, that are frustrating me:-
Grateful for any comments.

1. What is the difference between
#include <iostream> // (or any include file) which is used in this


This is valid C++.

book, and
#include <iostream.h> // which is used in other books I've looked at.


This is not valid C++. It was used before the final C++ standard.


2. Does the absence of the '.h' have something to do with the need
for std:: before cout ?
No.



3. std, the book esplains, is a namesace. But what is a namespace,
particularly in this case?. Is it something I can open and look at in
Notepad?


We can say that namespace is a scope for grouping facilities.
 
A

Andrew Koenig

1. What is the difference between
#include <iostream> // (or any include file) which is used in this
book, and
#include <iostream.h> // which is used in other books I've looked at.

2. Does the absence of the '.h' have something to do with the need
for std:: before cout ?
Yes.

3. std, the book esplains, is a namesace. But what is a namespace,
particularly in this case?. Is it something I can open and look at in
Notepad?

A namespace is something that is part of the C++ language. It is not a data
structure that you can access directly. A namespace has a name and contains
a (possibly empty) collection of names. When you want to use one of the
names that a namespace contains, you can refer to it as x::y, where x is the
name of the namespace and y is the name that it contains.

So when you say

#include <vector>

the compiler puts the name "vector" into the namespace named "std", after
which you can refer to std::vector. Alternatively, by saying "using
std::vector;" you can cause the name "vector" to be brought into the global
namespace, after which you can refer to vector without the "std::" in front
of it.
 
A

Alf P. Steinbach

* Pelle Beckman:
TonyJeffs skrev:
#include <iostream> is the suggested way of including files
in c++ (at least for standard libraries)

No suggestion.

<iostream> is a standard header.

#include <iostream.h> is just about the same, except most
compilers will give you a warning telling you its deprecated.

_No_ compiler will tell you that.
 
T

tony_jeffs

Thanks for all the advice. As well as helping me know what's going on,
any interaction on the topic will be a big help in keeping me going
and on track!

My next Question (sec 4.1.2 in case anyone wants to look at the
book):

const vector<double>& hw =homework)

I follow that hw is a synonym for homework.
But it seems to me that '&' does not mean 'the address of' in this
case. It means something a bit different. Am I correct? What phrase
could I use to explain the &?
Does this & mean 'by reference'?

(You'll see my brain's a bit cluttered with things I've half read and
half understood in the past!!)

Thanks again,

Tony
 
P

Peter Kragh

tony_jeffs said:
My next Question (sec 4.1.2 in case anyone wants to look at the
book):

const vector<double>& hw =homework)

I follow that hw is a synonym for homework.
But it seems to me that '&' does not mean 'the address of' in this
case. It means something a bit different. Am I correct? What phrase
could I use to explain the &?
Does this & mean 'by reference'?

Not quite. In this context & means that hw is a reference. Look up
references in your book.

To quote another great C++ book (C++ Primer, Lippman & Lajoie): "A
reference, sometimes referred to as an alias, serves as an alternative
name for an object. A reference allows for the indirect manipulation of
an object in a manner similar to the use of a pointer but without
requiring use of pointer syntax".

But what does that mean in real life? Lets look at an example:

Using a pointer:

void GetName(std::string* name)
{
// Check if name is valid
if(!name)
{
throw std::runtime_error("Invalid name pointer");
}

// Do something to get a name
...

*name = "something";
}

Using a reference:

void GetName(std::string& name)
{
// Do something to get a name
...

name = "something";
}

By using a reference parameter instead of a pointer parameter, the
compiler will make sure that you get a valid object you can use
(although there are pitfalls!). If you use a pointer you need to do the
checking by yourself.

There are other ways of using a reference type. Compare these two functions:

void OutputName(std::string name)
{
....
}

void OutputName(const std::string& name)
{
....
}

What's the difference? Calling the first function will pass a copy of
the name (which of course means that a copy will be made). Calling the
second function will only pass a reference to the name (much cheaper,
especially when dealing with long strings, vectors, etc.).

HTH.
 
A

Andrew Koenig

_No_ compiler will tell you that.

Not true. If under gcc 3.3.3 I compile

#include <iostream.h>

I get the following diagnostic message:

In file included from /usr/include/c++/3.3.3/backward/iostream.h:31,
from xx.c:1:
/usr/include/c++/3.3.3/backward/backward_warning.h:32:2: warning: #warning
This file includes at least one deprecated or antiquated header. Please
consider using one of the 32 headers found in section 17.4.1.2 of the C++
standard. Examples include substituting the <X> header for the <X.h> header
for C++ includes, or <sstream> instead of the deprecated header
<strstream.h>. To disable this warning use -Wno-deprecated.
 
A

Alf P. Steinbach

* Andrew Koenig:
Not true.

Heyah, that's not true! ;-)

If under gcc 3.3.3 I compile

#include <iostream.h>

I get the following diagnostic message:

In file included from /usr/include/c++/3.3.3/backward/iostream.h:31,
from xx.c:1:
/usr/include/c++/3.3.3/backward/backward_warning.h:32:2: warning: #warning
This file includes at least one deprecated or antiquated header.

Note: "deprecated or antiquated".

gcc doesn't incorrectly tell you that it's deprecated.

It correctly tells you that it's deprecated or antiquated, and in this case,
it's antiquated.

The logic error is: special case -> generalization -> some other special
case, incompatible with the first, where the last specialization is invalid.

Cheers,

- Alf

(Sorry I missed out on Bjarne's lecture here in Oslo on Friday, I couldn't
attend, otherwise I'd been there and if enough committee people were around
I'd collect all the beers they've pledged to buy me... ;-))
 
E

enki

I have to say it is a real treat to know that you take the time help
the rest of us out.

I wish I could learn all the lessons of Accelerated C++. I learned
more in in the first 3 chapters of that book than I have from massive
C++ tomes.

I am getting into win32 programming but I hope to apply the lessons of
the book to my projects. I am trying to accuire the skills to get
hired working with code.
 
T

tony_jeffs

Andrew Koenig said:
So when you say

#include <vector>

the compiler puts the name "vector" into the namespace named "std", after
which you can refer to std::vector. Alternatively, by saying "using
std::vector;" you can cause the name "vector" to be brought into the global
namespace, after which you can refer to vector without the "std::" in front
of it.

Hi Andrew,
I'm honoured to meet the author!

So...
Any with any #include<...> header, the compiler automatically creates
the std namespace and puts things in it.
With #include <vector>, it puts "vector" in the std namespace, but
with some, eg #include<iostream> it will put more in the std
namespace - eg "cout". Am I right on the last part?

................

Thanks Peter, I'll work on your examples.
Thanks all.
Tony
 
A

Andrew Koenig

Hi Andrew,
I'm honoured to meet the author!
Thanks!

Any with any #include<...> header, the compiler automatically creates
the std namespace and puts things in it.
With #include <vector>, it puts "vector" in the std namespace, but
with some, eg #include<iostream> it will put more in the std
namespace - eg "cout". Am I right on the last part?

Right you are.
 
A

Axter

Alf said:
* Pelle Beckman:

No suggestion.

<iostream> is a standard header.



_No_ compiler will tell you that.

Not quite true.
Some compilers do tell you that it's deprecated, however, I believe
they're referring to it being deprecated by the compiler, and not by
the standards.
In other words, it's an implementation extended feature that the
implementation considers deprecated.

Since *.h STD headers where never part of the official C++ standard,
the implementation can say anything it want about it or nothing at all.
 
T

tony_jeffs

//references

I now understand the ampersands, -which was my main aim with this-
But I don't understand what's happening in the line labelled "#Line 23".
I thought myFun would incr x to 8, and "+=1" would incr myFun to 9.
What increments x to 9?

thanks
Tony

int& myFun(int& a, int& b) //int& myFun(~~~) makes it an lvalue!
{
a = a+1;
return a;
}

int main()
{
int x;
int y;
x=6;
y=1;

std::cout <<x; //x==6
myFun(x,y);
std::cout <<x; //x==7 -incr'd by myFun
int& z=myFun(x,y)+=1; // #Line 23
//x==8(No!), myFun==8, then myFun==9
std::cout <<x; //NO x==9. why?

return 0;
}
 
A

Alf P. Steinbach

* Axter:
* Alf P. Steinbach:

Not quite true.
Some compilers do tell you that it's deprecated

Example?

(Note that g++ has already been discussed and dismissed as example.)
 
C

Chris Croughton

* Pelle Beckman:

No suggestion.

<iostream> is a standard header.

<iostream.h> is not, and is only available with certain compilers.

iostream.h was the old (pre-standard) name for it.
_No_ compiler will tell you that.

You are provably wrong:

$ c++ -c /tmp/1.cc
In file included from /usr/local/gcc/lib/.../iostream.h:31,
from /tmp/1.cc:1:
/usr/local/gcc/lib/gcc/.../include/c++/3.4.3/backward/backward_warning.h:32:2:
warning: #warning This file includes at least one deprecated or antiquated
header. Please consider using one of the 32 headers found in section 17.4.1.2
of the C++ standard. Examples include substituting the <X> header for the
<X.h> header for C++ includes, or <iostream> instead of the deprecated header
<iostream.h>. To disable this warning use -Wno-deprecated.

$ c++ --version
c++ (GCC) 3.4.3
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

(Lines wrapped and paths truncated using ... to fit in a post.)

Chris C
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top