Understanding C++/Introduction

From Wikibooks, open books for an open world
Jump to: navigation, search

[edit] 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 to do something. A computer can only follow instructions that are part of its instruction set, much in the same way as a person that only understands English wouldn't understand a person communicating in Spanish.

A programming language consists of instructions that a computer knows of and can use to do something. 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. Similarly every programming language has grammatical rules that when followed can be used to create meaningful expressions, such as "1+1". sense It followed syntax wouldn't you a different make if (It wouldn't make sense if you followed a different syntax). Just to illustrate the importance of proper syntax/"grammar".

An expression is a sequence of one or more grammatically correct instructions that can be processed by a computer to make it do something. A compiler is a software program that translates the meaning of expressions of a programming language into appropriate instructions that can be processed by the computer to make it do things. This allows anyone who understands how to write expressions that follows the rules of a programming language to tell a computer to do something. 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, contains an instruction to add 1 to 1 and when processed the calculator returns "2" as a result. 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.

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. */
  7.  
  8. #include <iostream.h>
  9.  
  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, line by line.

The first line contains /*, this is used to denote a comment in C++. Comments are completely ignored by the compiler, they are used for record keeping purposes, to display info to help those reviewing code, and to prevent a section of code from being run. Similar to the block described above, the /* syntax is used to start a comment block, it tells the compiler to ignore all material until the first occurrence of */, marking off the end of a comment block, as seen in line six.

The first uncommented code is on line 8, 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 send output and receive input. If C++ programs could not include other files, programming in C++ would be more difficult.

At line 10, you will find another key term(s) in C++. The 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. */
  7.  
  8. #include <iostream.h>
  9. using namespace std;
  10.  
  11. int main() {
  12.    cout << "Hello World!" << endl << "My name is Dell!" << endl;
  13.    return 0; // exit program
  14. }

Next, "what are those wierd << 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.

Personal tools
Namespaces
Variants
Actions
Navigation
Community
Toolbox
Sister projects
Print/export