www.easyCPlusPlus.com

Easy C++

www.easyCPlusPlus.com

C++ Tutorial - Lesson 1: A First Program

by John Kopp

Support this site at no cost to you

Welcome to easyCPlusPlus.com's C++ tutorial. The lessons in this tutorial will take you from being a beginner to being able to write real programs in C++.

C++ is a compiled language. The C++ compiler is a program that reads source code, which is the C++ code written by a programmer, and it produces an executable or binary file that in a format that can be read and executed (run) by a computer. The source file is a plain text file containing your code. The executable file consists of machine code, 1's and 0's that are not meant to be understood or read by people, but only by computers.

The best way to learn anything is to jump right in, so let's start by writing a simple C++ hello world program.

First Program

#include <iostream>
using namespace std;
int main()
{
     cout << "Hello World From easyCPlusPlus\n";
}

Line 1: #include <iostream>
     As part of compilation, the C++ compiler runs a program called the C++ preprocessor. The preprocessor is able to add and remove code from your source file. In this case, the directive #include tells the preprocessor to include code from the file iostream. This file contains declarations for functions that the program needs to use, as well as available classes.

Line 2: using namespace std
     C++ supports the concept of name spaces. Essentially, this allows variables to localized to certain regions of code. The command using namespace std allows all objects and functions from the standard input and output library to be used within this program without explicit qualifications. Namespaces are an advanced concept. For now, all you need to know is that this line allows simple use of the standard library.

Line 3: int main()
     This statement declares the main function. A C++ program can contain many functions but must always have one main function. A function is a self-contained module of code that can accomplish some task. Functions are examined in a later tutorial. The "int" specifies the return type of main to be an integer. An explicit value may be returned using a return statement. In this case, 0 is returned be default.

Line 4: {
     This opening bracket denotes the start of the program.

Line 5: cout << "Hello From easyCPlusPlus\n";
     cout is a object from a standard C++ library that has a method used to print strings to the standard output, normally your screen. The compiler links code from these standard libraries to the code you have written to produce the final executable. The "\n" is a special format modifier that tells the method to put a line feed at the end of the line. If there were another "cout" in this program, its string would print on the next line.

Line 6: }
     This closing bracket denotes the end of the program.

That's it. To get the most of this series of tutorials, you should get access to both a text editor and a C++ compiler. Some instructions for doing this are in our tutorials on compiling.

John

Next Page