All pastes #86760 Raw Edit

Someone

public text v1 · immutable
·published 2006-07-13 05:53 UTC
rendered paste body
	Getting Started
The Simplest Of Programs:
	This is the simplest C++ program out there, all what it does, is produce the text “Hello, Object”.
<pre>
#include <iostream>
void main()
{
	cout << &#8220;Hello, Object&#8221; << endl;
}
</pre>

This really just follows the traditional starting off program that pretty much everyone does. If you&#8217;ve looked into &#8220;Starting C++&#8221; tutorials before chances are, you&#8217;ve seen a program like this. Here is the actual bare bones of the program itself:

<pre>
void main()
{
}
</pre>

Please, experiment with the programs, if you fuck the program up, chances are the worst thing will be, that it doesn&#8217;t compile. The more you mess around with the language, the more you learn. So please, mess around with everything you can in your compiler!

The Comment Line:
There are different comment lines, we will be going over them in this tutorial, and explain what they do.
The double forward slash comment characters ( // ) inform the compiler what is after the double slashes, are comments. Thus, the compiler needs to ignore them. These forms of comment lines extend to the end of the line. No closing characters are required.

<pre>
void main()
{
	cout << &#8220;Hello, Object&#8221; << endl;
} // end of main()
// sweet&#8230;. I dunno why I put this here..
</pre>

Then, there is the original C style of comments. This involves using the characters ( /* ) for declaring the beginning of the comment, and using the ( */ ) combination of characters to declare the ending of the comment. This form of comment does not expire on the end of the line, such as the double forward slash&#8217;s do. These comments will go on continuously until it has been closed. Here is an example:

<pre>
/* I love C++! */
</pre>

Now, so you can understand that the comments do not expire until you close them, I will show you another example:

<pre>
/* I love C++!
www.xoredevelopmentstudios.com is the best site ever!
Programming can be used for pretty much everything!!!! */
</pre>

However, some of you might be clean freaks, and prefer that your comments would look like this:

<pre>
/*
 I love C++!
www.xoredevelopmentstudios.com is the best site ever!
Programming can be used for pretty much everything!!!! 
*/
</pre>

It&#8217;s ok, I&#8217;m a clean freak too. You&#8217;ll see in my code soon.

Note: Remember this, if you wish to use comments, don&#8217;t put spaces between the characters. So, use // instead of  / /. Or /* instead of / *. If you put spaces between the characters, then it will not comment out what you wish to be commented.

Note: Know that the comments have no effect on the executable code size, or speed of execution of a program. Comments are removed by the compiler before any program code is generated by the compiler.

Preprocessor Directives:
Remember the code I showed you at the beginning of this tutorial? Look over it again real fast, and look at the line that has :
<pre>
#include <iostream>
</pre>
This contains a preprocessor directive that informs the compiler to read a file named iostream.h (a header) into the program. A preprocessor directive is a command to the compiler&#8217;s preprocessor that deals with instructions before compilations begin. A preprocessor directive begins with the number (or hash) symbol (#).
There are several preprocessor directives, such as the
<pre>
#define
#if
#endif
</pre>

The #include directive instructs the compiler to insert the header or include file iostream.h into the program at the point where the directive occurs. For example, suppose you have a file called info.h which contains information you need to use.

<pre>
#include &#8220;info.h&#8221;
#include <iostream>
void main()
{
	cout << &#8220;Hello, Object&#8221; << endl;
}
</pre>

The #include preprocessor directive has inserted the header file info.h into the program.

What is iostream.h?
Iostream.h contains class declarations that are essential for programs that want to perform input and output ( I/O, I = input, O = output ). Namely ios, istream, ostream, and iostream. A stream is actually an abstraction of data flow. In order to use the cout identifier and << operator (we will look over this operator soon!) the compiler needs to have their declarations by including the right header file.
Header files should generally not contain any executable program code, and a glance through iostream.h confirms that the file consists entirely of class declarations for I/O operations. The implementation of these class member functions is restricted to an implementation file (.cpp, the main source file). If you do want to look through iostream.h, then I would suggest not to alter anything. As a matter of fact, I&#8217;d rather you didn&#8217;t look through it. If you are a beginner, it will be too complex for you and it will confuse you. No offense, but I don&#8217;t want to overload you with too much to think about through these tutorials. So we&#8217;ll just keep it step at a time.

&#8220;&#8221; or <> ?
You might have noticed in the source I posted where we included info.h, that when we included info.h, it was enclosed between double quotes ( &#8220;&#8221; ), while iostream was enclosed in angle brackets ( <> ).
The #include preprocessor directive may take one or two forms for header files, these two being the different forms:

<pre>
#include <HeaderFile>
#include &#8220;HeaderFile&#8221;
</pre>

Where we put the HeaderFile must be a valid filename with an extension that fits that of some form of a header. The pathname and delimiters are optional, for instance, we could use the exact path for info.h. Let&#8217;s say it&#8217;s under C:\\MyCPP\Learning\.

<pre>
#include &#8220;C:\MyCPP\Learning\info.h&#8221;
</pre>

Note that the single backslash ( \ ) is sufficient to indicate subdirectories for a preprocessor directive.
What&#8217;s the difference between the two different methods of including a header? The < >'s will look through your "include" folders in your compiler. When you use " "'s, it will search through the project's folder.

.H or .HPP?
You might wonder, if .cpp denotes an implementation file for a C++ program, then doesn&#8217;t .hpp to denote a C++ header file? It&#8217;s very clear that .hpp is a C++ header, it clearly distinguishes a C++ header from the traditional C header file.  However, .h unlike .cpp is a one-to-one abbreviation for C++, .hpp appears meaningless. So you will see .h be used much, much more commonly rather than .hpp. However, there is a new style of naming header files proposed in the draft ANSI/ISO C++ standard, which is only called by name. You will realize I did that with the #include <iostream>.

Keywords
A keyword,  or reserved word, such as void, is a feature of the C++ language which has associated with it some special meaning. Other keywords include char, int, and class.
A keyword cannot be used as an identifier, object, or function name.

<pre>
//&#8230;.
void void() //error!
{
	//&#8230;.
	int char; // error!
	//&#8230;
}
</pre>

The main() Function
Every C and C++ console based program must call the main() function at program startup.

<pre>
void main()
{
	//&#8230;
}
</pre>

It is possible not to use main() and define an alternative function name:

<pre>
void my_main()
{
	//&#8230;.
}
</pre>

And still have successful program compilation. However, if there is no main() function in your program, the linker will produce an error depending on the memory model used.
Inter-word spaces are not allowed for keywords, function names, classes, etc. Thus, vo id, and  my main() are illegal and hence the reason for using the underscore: my_main().
The function name main is followed by a pair of parentheses (opening and closing). Since main is not a keyword in C++, the parentheses after the function name indicate to the compiler that main() is a function.

Note: main() is not a user-defined function. C++ is a case sensitive language, so main() is different from Main(). Let me demonstrate to you, how you could have a program. This will work fine:

<pre>
#include <iostream>
void main()
{
	Cout << &#8220; Hello, Object&#8221; << endl;
}

void Main()
{
	Cout << &#8220; I&#8217;m different!&#8221; << endl;
}
</pre>

If the uppercase M in Main() is replaced with a lowercase m,  you will get a compilation error of the form &#8220;function main() already defined&#8221;. Therefore, main(), Main(), and MAIN() all have different meanings in C++. Make sure that the main() toy use is always in lowercase.
Functions, C++, and user-defined, will be discussed in more detail at a later stage, but for now, the essential components of a function are: