C++ for a thirteen year old

Pages: 12345
std::ifstream in ("in.txt") as the condition in a selection statement is not an expression, it is still an initialised declaration of a block-scope name.

Selection statements choose one of several flows of control.

selection-statement:
if ( condition ) statement
if ( condition ) statement else statement
switch ( condition ) statement

condition:
expression
attribute-specifier-seq opt decl-specifier-seq declarator = initializer-clause
attribute-specifier-seq opt decl-specifier-seq declarator braced-init-list
...
The value of a condition that is an initialized declaration in a statement other than a switch statement is the value of the declared variable contextually converted to bool (Clause 4). If that conversion is ill-formed, the program is ill-formed. The value of a condition that is an initialized declaration in a switch statement is the value of the declared variable if it has integral or enumeration type, or of that variable implicitly converted to integral or enumeration type otherwise.

The value of a condition that is an expression is is the value of the expression, contextually converted to bool for statements other than switch; if that conversion is ill-formed, the program is ill-formed.
....
JL, earlier we were discussing using intellisense in PuTTy..Can you give me a link to where I can learn how to implement it on the putty server I connect to for my school? Right now I currently compile all my errors using VS then use Filezilla to xfer all of my files to PuTTy..But if there is a intellisense for PuTTy then why should I waste my time with VS? I'm just trying to figure out how to install a intellisense for my PuTTy using the pico editor. Any good links?
@JLBorges do I need to know about this selection statement thing you guys are discussing?
> do I need to know about this selection statement thing you guys are discussing?

No. Not right now.
You will come across selection statements later, after expressions.

My post was a response to this particular post: http://www.cplusplus.com/forum/beginner/157585/2/#msg809410
It is not something you should be concerned about right now.


> JL, earlier we were discussing using intellisense in PuTTy

I'm not aware of that particular discussion.

There is GCCSense http://cx4a.org/software/gccsense/

And there are a few code completion plugins for vim:
http://www.vim.org/scripts/script.php?script_id=3302
For IDE/Compiler, i'll recommend Visual Stduio 13 with increased warning level/strictness.
@shadowCODE I'm already using Code::Blocks with MinGW. Maybe I'll look into VS13 once I'm a bit more experienced.

@JLBorges Okay, about to reach the end of the chapter. The last section is about writing your own header files. Firstly, I don't get how the entire header thing works. What is the difference between a header and a class?

We know from Section 1.5(p. 20)that ordinarily class definitions go into a header file. In this section we'll see how to define a header file for the Sales_item class.

In fact, C++ programs use headers to contain more than class definitions. Recall that every name must be declared or defined before it is used. The programs we've written so far handle this requirement by putting all their code into a single file. As long as each entity precedes the code that uses it, this strategy works. However, few programs are so simple that they can be written in a single file. Programs made up of multiple files need a way to link the use of a name and its declaration. In C++ that is done through header files.

To allow programs to be broken up into logical parts, C++ supports what is commonly known as separate compilation. Separate compilation lets us compose a program from several files. To support separate compilation, we'll put the definition of Sales_item in a header file. The member functions for Sales_item, which we'll define in Section 7.7 (p. 258), will go in a separate source file. Functions such as main that use Sales_item objects are in other source files. Each of the source files that use Sales_item must include our Sales_item.h header file.


>To support separate compilation, we'll put the definition of Sales_item in a header file. The member functions for Sales_item, which we'll define in Section 7.7 (p. 258), will go in a separate source file.
Why can't they be in a single file? Why would you even need to use separate compilation?

A header provides a centralized location for related declarations. Headers normally contain class definitions, extern variable declarations, and function declarations, about which we'll learn in Section 7.4 (p. 251). Files that use or define these entities include the appropriate header(s).

Proper use of header files can provide two benefits: All files are guaranteed to use the same declaration for a given entity; and should a declaration require change, only the header needs to be updated.


>All files are guaranteed to use the same declaration for a given entity
What does this mean?

Headers Are for Declarations, Not Definitions
When designing a header it is essential to remember the difference between definitions, which may only occur once, and declarations, which may occur multiple times (Section 2.3.5, p. 52). The following statements are definitions and therefore should not appear in a header:


>Is there a reason no definitions are allowed?

So far, this is all I've read. Will continue reading after a short break, only 4 pages left.
> What is the difference between a header and a class?

A header file is an actual file which contains C++ code; it is a physical component.
A class is a logical component; a user-defined type (loosely akin to what it is in Java).
C++ does not impose restrictions on how the code should be distributed across physical files; it is possible, but not a requirement, that a header file contains the definition of a single class and nothing else.


> Why can't they be in a single file?

They can be in a single file.


> Why would you even need to use separate compilation?

A single program may consist of many different parts. In real life, these separate parts are often written by different people. These separate parts (translation units) can be compiled, and errors in them can be corrected independently. After which they can be linked together to form a complete executable program. This allows us to eliminate the complexity that would arise from putting everything into a single file.


> All files are guaranteed to use the same declaration for a given entity
> What does this mean?

When many files include the same header file, all of them get the same declarations that are in the header file. For instance, if a header file contains the declaration the the variable maximum_temperature is of type double, in every translation unit that includes the header, maximum_temperature is declared to be of type double.

By this, we avoid silly errors like a programmer making a typing mistake - say, maximmum_temperture or incorrectly declaring maximum_temperature to be of type float instead of a double.


> Is there a reason no definitions are allowed?

One of the fundamental rules of C++ is the 'One-Definition-Rule'.
http://en.cppreference.com/w/cpp/language/definition

This would become clearer when you reach the indicated (Section 2.3.5, p. 52) in your book.
I got all of them except the last one. The 'One-Definition-Rule' has been covered, and I understood it, but they say NO definitions (except objects that the compiler needs to know about - const objects, classes and something called "inline functions" that hasn't been covered yet.

So headers contain classes and there is nothing called a class file, right?
Last edited on
Headers may contain class definitions.
They may also contain other things; for instance the declaration of a non-member function.

No, there is nothing called a 'class file'.
Alright.

>They can be in a single file.
If everything can be contained in one file, what is the reason not to? Is it so that multiple programmers can work independently? Did the book have that idea in mind or was it something else?
@JLBorges Just finished the chapter. Preprocessor directive was a breeze. Looks like I'm actually getting somewhere.

How am I doing so far?
> If everything can be contained in one file, what is the reason not to?
> Is it so that multiple programmers can work independently?

That is one reason. Even if there was a only a single programmer, it is easier to reason about, and make changes to 200 lines of code in one file, without having to simultaneously worry about the other 1800 lines of code which are in other files.


> How am I doing so far?

You would know that better than I do; you seem to be doing fine so far.
Hi,

Just wanted to make sure that you were aware of all the documentation on this site. At the top left of this page you can see Information, Tutorials, Reference and Articles.

I was sure there was an article by Duoas that covered most everything a beginner might need to know (on top of the site tutorial), but I couldn't find it.

Regards
@TheIdeasMan Thanks for the info, will have a look at it.

@JLBorges @TheIdeasMan So I just started the next chapter and it discusses the using std::cout code. Now it says that from that point in the book it will use this "using std::name" code to shorten the length of the code. But I have heard a lot of people saying that the using keyword is bad. Is what I heard true? Can I have the reason for it?

Also, JLBorges, this chapter discusses "bitsets" towards the end. When I come to that part, would it be an appropriate time to learn about all the bits and bytes things discussed in the previous chapter?

Regards,
GreatBlitz.
using namespace xyz; is bad, especially in global scope. using specific::name; is fine in a local scope.
What about using them in the global scope?
Hi,

There are several uses of the using keyword.

using namespace std; // or any other namepace Is really bad.

This brings in the entire namespace, and causes naming conflicts, which defeats the purpose of namespaces!!


using std::cout; // fine, or anything similar, can now use cout unqualified

With std::cout and similar examples, you will notice that experts and any partially decent coders always do that, rather than using std::cout;. Have a look at any of JLBorges code.


using bg = boost::geometry; // fine, now we can use bg elsewhere. This is an alias

This has more uses than typedef.

http://en.cppreference.com/w/cpp/language/type_alias



Note that it is a good idea to put your own code into a namespace/s of it's own. Look it up in the reference.

:+)
Last edited on
@TheIdeasMan: C++ uses double-colons for the scope-resolution-operator, not forward slashes.
ah yes, silly me :+)
> this chapter discusses "bitsets" towards the end. When I come to that part, would it be an appropriate time
> to learn about all the bits and bytes things discussed in the previous chapter?

Yes.

Since skipping this seems to leave you somewhat uncertain, let us start looking at it right now:

In the view of C++, storage (memory) is divided into distinct pieces called 'bytes'. Byte is a synonym for one of these three character types - char, signed char, unsigned char - that C++ supports. Each byte has a distinct location in memory, called its 'address'; somewhat like houses in a street can be given distinct (consecutive) numbers.

A byte is made up of a number of 'bits'. A bit is a switch that can be in one of two positions, which we can think of in many ways - 'on'/'off', 'flip'/'flop', 'up'/'down', 'left'/'right', 'true'/'false' or 1/0 . Thinking of a bit as either one or zero is conventional; this allows us to think of a number of contiguous bits together forming a value (a number) which is greater than one. C++ requires that a byte must consist of at least eight bits; in practice, in every C++ implementation, a byte is an octet of eight bits.

Each object in memory takes up a sequence of some integral number of bytes. For instance, if sizeof(int) is 4, an object of type int occupies four contiguous bytes in memory. The address of an object of type int is the address of the byte, starting at which the next four contiguous bytes form the object representation of an integer.

Since char, signed char and unsigned char are synonyms for a byte, by definition, they have a size of one byte. That is: sizeof(char) == 1. All bits in the byte participate in determining the value of the character.

Since every object has an address in memory, which is the address of a byte, the size of an object is an integral number of bytes that its object representation takes up. For instance, an object of type bool can have only one of two possible well-defined values; but the object has a specific address in memory; sizeof(bool) must be at least one byte. Some subset of bits (typically one bit) in the object representation of a bool make up its value representation.

Have you understood this much? Do you have any questions?
Pages: 12345