Recap
• Programming Languages(C, C++, Java, Python)
• Source code, Object code
• Errors, Types of errors
• Language translators
• Major types of languages
C++ Programming Environment(IDE)
To program in a language, we need a special software called an
Integrated Development Environment (IDE).
An IDE consists of :
•An editor to edit source code
•A compiler to convert your code to executable machine code
•A debugger to find errors in a program.
Microsoft Visual C++, Turbo C++ Explorer, Dev C++ etc.
C++ Programming Environment
Sourceprogram/Source code
•A program written in a high-level language
•The statements written by the programmer are called source code, and the file they are
saved in is called the source file.
Object program/Object code
• The machine language version of the high-level language program
• The compiler stores the translated machine language instructions, which are called object
code, in an object file.
Executable code
• The executable file contains machine language instructions, or executable code, and is
ready to run on the computer.
7.
Basics of aTypical C++ Environment
Loader
Primary
Memory
Program is created in
the editor and stored
on disk.
Preprocessor program
processes the code.
Loader puts program
in memory.
CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
executes.
Compiler
Compiler creates
object code and stores
it on disk.
Linker links the object
code with the libraries,
creates a.out and
stores it on disk
Editor
Preprocessor
Linker
CPU
Primary
Memory
.
.
.
.
.
.
.
.
.
.
.
.
Disk
Disk
Disk
Disk
Disk
Phases of C++ Programs:
1. Edit
2. Preprocess
3. Compile
4. Link
5. Load
6. Execute
1 // FirstProgram.cpp
2// A first program in C++
3 #include <iostream>
4
5 int main()
6 {
7 std::cout << "Welcome to C++!n";
8
9 return 0; // indicate that program
ended successfully
10}
Welcome to C++!
preprocessor directive
Message to the C++ preprocessor.
Lines beginning with # are preprocessor directives.
#include <iostream> tells the preprocessor to
include the contents of the file <iostream>, which
includes input/output operations (such as printing to
the screen).
Comments
Written between /* and */ or following a //.
Improve program readability and do not cause the
computer to perform any action.
C++ programs contain one or more functions, one of
which must be main
Parenthesis are used to indicate a function
int means that main "returns" an integer value.
More in Chapter 3.
A left brace { begins the body of every function
and a right brace } ends it.
Prints the string of characters contained between the
quotation marks.
The entire line, including std::cout, the <<
operator, the string "Welcome to C++!n" and
the semicolon (;), is called a statement.
All statements must end with a semicolon.
return is a way to exit a function
from a function.
return 0, in this case, means that
the program terminated normally.
The Parts ofa C++ Program
C++ Comments
• Comments are for the reader, not for the compiler. So when a compiler compiles a
program to check for the syntax errors, it completely ignores comments.
• Comments are normally used to annotate code for future reference.
A C++ comment is written in one of the following ways:
• Single-line comment- starts with // characters, followed by any sequence of characters, till
the end of the line
• Multi-line comment- The /* (slash, asterisk) characters, followed by any sequence of
characters (including new lines), followed by the */ characters.
12.
The Parts ofa C++ Program
Preprocessor directive & header file(s)
• The preprocessor reads the program before it is compiled and only executes those
lines beginning with a # symbol.
• The #include directive causes the preprocessor to include the contents of another
file in the program (iostream).
• The iostream file is called a header file, contains code that allows a C++ program to
display output on the screen and read input from the keyboard.
The compiler doesn’t actually see the #include directive. Instead it sees the code that was inserted by
the preprocessor, just as if the programmer had typed it there.
13.
The Parts ofa C++ Program
int main () { }
• The part of the program called main function, starts with { and ends with }. The set
of parentheses that follows the name indicate that it is a function.
A function can be thought of as a group of one or more programming statements
that collectively has a name .
• int stands for “integer” It indicates that the function sends an integer value back ,
return statement explicitly return a value.
Every C++ program must have a function called main. It is the starting point of the
program
14.
The Parts ofa C++ Program
• std::cout
• Standard output stream object
• “Connected” to the screen
• std:: specifies the "namespace" which cout belongs to
• std:: can be removed through the use of using statements
• <<
• Stream insertion operator
• Value to the right of the operator (right operand) inserted into output stream
(which is connected to the screen)
• std::cout << “Welcome to C++!n”;
• Any thing in “” is called string literal or string constant
15.
The Parts ofa C++ Program
Escape Sequence
• Escape sequences in C++ Programming are character combinations which
comprise a backslash () followed by some character.
• They give results such as getting to the next line or a TAB space.
The Parts ofa C++ Program
Escape Sequence
cout<< "ThisnisnantestnnShe said, "How are you?"n";
Which would display:
This
is
a
test
She said, "How are you?"