Last modified on 24 April 2014, at 13:12

Understanding C++/Introduction


To understand the natives, you must learn their language.

A software program or application consists of a sequence of instructions that tells a computer what to do. A cake recipe can be thought of as a program with a list of instructions that tells a person what to do to make a cake. You can refer to the same cake recipe anytime you wish to make a cake when you know the language the instructions are written in. A computer can refer to the same software program to repeat the same sequence of instructions again later when the instructions are within the set of instructions the computer can follow. The set of instructions a computer can follow is known as an instruction set.

IPO Diagram.svg

An instruction may need to receive input for a computer to do something, process the input as data, and return information as output. The output of one instruction may be used as the input of another instruction. Input and output may be anything the computer is designed to do. A cake making computer might need to know how many servings to make as input, inform you of how much of each ingredient is needed as output, and process the ingredients to make cake.

A programming language consists of the set of available instructions that can be used to tell a computer what to do and grammatical rules that when followed can be used to create meaningful expressions, such as "1+1". The English language has grammatical rules that when followed can be used to create meaningful sentences from different types of words, like nouns and verbs. rules Language when grammar sense followed make are (Languages make sense when grammar rules are followed). The importance of proper grammar should be clear.

An expression is a sequence of one or more grammatically correct instructions that tell a computer what to do. A compiler is a software program that translates the meaning of expressions of a programming language into appropriate instructions for a computer. Anyone who understands how to write expressions of a programming language can tell a computer what to do by using a compiler to do the translation, much like a person who knows both Spanish and English can be a translator for people who only know English or Spanish. A person who understands and can use a programming language to write software programs is called a programmer.


An electronic calculator is one of the simplest forms of a computer with expressions consisting of numbers, mathematical operators and functions like log() that can be combined together to a create mathematical expression that the calculator can process to return a result. The mathematical expression "1+1", for example, consists of the instruction add which takes as input two numbers and when processed by the calculator returns "2" as the result or output. However unlike calculators, the instructions of one computer may not mean anything to another computer, just as two people who don't share a common language may not understand each other. Programming languages, like C++, make writing portable programs easier, through translation of a consistent language into instructions that the target computer can process.

Expressions in C++ consist of numbers, operators, variables, functions, classes, enumerations and other things which make up the language. In English, various punctuation marks end a sentence. In C++, a semicolon (;) ends all statements, such as "a=1+1;". A block is a group of statements surrounded by open and closing curly brackets ({}). Blocks serve much the same purpose as paragraphs do in English.

Example programEdit

Now that you understand the basic building blocks of C++, you are ready to see your first C++ program:

  1. /*
  2.    helloworld.cpp
  3.    Displays:
  4.    Hello World!
  5.    My name is Dell!
  6. */
  8. #include <iostream>
  10. int main() {
  11.    std::cout << "Hello World!" << std::endl << "My name is Dell!" << std::endl;
  12.    return 0; // exit program
  13. }

You have probably seen some things that don't make sense yet. Let's look at this, piece by piece.

Lines 1 through 6 denote a C++ comment. Comments help those reviewing code and are completely ignored by compilers. Comments are often used for record keeping purposes, or to cause a section of code to be ignored by compilers. Similar to blocks described above, "/*" begins a comment block, and the first occurrence of "*/" ends a comment block.

Line 8 is the first instruction that compilers will not ignore, it reads "#include <iostream>". #include tells the compiler to consider the content of a file to be part of the current program. In the case of iostream, a C++ program will be able to receive input and send output. Inclusion makes programming in C++ easier by allowing you to reuse other programmer's existing work in your own program.

Line 10, you will find more key term(s) in C++. int is one of the basic types in C++. int is short for integer. An int can hold any whole number value, positive or negative, within certain limits, but we won't get to that for a while.

Next you will find main, a function name. C++ programs begin by calling the main function. C++ programs must have only one main function and that function must return an integer.

Next you will find (), which means this function isn't passed anything. We will talk about functions later.

Next is {, which indicates the beginning of a block. This particular block is called a function block. We will discuss other types of blocks that exist in C++ later as new concepts are introduced.

Before continuing, meet "using namespace std;". This expression means that std:: doesn't need to be included before certain things in the standard library. This can save typing, but at a cost which we'll get to later. After including that the code can be shortened like this:

  1. /*
  2.    helloworld.cpp
  3.    Displays:
  4.    Hello World!
  5.    My name is Dell!
  6. */
  8. #include <iostream.h>
  9. using namespace std;
  11. int main() {
  12.    cout << "Hello World!" << endl << "My name is Dell!" << endl;
  13.    return 0; // exit program
  14. }

Next, "what are those weird << things doing in the code?" you may ask. This tells the compiler "Ok, this is what we are going to print to the screen." and when you see something like:

cout << "Hello World!" << endl << "My name is Dell!" << endl;

You may wonder why not just do this?:

cout "Hello World!" endl "My name is Dell!";

The reason is syntax. Without the << in place, the compiler doesn't know when to perform the next task (which in this case, the 2nd task is the "Hello World!"). Technically, this is what the computer thinks (you don't have to read it if you understand):

"Ok, i see a cout. That means print stuff to the prompt. Next to it i see "Hello World!", ill send that to the screen. Oh, i see a <<, im moving on to the next step. The next thing is endl, so i will create a new line. Another <<! Wow, i need to play the lotto. Ok, so now i have to print "My name is Dell!". Now i see ;. Ok, im done with this whole line of code."

Now, the basis in which programming interactive programs... variables. In short, variables store stuff, as in a storage bin. This will be finished later...

Throughout the rest of this book, you will learn more about C++, the standard rules of the language, and come to understand this and other C++ programs you may encounter. The rest of this chapter takes a look at what each of the remaining chapters in this book covers.