Chapter 2v2
Chapter 2v2
2
A C++ program
• A C++ program: is a collection of functions, one
of which is the function main. This is where
execution begins when you run the program.
• C++ program has two parts:
1. Preprocessor directives
2. The program
The structure of a simple C++ program
So the student should know the structure of a simple c++
program, in that, any program may be written according to
the following format
#include<iostream>
using namespace std;
int main()
{
//Program body
return 0;
}
The main Function
• A function is a named block of code that performs
some well-defined set of operations.
• Every C++ program must contain a function named
main. This is where execution begins when you run
the program.
• Complex programs contain many functions, but for
now most of your programs will only contain the
main function.
The main Function (cont’d.)
• The body of the function main has the following
form:
int main ()
{
Declare variables(Declaration statements);
Executable statements;
return 0;
}
The main Function (cont’d.)
• The first line of a
function is called
the function’s
heading:
int main()
• The statements
enclosed between
the curly braces
{ and } form the
function’s body.
Preprocessor Directives
• Your programs will often
contain preprocessor
directives. These are
commands that the
preprocessor program
executes before your
program is compiled.
• All preprocessor
directives begin with #.
• No semicolon at the end of these directives.
The #include Directive
• The most common preprocessor directive is #include,
which is how you tell the compiler that you want to use a
Standard Library file.
• Syntax to include a header file:
• For example:
#include <iostream>
– Causes the preprocessor to include the header file iostream
in the program. Without this, you could not use cout or cin
in your program.
namespace and Using cin and cout
in a Program
• cin and cout are declared in the header file
iostream, but within the std namespace.
• To use cin or cout in a program, use the following two
statements:
#include <iostream>
using namespace std;
• Without that using statement, you would need to type
std::cin instead of cin throughout your program.
The Standard Library
• The core C++ language has a relatively small number
of built-in operations.
• Many other useful operations are provided as a
collection of files called the C++ Standard Library.
• These library files make your job easier, because
they save you from having to re-invent the wheel
when you want to perform common tasks.
• A big part of becoming a C++ programmer is learning
about the many library files in the Standard Library.
The Standard Library (cont’d.)
• Each library file (or “header file”) has a name,
which is typically written inside angle brackets.
– Examples:
• <iostream>
• <string>
• <cmath>
Comments
• Comments are for the reader, not the compiler.
• Two types:
– Single line: begin with //
// This is a C++ program.
// Welcome to C++ Programming.
– Multiple line: enclosed between /* and */
/*
You can include comments that can
occupy several lines.
*/
Special Symbols
• List of special symbols in C++ :
Reserved Words (Keywords)
• Reserved words (or keywords):
– You cannot redefine these words.
– You cannot use them for anything other than their
intended use.
Examples:
– int
– using
– return
– See Appendix A (next slide) in textbook for complete list.
Tokens
• Tokens: the smallest meaningful units of a
program.
• C++ tokens include: special symbols, reserved
words, and identifiers.
Reserved words Identifier Special symbol
Identifiers
• An identifier is the name of something in a
program.
– Identifiers can contain letters, digits, and the
underscore character (_), but no other characters or
symbols.
– Identifiers must begin with a letter or underscore
(not a digit).
• C++ is case sensitive.
– For example, number, Number, and NUMBER are
three different identifiers.
Identifiers (cont’d.)
• Examples of legal identifiers in C++:
• first
• payRate2
• Employee_Salary
Data Types
• Data type: set of values together with a
set of allowed operations on those values.
• C++ data types fall into three categories:
– Simple data types
– Structured data types
– Pointers
• For the next few weeks we’ll mostly use
simple data types.
Partial Hierarchy of Data Types
Data Types
Integral Floating-Point
(int, bool, (float, Enumeration
char, …) double, …)
Simple Data Types
• Three categories of simple data:
– Integral: integers (numbers without a decimal).
• Can be further categorized:
– char, short, int, long, long long, bool,
unsigned char, unsigned short, unsigned
int, unsigned long
– Floating-point: decimal numbers.
• Can be further categorized:
– float, double, long double
– Enumeration type: user-defined data type.
Simple Data Types (cont’d.)
+= c+=y c=c+y
-= c-=3 c=c-3
*= c*=y+2 c=c*(y+2)
/= c/=4 c=c/4
%= c%=4 c=c%4
Prompt Lines
• Prompt lines are statements that tell the user what
to do.
cout << "Please enter a number between 1 and 10 and "
<< "press the return key." << endl;
cin >> num;