Getting Started with C++
C++ Taking the Bull by the Horns – Part 1
Forward: C++ is a computer language I want to teach in these tutorials. C++ is a very developed language. You can learn C++ from these tutorials as your first language. This is the first part of the series.
By: Chrysanthus Date Published: 21 Aug 2012
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at firstname.lastname@example.org. That is, contact me for the slightest problem you have about what you are reading.
The Teaching Philosophy of these Tutorials
From what I know, people find it more difficult to learn C++ than to learn some other languages. I believe the reason is that the C++ language is closer to the machine (computer hardware) language than to the human language, while the other languages are closer to the human language than the machine language.
The philosophy to learn other languages is to have a problem in life and then learn the language in other to solve the problem. That is the philosophy used by many C++ books and C++ tutorials to teach C++. I think that is what makes the learning of C++ from those books and tutorials, difficult. So I use the reverse philosophy in these tutorials. That is, in these tutorials, the philosophy is to learn the C++ language and then decide on what problem the C++ language will solve. I use this philosophy because the C++ language is closer to the machine language than the human language.
With the philosophy of these tutorials I will start teaching you C++ the way the inventors see it, but in simple terms. In other C++ books and C++ tutorials they start teaching you C++ the way you would want to see it (closer to human language), after that, bridging the gap between human language and “machine” language, becomes difficult. As I teach you C++ from the inventors’ point of view, I believe, bridging the gap between “machine” language (C) and human language that you already know, becomes natural (easy).
It is because of this approach that I give the title to this series as, “C++ Taking the Bull by the Horns”. May be you have tried to learn C++ from some other source and found it difficult. My approach looks challenging, but explaining it in simple terms should take you through. That is why I have given it this particular title.
A programmer types a program in a text editor. A program just typed in a text editor, is coded text, understood mainly by programmers. If the ordinary person looks at the text, he would be able to identify a lot of words in the text, but he would not really understand what the whole text does. The program (code) typed in the text editor, is not machine language. In the case of C++, it is closer to machine language than human language. The coded text is the program code. It has certain strings and characters positioned in such a way that the ordinary man cannot understand. Learning programming implies, learning the meaning of such typed text. The complete code typed is the program. A program can also exist in what is known as the binary form (see below).
Source Code and Execution
Code of text as mentioned above, is called the source code. A program is meant to carry out a task like to add numbers together. When a program is carrying out its task, it is being executed; the program is said to be running. To “run” a program means to have it executed.
Many programs are not run in their source code (typed) form. In this case the source code has to be converted into what is called the Binary. A binary is a form of the code, which is best handled by the computer hardware. Some software has to do the conversion from the source code into the binary. The software that does this conversion is called a compiler. An example of a compiler is the MinGW Compiler Suite. That is what I will use in these tutorials. In works with the Windows operating system. Nothing stops you from using some other compiler with some other operating system. The principles outlined in these tutorials will work with almost all C++ compilers for their different operating systems. The principles outlined in these tutorials are based on the ISO/IEC 14882:2003 specification. That is the latest international C++ standard (specification), and by today, most C++ compilers comply with it.
C and C++
C is a computer language and C++ is another computer language. C is an Old language. C++ is newer. C++ is the big brother of C. C++ is derived from C. Many programs are written in a combination of C and C++. This combination is usually indicated as, C/C++. I have written a series of tutorials on the C. The series is titled, C Tutorials from Roots. After completing this series you should come back to this block and learn C, if you do not already understand C. It is very easy to learn C after learning C++ and vice-versa. To get to my series on C, just type the title, C Tutorials from Roots , and my name Chrys in the Search box of this blog and click Search.
In order to study C++ and master it, your level of mathematics should be at least that of Middle School. If you did not pass in middle school mathematics, do not worry. There is a way out. You should study the mathematics course easily at the link below. This site offers online interactive middle school math course in a step-by-step fashion, in as short as three months or as long as 10 months. There you have the right to ask questions from the authors of the web site.
However, if you do not have middle school mathematics, you can still learn this course (subject), since the little mathematics involved is taught as you go through the course.
Here I give you the requirements for my computer. Nothing stops you from using some other requirements if you know how to handle that.
- MinGW Compiler Suite
- gcc-g++ (bin, dll) for C++
- Text Editor
- Your Personal Computer and a Windows Operating System.
The MinGW Compiler Suite is free to download. There is the downloading for automatic installation and the downloading for manual installation. I will talk only about the downloading for manual installation.
Go to the page, www.mingw.org on the web. Look for the link, Getting Started, and click it. The Getting Started page opens. Under the heading, Manual Installation on the page, you should see some lines like:
mingw-runtime (dev, dll)
Required runtime libraries for GCC (gmp, libiconv, MPFR, pthreads)
gcc-core (bin, dll)
Below these lines, you should see another line like,
gcc-g++ (bin, dll) for C++
In these lines, the following words are links: binutils; dev and dll from the mingw-runtime phrase; w32api; gmp and libiconv and MPFR and pthreads from the Required runtime libraries for GCC phrase; bin and dll from the gcc-core phrase. All that will compile C very well. In addition to that you have the links, bin and dll for compiling C++. You must install all the software to compile C and the one to compile C++ before your C++ code can be compiled.
Click all the above links including the two links for C++ to download the following files:
binutils-2.19.1-mingw32-bin.tar.gz | tar x
mingwrt-3.16-mingw32-dev.tar.gz | tar x
mingwrt-3.16-mingw32-dll.tar.gz | tar x
w32api-3.13-mingw32-dev.tar.gz | tar x
gmp-4.2.4-mingw32-dll.tar.gz | tar x
libiconv-1.13.1-1-mingw32-dll-2.tar.lzma | tar x
mpfr-2.4.1-mingw32-dll.tar.gz | tar x
pthreads-w32-2.8.0-mingw32-dll.tar.gz | tar x
gcc-core-4.4.0-mingw32-bin.tar.gz | tar x
gcc-core-4.4.0-mingw32-dll.tar.gz | tar x
The last two files are specifically for C++. Create a directory called, MinGW in the root directory, (C:) of the hard disk. Copy these 12 downloaded files into the created directory, MinGW.
Search the web for the zip software called 7-zip. Download it; it is free. Use the 7-zip File Manager to unzip these 12 files into the same MinGW directory. During the unzip process if the zip software ask you to overwrite any file, choose the bigger file.
You will have to do something to the path environment variable. You will do something similar to the following: Go to the Control Panel of your windows operating system; click the System icon. The System Properties window opens; Click the Advanced tab. Click the Environment Variables button, in the Advanced tab pane. Add the following without the quotes to the path variable: “C:\MinGW\bin;%path%”. If you already have other values in the path variable, then precede this value with a semicolon.
You are now ready to write source code, and to compile it and run (execute) it.
We shall work with the console; this means that your input and output to any C program you write will be text. In other to achieve this, you will use the Windows Command Prompt program. (if you have a different operating system, you will have to use an equivalent to the command prompt window, which will display input and output as text on the screen). To access the Command Prompt program, click the start button, point to All Programs, point to Accessories, you will see the Command Prompt program; click it. You will then see a window for white text on black background. That is your window to type input as text and see output as text. For a different operating system, you might have another way of arriving at the console.
Your working directory is the MinGW you created above.
Your First C++ Program
Start your computer and open the Command Prompt window. You should see some white text in the window, ending with something like, C:\Documents and Settings\User> , where user is your name. This is called the DOS Prompt. In order to work with the console, you need the DOS prompt. Your commands are typed to the right of the DOS prompt. Your response will be displayed by the system, below the DOS Prompt. After that display, you see the DOS Prompt again.
C:\Documents and Settings\User> is not the DOS prompt we shall use. We shall use the DOS Prompt C:\MinGW> because MinGW will be your working directory.
To arrive at this DOS Prompt, type, cd.. , and press the Enter key of the keyboard. Type, cd.. , again and press the Enter key. You should now be at the DOS prompt, C:\> To go to the working directory (DOS Prompt for working directory), type, cd mingw , and press the Enter key. You should see the DOS Prompt, C:\MinGW>. That is the DOS Prompt for your working directory.
Open your text editor and type the following source code exactly as you see it:
using namespace std;
cout << "Hello World!";
This is your first source code. Save the document typed with the name hello.cpp in the directory, MinGW. Note that the file name is hello and the extension is cpp . I will explain what is in the source code later.
Bring up the Command Prompt window again. While at the C:\MinGW> DOS Prompt, and on the right, type the following and press the Enter Key, where the letter following – is o and not zero:
g++ hello.cpp -o hello.exe
You should see the DOS prompt, C:\MinGW> after a short while. In the above command, g++ is the name of the compiler. hello.cpp is the name of the source file to be compiled. It is the file you typed and saved in the MinGW directory. hello.exe is the name that is given to the compiled file. –o is called a switch. For now just know that you need it to produce an executable file. The compiler produces the compiled (executable file). So the compiler compiles hello.cpp, creates a compiled file with the name hello.exe and saves it in the working directory. You can give some other name for the compiled (executable) file instead of hello.exe. However, it is conventional to give the same name as that of the source file and the extension, exe, since we want an executable file.
Now, we have an executable file called hello.exe in the working directory, MinGW. While still at the DOS Prompt of the working directory, we can execute (run) this file. To do that you just type the name of the file at the DOS Prompt and press Enter. Let us do that as follows. Type the following name and press the Enter key:
After a short while, you should see, “Hello World!” on the next line. This is the output of the file. Well, you should also see the DOS Prompt, C:\MinGW> below the output (it always happens like that).
We shall see the meaning of most of the things in your first source code as we go along in the series. For now I will only comment on the line, “cout << "Hello World!";” cout is an example of what is called a predefined object. The double less than signs (<<) is an example of what is called an operator. The cout object works with it to send what is on thier right to the output. What is on their right is the string, "Hello World!". This is sent to the output without the double quotes. There is more to the cout object, we shall see some of that later.
We have seen a lot, let us end here and continue in the next part of the series. I have done my best to be very explicit with all the parts of the series. Since I am teaching you C++ the way the inventors (scientists) see it, in simple terms, when you understand the first few (5) parts of the series, the rest of the series should be easy to understand. You should learn C++ from this series in the order (part 1, then part 2, then part 3, then part 4, and so on) given.
Related LinksC++ Taking the Bull by the Horns
C++ Just After the Basics
Object Oriented Programming in C++
Exception Handling in C++
Container Library Sequences in C++ Simplified
Associative Container in C++ Simplified
String in C++ Standard Library Simplified
Date and Time in C++ Simplified
Storage Duration in C++
Scopes in C++
Function and Operator Overloading in C++
Specifiers in C++
Some Features of C++ Entities
C++ Preprocessing Directives
Writing a C++ Container
Writing a C++ Application
More Related Links
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem