C++ Learning TUT BIG!

Posts 115 of 15 · Page 1 of 1
C++ Learning TUT BIG!
OK guys well I will be teaching you C++ over time you will learn from this. I will be adding more and more and more as I feel the need you have Grasped the concept. So let's start



One of the hardest things about learning a programming language is the fact that no element exists in isolation. Rather, the components of the language work without involving another. To help overcome this problem, this chapter Chaper 1.. lol provides a brief overview of seceral core C++ features, including the general form of a C++ program, some simple control statements, variables, and operators. It does not go into too many details, but rather concentrates on the general concepts common to all C++ programs. Most of the topics presented here are examined more closely in later chapters.

Since learning is best accomplished by doing, it is recommended that you work through the example using your computer.
Your first C++ program
Before getting into any theory, let's look at a simple C++ program. We will start by entering, compiling, and running the following program.

Code:
/* Program #1 - A first C++ program.

Enter this program, then compile and run it.
*/

#include <iostream>
using namespace std;

// main() is where program execution begins.
int main()
{
cout << "This is my first C++ program.";

return 0;
}
You will follow these steps.

1. Enter the program.
2. Compile the program.
3. Execute the program.

Before beginning, it is necessary to fedine two terms. The first is source code. Source code is the version of your program that humans can read. :P The preceding listing is and example of source code. The executable version of your program is called object code or executable code. Objeect code is created by the compiler when it compiles your pgoram.
Entering the Program
Typing the programs yourself often helps you remember the key concepts.

The name of the file that holds the source code for the program is technically arbitary. However, C++ programs are normally contained in files that use the file extension .cpp. thus, you can call a C++ program file by any name, but it should use the .cpp extension. For this example, call the source file MyProgramOwns.cpp so that you can follow along. For most of the other programs in this tut , simply use a name of your own choosing.
Compiling the program
How you will compile MyProgramOwns.cpp depends upon your compiler, and what options you are using. Furthermore, many compilers, such as Microsoft's Visual C++ and Borland's C++ builder, provide two different ways for compiling a program: the command line compiler and the Integrated Development Environment (IDE). Thus, It is not possible to give generalized instructions for compiling a C++ program that will work for all compilers. You must consult your compiler's instructions.

The preceding paragraph not withstanding, two of the most popular compilers are Visual C++ and C++ builder. For the benefit of readers using one of these compilers, the easiest way to compile and run the programs in this tut is to the use the command-line compilers offered by these environments, and that is the method described.

To comple MyProgramOwns.cpp using Visual C++, you will use this command line.

C:\...>cl -GX MyProgramOwns.cpp

The -GX option enhances compilation. To use the visual C++ command-line compiler, you must first execute the batch file VCVARS32.BAT, which is provided by Visual C++. (You will want to consult your visual C++ documentation for details.)
To compile MyProgramOwns.cpp using C++ builder, use this command line

C:\...>bcc32 Sample.cpp

The output from a C++ compiler is executable object code. For a windows environment, the executable file will use the same name as the source file, but have the .exe extension. Thus, The executable version of MyProgramOwns.cpp will be in MyProgramOwns.cpp.exe.
Run the Program
After a C++ program has been compiled, It is ready to be run. Since the output from a C++ compiler is executable object code, to run the program, simply enter its name at the command prompt. For example, to run MyAwesomeProg.exe use this command line:

C:\...>MyAwesomeProg

When run, The program displays the following output.

This is my frist C++ program.

If you are using an Integrated Development Environment, then you can run a program by selecting Run from a menu. Consult the instructions for your specific compiler. As mentioned earlier, for the programs in this tut, it is usually easier to compile and run from the command line.

One last point: The programs in this tut are console-based, not window-based.
That is, they run in a Command Prompt session. C++ is completely at home with Windows programming. Indeed, it is the most commonly used language for Windows development. However, none of the programs in this tut use the Windows Graphics User Interface(GUI). The reason for this easy to understand: Windows is a complicated environment to write programs for, involving many side issues unrelated to the C++ language. In contrast, Console-based programs are much shorter and are the type of programs normally used to teach programming. One you have mastered C++, you will be able to apply your knowledge to Windows programming with no trouble
A line-by-line explanation
Now that you have successfully compiled and run the first sample program it is time to understand how it works. towards this end, we will examine the program line by line. The program begins with the lines

/* Program #1 - A first C++ program.

Enter this program, then compile and run it.
*/

This is a comment. Like most other programming languages, C++ lets you enter a remark into a program's source code. The contents of a comment are ignored by the compiler. The purpose of a comment is to describe or explain the operation of a program to anyone reading its source code. In the case of this comment, it identifies the program. In more complex programs, you will use comments to help explain what each features of the program is for now and how it goes about doing its work. In other words, You can use comments to provide a "play By Pay" description of what your program does.

In C++, there are two types of comments. The one you've just seen is called a multiline comment. This type of comment begins with a /* and ends with only a */ Anything between these two comment symbols is completely ignored by the compiler. Multiline comments may be one or more lines long. The second type of comment is found a little further on in the program; we'll be discussing it shortly.

The next line of code looks like this:

#include <iostream>

The C++ language defines several headers, which contain information that is either necessary or useful to you program. For this program, the header <iostream> is needed.(it is used to support the C++ I/O system.) This header is provided with your compiler. A header is included in your program by using the #include directive. Later in this tut, You will learn more about headers and why they are important.

The next line in the program is

using namespace std;

This tells the compiler to use the std namespace. Namespaces are a relatively recent addition to C++. Although namespaces are discussed in detail later in this tut, here is a brief description. A namespace creates a declarative region in which various program elements can be placed. Elements declared in one namespace are separate from elements declared in another. Namespaces help in the organization of large programs. The using statement informs the compiler that you want to use the std namespace. This is the namespace in which the entire Standard C++ library Is declared. By using the std, you simplify access to the standard library.

The next line in the program is

// main() is where program execution begins.

This line shows you the second type of comment available in C++: the single=line comment. Sing-line comments begin with // and stop at the end of the line. Typically, C++ programmers use mulltiline comments when writing larger, more detailed commentaries, and they use single-line comments when short remakrs are needed. However, this is a matter of personal style
A line-by-line explanation Cont.
The next line, As the preceding comment indicates, is where program execution begins:

int main()

All C++ programs are composed of one or more functions. (Loosely speaking, a function is a subroutine.) Every C++ function must have a name, and the only function that any C++ program must include is the one shown here, called main(). the Main() function is where program execution begins and (most commonly)Ends. (Technically speaking, a C++ program begins with a call to main()and, in most cases, ends when main() returns.) The opening curly brace on the line that follow mains() marks the start of the main() function's code. The int that precedes main() specifies the type of data returned by main(). As you will learn, C++ supports several built in data types and int is one of them. It stands for Integer.

The next line in the program is

cout << "this is my frist C++ program.";

This is a console output statement. It causes the message This is my first C++ program. to be displayed on the screen. It accomplishes this by using the output operator <<. the << operator causes whatever expression is on its right side to be output to the device specified on its left side. cout is predefined identifier that sstands for console output, which (most generally) refers to the computer's screen.
This, this statement causes the message to be output to the screen. Notice that this statement ends with a semicolon. In fact, all C++ statements end with a semicolon.

The message "this is my first C++ program." is a string. In C++ , a string is a sequence of characters enclosed between double quotes. As you will see, strings are used frequently in C++.

The next line in the program is

return 0;

This line terminates main() and causes it to return the value 0 to the calling process (which is typically the operating system(, for most operating systems a return value pf 0 signifies that the program is terminating normally. other values indicate that the program is terminating because of some error. return is one the C++'s keywords, and it is used to return a value from a function. AL;l of your programs should return 0 when they terminate normally(that is, Without error).

The closing curly brace at the end of the program formally concludes the program Although the brace is not actually part of the object code of the program, conceptually you can think of a C++ program ending when the closing curly brace of main() is executed. In fact, if the return statement were not part of this sample program, the program would automatically end when the closing curly brace was encountered.
Lol can you not post frequently and maybe just edit your first post?
*sigh*. You see that knife by your mouse, yeah, well either stab me ion the eyes, or yourself. There's an edit button RIGHT there, seriously man. lol

I don't mind you trying to help, but doing it over 20 posts is only going to piss people off.

EDIT: Oh look, I just used it just to say, that you should add a TOC, references you used. Otherwise you're going to be flamed for being a C+P'er. And, *sigh* if you say you wrote all of it yourself, then all I have to say is, where did you learn it from?. Few pointers.

And, btw. Here's what I mean unless you are too busy ignoring what everyone is saying. Kinda like how you ignored Lolland.
http://www.mpgh.net/forum/31-c-c/103...whar-i-dl.html

But I guarantee, that you'll completely ignore this post.

EDIT: Lol Why, now I see what you mean.

Quote Originally Posted by why06 View Post
Well they've been doing it long before you allowed them too... Oh well. Like father, like son o_O. I still gotta read all of this to be honest it may not be worth sticking since 99.9% of the nubs that run through don't bother to read.
Oh well, Hell_Daddy, whenever you're ready.
Well done, you managed to explain the simpleness of 'Hello World'!
Where other people, and even C++ books spend 5-10 lines of text on it, you manage to spend 7 posts on it, and even after those 7 posts noone will bother to read it since its fucking simple 'Hello World' app that isn't even worth learning...
lol increasing your post rate much?
Quote Originally Posted by BxR. View Post
lol increasing your post rate much?
I see you're doing the same.
Quote Originally Posted by Void View Post
I see you're doing the same.
and now so are you xD
Hmmmm... what to do?

Most of this is unnecessary, but at the same time I can't dog you for the effort. I'll to think about it when its not 3:30 A.M.
Sorry I've been scarce the last few days, I always seem to be extremely busy over vacations. =/
I wouldnt blame someone for bumping a thread after putting their time into it. Shouldn't be too hard on the guy.
Posts 115 of 15 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?