New guy Questions

Hey all, Im new to the furum and new to C++ . I decided that if I was to be serious about learning this programming language I should get input, and ask questions to real people, instead of just trying to look this stuff up.

Anyhoo, I have a few right now, and they are bout small details about The language:

When typing in operators, is it always like in english, where there is a space between words everytime? Or do you sometimes not neet to use spaces for the code on the left side of the operator, or on the code on the right side of the operator on that line of code?

so like this: # include or int main () or cout << "Hi everyone" << endl;
or like this: #include or intmain()... etc.

It does seem to matter for the :: scope resolution operator, but fir other ones like += or % seem to be typed with spaces.

Also, Im looking at the reasons why I have to type in these specific parts of code everytime for example

# include <iostream>

int main ()

{
std::cout << "Game Over!" << std::endl;
return 0;
}

Or like this:


#include <iostream>

int main ()

using std::cout;
using std::endl;


{

cout << "Game over!" << endl;
return 0;

}


So I use the :: in the first one, Im not sure what the scope resolution operator does. I have a vague idea, but not very clear.

With the using directive, It gave me a idea of how that worked, tell me If Im wrong:

English grammer: " Timmy said Hi to Suzy."



C++ grammer: On this planet, called earth, there is a section of the surface, on that section there is two people, one named "Timmy", and one named "Suzy" And Just now "Timmy" said "Hi" to "Suzy".

..... Assuming "Hi" has already been defined :)


Is my analogy close to how it works?
Is this how the using directive, and how C++ in general is programmed?

Also, I was typing in some code learning how to print out strings, or "string literals"

so it looked like this:

# include <iostream>

int main ()

{
std::cout << "Game Over!" << std::endl;
return 0;
}

And I noticed that the arrows or << those things were pointing towards std::cout , or more specifically towards cout.

The impression that I got, is that that means the computer reads this line of code from right to left???

so it goes this object "Game Over!" is being put into cout??

Is "Game Over!" an object? as more complicated code might have another section
of code labelled

<< "somecode"<<

and it feeds the entire code
labelled "somecode" into the std::cout, or some other thing like std::cout?
Or better yet, into <iostream> or some thing called <blockofcode> or whatever?




Actually, does the compiler only take cout, and not all of <iostream> ??

or do you need all of <iostream> to be working, in order for cout to work??

does <iostream> and/or cout do more than just print stuff on the screen?



Thanks, a ton of questions, but these make me wonder, so why not??

MK 4.
haha man your going to need to read some intro tutorials to answer all those questions..

start with something maybe like

http://www.cprogramming.com/tutorial/lesson1.html

then get a good book i always recommend Stanly B Lippman C++ Primer - i think it has a good layout

as for the <<
double carrot is just an operator like + or - or any other operator
to explain how operators work you need to know how functions work
to know how functions work you need to know some basics of the language
to know the basics you'll have to do some reading..

people here would love to recommend some other good starting tutorials im sure
closed account (zb0S216C)
MK 4 wrote:
"When typing in operators, is it always like in english, where there is a space between words everytime? Or do you sometimes not neet to use spaces for the code on the left side of the operator, or on the code on the right side of the operator on that line of code?"

The use of white-space depends on the context. The majority of the time, white-space is ignored, but this isn't always the case. Identifiers, such as variable/object names, cannot contain spaces, but they can contain numbers, underscores and upper-/lower-case letters. For instance:

In "int main", "int" is the type and "main" is the identifier. If the space were to be ignored, the compiler would see "intmain" which is something completely different.

See here: http://en.wikipedia.org/wiki/Naming_convention_(programming)#Common_elements

MK 4 wrote:
"It does seem to matter for the :: scope resolution operator, but fir other ones like += or % seem to be typed with spaces."

The Scope Resolution Operator (SRO for short) is a completely different operator in comparison to arithmetic operators. If you don't know, the SRO is used to qualify an identifier with a scope as the name implies. Because you're new to C++, I'd recommend not getting too involved with the SRO just yet. Also, white-space between arithmetic operators is usually ignored.

MK 4 wrote:
"So I use the :: in the first one, Im not sure what the scope resolution operator does. I have a vague idea, but not very clear."

As I said above, the SRO is used to qualify an identifier with a scope. "std" is the standard name-space which contains C++'s library. If an identifier is proceeded with an SRO it means that the identifier is nested. In lay terms, the SRO tells the compiler that the identifier on the right-hand side of the SRO is declared within the scope on the left-hand side of the SRO. For example: "Australia::Steve" can be worded as: "Steve ::(lives in) Australia".

MK 4 wrote:
"With the using directive, It gave me a idea of how that worked, tell me If Im wrong:

English grammer: " Timmy said Hi to Suzy."
"

Again, the meaning of "using" depends on the context. "using std::cout" tells the compiler to automatically qualify any references to "cout" with "std". However, this can cause ambiguities so it's best avoided for now until you understand why.

"std::cout" sends a message to the standard output stream. Without confusing you, "cout" is used to display text on the screen; nothing more, nothing less. "std::cin" is used to get data from the user via keyboard.

MK 4 wrote:
"The impression that I got, is that that means the computer reads this line of code from right to left???"

No. In some contexts ("context" appears a lot in technical writing), however, the compiler does read code from right-to-left, but left-to-right is the norm. The "<<" operator has different meanings, specifically "multiply", "shift-left" and "output to...". In more advanced C++, this operator can be overloaded so that its meaning changes in different scenarios.

MK 4 wrote:
"so it goes this object "Game Over!" is being put into cout??"

That's how programmers read it, but not the compiler. Most of the time, statements make more sense when they're read backwards. For instance: from right-to-left, "Y.X" can be read as "X is a member of Y".

MK 4 wrote:
"and it feeds the entire code
labelled "somecode" into the std::cout, or some other thing like std::cout?
"

No. See this: http://www.cplusplus.com/doc/tutorial/basic_io/

MK 4 wrote:
"Actually, does the compiler only take cout, and not all of <iostream> ??"

No. "include" directives take 1 operand and that's a file name. The directive is replaced with the entire contents of the file that it included. In other words, "#include <iostream>" is replaced with the contents of the "iostream" file within the compiler's directory.

See this for more information on everything: http://www.cplusplus.com/doc/tutorial/
Last edited on
closed account (o3hC5Di1)
Hi there,

Welcome to the forums!

There's actually a tutorial on this site: http://cplusplus.com/doc/tutorial/

MK 4 wrote:
When typing in operators, is it always like in english, where there is a space between words everytime?


As far as I'm aware, C++ is ignorant of whitespace. By that I mean you can write whitespace in between operators, or use no whitespace at all, the compiler doesn't mind. It's actually one of the reasons why you are required to write a semicolon at the end of every statement, that let's the compiler know the statement was finished, and so it can afford not to care about whitespace. Check here for some minor exceptions to this rule:
http://www.learncpp.com/cpp-tutorial/16-whitespace-and-basic-formatting/

MK 4 wrote:
Is my analogy close to how it works?
Is this how the using directive, and how C++ in general is programmed?


I think your analogy is a bit far fetched. Regarding the using directive and scope resolution operator here is the gist: In C++ we have the ability to use namespaces, which are basically groupings for names. They allow you the opportunity to force others to explicitly have to say which variable or function they are requesting, for instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
namespace Cat {
    void make_noise() { std::cout << "mew"; }
}

namespace Dog {
    void make_noise() { std::cout << "woof"; }
}

make_noise(); // which one are you talking about?
Cat::make_noise();  //oh, you want the make_noise function of the Cat namespace

using namespace Dog;  //alright, I'll look for functions in the Dog namespace too
make_noise(); //aha! Dog namespace has a make_noise() function, I'll call that for you. 


In the real world this is often used to group variables / functions / classes / that are part of a common functionality.


MK 4 wrote:
The impression that I got, is that that means the computer reads this line of code from right to left??? ... << "somecode"<< and it feeds the entire code labelled "somecode" into the std::cout, or some other thing like std::cout?


It actually reads it left to right, but to understand that you need to understand more about how operators / functions work. operator << is actually called the "insertion operator", so you read it right when you say that the string literal is inserted into std::cout.

MK 4 wrote:
Actually, does the compiler only take cout, and not all of <iostream> ??
or do you need all of <iostream> to be working, in order for cout to work??
does <iostream> and/or cout do more than just print stuff on the screen?


If you only use std::cout, you could also #include <ostream> , which will only give you the necessary headers for output streams. To quote the documentation on this website:

cout is an object of class ostream that represents the standard output stream


Streams can do much more than just output to the screen or read from the keyboard, although to beginners that is their most common use.

Hope that helps.

All the best,
NwN

Edit: Seems I was way too slow once again!
Last edited on
Thanks for all the helpful input from everyone. All your reponses have clarified things a bit. Ill be sure to check out the tutorials to learn more.

MK 4.
I would just like to suggest that you take a look at thenewboston tutorials, they are informative and entertaining, it will answer alot of you questions.

http://www.youtube.com/course?list=ECAE85DE8440AA6B83

also as guyz said, the cplusplus tutorial is very good, use it at least as good reference.

Hope it helps
Topic archived. No new replies allowed.