Lecture 01
Lecture 01
Kareem Mahdhloom
C++ PROGRAMMING:
FROM PROBLEM ANALYSIS TO PROGRAM DESIGN
FIFTH EDITION
D.S. MALIK
General Notes on C++
C++ is immensely popular, particularly for applications that require speed and/or access
to some low-level features.
Hello World
In the tradition of programmers everywhere, we’ll use a “Hello, world!” program as an entry point into
the basic features of C++
The code
Tokens:
Tokens are the minimals chunk of program that have meaning to the compiler – the smallest meaningful symbols in the
language. Our code displays all 6 kinds of tokens, though the usual use of operators is not present here
Line-By-Line Explanation
1. // indicates that everything following it until the end of the line is a comment: it is ignored by the compiler.
Another way to write a comment is to put it between /* and */ (e.g. x = 1 + /*sneaky comment here*/ 1;). A
comment of this form may span multiple lines. Comments exist to explain non-obvious things going on in the
code.
2. Lines beginning with # are preprocessor commands, which usually change what code is actually being compiled.
#include tells the preprocessor to dump in the contents of another file, here the iostream file, which defines the
procedures for input/output.
4. int main() {...} defines the code that should execute when the program starts up. The curly braces represent
grouping of multiple commands into a block. More about this syntax in the next few lectures.
5. • cout << : This is the syntax for outputting some piece of text to the screen.
• Namespaces: In C++, identifiers can be defined within a context – sort of a directory of names – called a
namespace. When we want to access an identifier defined in a namespace, we tell the compiler to look for it in
that namespace using
the scope resolution operator (::). Here, we’re telling the compiler to look for cout in the std namespace, in which
many standard C++ identifiers are defined. A cleaner alternative is to add the following line below line 2:
using namespace std;
This line tells the compiler that it should look in the std namespace for any identifier we haven’t defined. If we do
this, we can omit the std:: prefix when writing cout. This is the recommended practice.
• Strings: A sequence of characters such as Hello, world is known as a string. A string that is specified explicitly in a
program is a string literal.
• Escape sequences: The \n indicates a newline character. It is an example of an escape sequence – a symbol used
to represent a special character in a text literal.
Here are all the C++ escape sequences which you can include in strings:
7. return 0 indicates that the program should tell the operating system it has completed successfully. This syntax will
be explained in the context of functions; for now, just include it as the last line in the main block.
Note that every statement ends with a semicolon (except preprocessor commands and blocks using {}). Forgetting
these semicolons is a common mistake among new C++ programmers.
Just as cout << is the syntax for outputting values, cin >> (line 6) is the syntax for inputting values.