Sinisterly
C++ tutorial part I - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C)
+--- Thread: C++ tutorial part I (/Thread-C-tutorial-part-I)



C++ tutorial part I - chipp - 03-30-2012

because there's many tutorial for C++, i wanna make my own tutorial Biggrin (nice try, dude...)

ok, since C++ is either known as good programming language (read as: great) and, for that, is (one of) hacker's (best) choice, so it is a good point to learn this programming language.
btw, critics, opinions, and additions comes for this tutorial are perfectly welcomed (since i'm not really a master of C++)...

let's begin...

first of all, for you if you wanna know about: what is programming is, you can just go straight forward to this:

http://adf.ly/6qlhG

actually i never read it though, i define my own programming definition Biggrin

right, let's start our first C++ code (program), here it is:

Code:
//the universal starting program ever, "hello, world!" #include <iostream> using namespace std; int main() { cout << "hello, world!" << endl; return 0; }

this is the most common program in every start of any programming language, "hello, world!" program.

Code:
//the universal starting program ever, "hello, world!"
the first line is a line of comment. we can write anything after the "//" marks. that note the compiler to ignore it and what lies after it (i.e. the sentence). it's useful to mark or commenting in certain part of the code, for remind us for what purpose that line are intended to.

Code:
#include <iostream> using namespace std;
the second and third line are code for including the iostream library and using the std namespace to use the input and output stream from iostream (am i right in this part?). the hash "#" is the preprocessor directives and "include" that follows it is the code for include a library. except directives, all the statements in C++ is ended with semicolon ";".

the fourth line, is... a blank space... i bet you all already know... congratulations...haha... ok, seriously, it's just for sake of neat of the source code and doesn't have an effect for the compiler...

the fifth line is the main function. it's the main function of our program, and it's where our program runs. and that the first function being called by the program when it runs. by its syntax, we can see that main is returning an int which is an integer data type of C++. and our main, this time, doesn't takes arguments. we'll learn about arguments, return type, data type later... the "{" brace is the symbol of the beginning of a block of statements. and in this case, the beginning of our function's block of statements (functions always have to begin and ended with braces too)

Code:
cout << "hello, world!" << endl;
this line prints a "hello, world!" plus a newline at the screen. cout is the print statement. and the operator << (shift left) will extract the following sentences or data (e.g. variables, etc) through cout. endl statement is the newline statement. newline can be done with one of the escape characters, i.e. '\n'. but as far as i know, endl is a better choice, because it flushes the output buffer and then make a newline on the screen (CMIIW)

Code:
return 0;
the returned value of the program, it exits with the value of 0 (zero) indicates the program is successfully exited. (i don't really know what it does)

the closing brace "}" is the end of the function, in this case, main function.

so, that's the end of my first C++'s tutorial. it's quite simple though, but, for the newbies for programming world, you all now can see more details of how a program flows...

please CMIIW


RE: C++ tutorial part I - Frooxius - 03-30-2012

A few pointers:

Pre-processor directives aren't technically part of the C/C++ language, so when you write an
Code:
#include <iostream>
the C++ compiler doesn't really even see this part, instead, the pre-processor takes the contents of the file iostream and puts them at the place of directive and then passes the result to the compiler.

cout isn't a print statement, it's an object from the standard library, that's linked to the stdout stream, which is linked to the console output by standard. It's because C++ is object oriented language.

The operator << isn't a shift operator in this case (it doesn't shift anything really), it's an overloaded operator for the ostream class (maybe not exactly ostream, I would have to check docs, but it doesn't matter really) - the type of object cout is. C++ allows you to overload operators in relation to classes - change their meanings for variables that hold objects of certain glasses and it's basically a nicer way to call a function, so if you write:

Code:
cout << "Something";
Then what happens behind the scenes is, that it basically calls this method of the cout object:
Code:
ostream operator<<(const char *message_to_print);
Which handles the printing and returns the cout, so it can be used in chain statements and expressions.

It's a almost the same as calling printf("Something"); in C, except that it's more fancy and it's object oriented (it's called in relation to a specific object). Think of using cout << "Something"; as more fancy way of calling a function, like cout.printf("Something");
There not really any data "extraction" or whatever you were talking about.

Also, endl is iostream manipulator, if you pass this object to cout (and others), it somehow manipulates the input or output stream. For example
Code:
cout << hex << 15;
Prints the number in hexadecimal. The hex is another IO stream manipulator, just like endl.



RE: C++ tutorial part I - chipp - 03-31-2012

about "statement" and "extraction" i just lack of words (i don't know what words to described my explanation, so it wasn't (really) intend to C++'s "statement" and "extraction" haha..)


RE: C++ tutorial part I - Frooxius - 03-31-2012

That part of explanation was completely wrong to begin with, so even different words wouldn't really make it much more correct.

What it does, is that it calls the overloaded operator method of the cout object and passes it the right operand. However, it's possible that it's not defined for the class itself, but that the overloaded operator is defined as a friend, so the cout is passed to the method as well as an argument, otherwise it's passed implicitly and accessible via this pointer.



RE: C++ tutorial part I - CasaNova - 04-14-2012

you might explain everything much better but for the first tut not bad..


RE: C++ tutorial part I - CasaNova - 04-14-2012

you might explain everything much better but for the first tut not bad..