C++/Introduction

From Wikiversity
< C++(Redirected from Introduction to C++)
Jump to: navigation, search

Hello World![edit]

The first program that most aspiring programmers write is the classic "Hello World" program. The purpose of this program is to display the text "Hello World!" to the user. The "Hello World" example is somewhat famous as it is often the first program presented when introducing a programming language[1].


#include <iostream>
 
using namespace std;
 
int main()
{
   cout << "Hello World!" << endl;
   cin.get();
 
   return 0;
}


NOTE:

The 'return 0;' as shown above, is not a necessary addition to the 'hello world' program. A return value of 0 in main simply signals to the operating system that everything went smoothly. By default, a C++ program will always return 0 if there is no return at the end of main.

Understanding the Code[edit]

Before discussing the particulars, it is useful to think of a computer program simultaneously in terms of both its structure and its meaning.

A C++ program is structured in a specific, particular manner. C++ is a language and therefore has a grammar similar to a spoken language like English. The grammar of computer languages is usually much, much simpler than spoken languages but comes with the disadvantage of having stricter rules. Applying this structure or grammar to the language is what allows the computer to understand the program and what it is supposed to do.

The overall program has a structure, but it is also important to understand the purpose of part of that structure. By analogy, a textbook can be split into sections, chapters, paragraphs, sentences, and words (the structure), but it is also necessary to understand the overall meaning of the words, the sentences, and chapters to fully understand the content of the textbook. You can think of this as the semantics of the program.

A line-by-line analysis of the program should give a better idea of both the structure and meaning of the classic "Hello World" program.

A Detailed Explanation of the Code[edit]

#include[edit]

#include <iostream>

The hash sign (#) signifies the start of a preprocessor command. The include command is a specific preprocessor command that effectively copies and pastes the entire text of the file specified in the angled brackets into the source code. In this case the file is "iostream" which is standard file which should come with the C++ compiler. This file name is short for "input-output streams"; in short, it contains code for displaying and getting text from the user.

The include statement allows a program to "include" this functionality in the program without having to literally cut and paste it into the source code every time. The iostream file is part of C++ standard library, which provides a set of useful and commonly used functionality provided with the compiler. The "include" mechanism, however, can be used both for standard code provided by the compiler and for reusable files created by the programmer.

using namespace std[edit]

using namespace std;

C++ supports the concept of namespaces. A namespace is essentially a prefix that is applied to all the names in a certain set. One way to think about namespaces is that they are like toolboxes with different useful tools. The using command tells the compiler to allow all the names in the "std" namespace to be usable without their prefix. The iostream file defines three names used in this program - cout, cin, and endl - which are all defined in the std namespace. "std" is short for "standard" since these names are defined in the standard C++ library that comes with the compiler.

Without using the std namespace, the names would have to include the prefix and be written as std::cout, std::cin, and std::endl. If we continue with the toolbox example, this code would be saying, "Use the cout, cin and endl tools from the std toolbox."

int main()[edit]

The entry point of all C++ programs is the main function. This function is called by the operating system when your program is loaded.

cin, cout[edit]

cout << "Hello, World!" << endl;
cin.get();

The name cout is short for "character output" and cin, correspondingly, is an abbreviation for "character input".

In a typical C++ program, most function calls are of the form object.function_name(argument1, argument2), such as cin.get() in the example above (where cin is the object, get is the function name, and there are no arguments in the argument list). However, symbols such as << can also behave as functions, as is done with cout in this case. This functionality is called operator overloading which will be discussed later.

{ }[edit]

A block of code is defined with the { } tokens. { signifies the start of a block of code and } signifies the end.

NOTE: The { } tokens have other functions.

semicolons[edit]

Each statement in C++ is ended by a semicolon. This is very roughly the equivalent of a sentence in a language like English. This tells the compiler where one operation ends and the next begins.

Note that it is common to hear someone refer to a "line of C++ code". This often can actually mean a single statement rather than literally a single line of text, since most programmers prefer a style of one line of text per statement.

return[edit]

The return keyword stops the function where it is and returns a value (the return type must match the function's type) to the calling function. Using the return keyword is mainly effective to stop the program.

Compiling the code[edit]

In order for the computer to run the code you have written, it needs to first be compiled by a C++ compiler. In short, a compiler changes the program into a form that the computer can more directly understand.

What the compiler does[edit]

In very broad terms, the compiler is a translator that acts as an intermediary between the programmer and the CPU on the computer. A high-level language like C++ is actually a sort of 'compromise' language between the native language of the CPU (generally referred to as machine language) and the native language of the programmer (say English). Computers do not natively understand human languages, yet for someone to write computer code in the native language of the machine would be too difficult and time consuming. Therefore, the purpose of the computer language itself is to define a mid-point that is closer to how humans think and organize procedures but is still unambiguously translatable to the native machine language.

The compiler therefore is reading in the code written by the programmer and translating it into machine language code, that the computer can understand directly.

C++ is a compiled language that is converted to machine language by the compiler. Beginner programmers will likely also come across the notion of interpreted languages and interpreters. Since this text covers C++, interpreted languages are not covered in detail; however, in brief, an interpreter is like a compiler that converts the program into machine language at the time it is run on the computer rather than in advance as is done with a compiled language. An example of a interpreted language is Java.

Running the compiler[edit]

The code needs to be compiled with a compiler to finish the process. What if you don't have one? Well, the good news is, there are tons of good compilers that are available for free. The GNU Compiler Collection (GCC) has versions available for most systems and does a good job of implementing the ISO C++ standard. The clang compiler has complete support for C++11 and FreeBSD fully support clang and C++11. However, many people prefer to use an Integrated Development Environment (IDE) which provides a user friendly environment for developing programs. For MacOS X, there is Xcode which uses gcc for compiling C++. For Windows, there is Dev C++ which also uses gcc for compiling C++, Microsoft Visual C++ (and its free Express version), TCLITE, and ports of the GNU Compiler Collection distributed within Cygwin and MinGW.

Each compiler has its own way of compiling programs. If you use GCC, type the following in a terminal:

c++ example.cpp -o example

Replace example.cpp with the actual name of the file you wish to compile. Replace example with the name you wish to give the executable program.

If you want to use a specific compiler, you will need to read up on that particular compiler.

Exercises[edit]

Exercise 1[edit]

Copy the following, then edit it so it compiles correctly and prints "Hello, World!" on the screen.

#include<iosteam.h>
void main()
{
 
cout<<"Danny Walling";
cin getput();
}

Exercise 2[edit]

Change the "Hello, World!" example above to display another line. If Spock were doing this exercise, he might add to it so that it would display:

Hello, World!
Live long and prosper.

Where To Go Next[edit]

Topics in C++
Beginners Data Structures Advanced
Part of the School of Computer Science


References[edit]