The Basics of C++ (Lesson 1)

Assuming you already set up the development environment, you are ready to run your first program. Let’s start with a very simple code and walk through the program line by line: #include <iostream> using namespace std; int main () { cout <<"Hey, you, I'm alive! n"; // this is just a comment }
  • The first line #include iostream is an include statement telling the compiler to put code from the header file called iostream into our program. This line allows us to access to basic functions.
  • Almost all C++ programs include using namespace std. The statement itself makes it easier to use shorter versions of the routines provided by iostream header file.
  • The semicolon is part of the snytax of C++. It tells the compiler that you are at the end of a statement.
  • The main function is a special function; it’s the only function that must be included in all C++ programs, and it’s the point where your program start when you run it. Normally we would need to return a value here, but C++ allows the main function to omit the return statement and it will default to returning 0 (a code that tells the operating system everything went ok). The curly braces, { and }, signal the beginning and end of functions.
  • C++ uses cout object to display text. Getting access to cout is the reason we included the iostream header file. It uses the << symbols, known as “insertion operators”, to indicate what to output. The sequence n stands for a new line.
  • You can add comments to document your programs by using either //, which tells the compiler that the rest of the line is a comment, or /* and then */ to block off everything between a comment.
Now, why don’t you play with this code by your own and see what happens. Remember, programming can only be learnt by writing your own code.
This material is from the textbook Jumping into C++ by Alex Allain. I strongly recommend this book if you are new to programming and want to learn more than what I described here.
Click for Lesson 2 tr Türkçe
This entry was posted in Programming and tagged , , . Bookmark the permalink.