Basic Elements
Basic Elements
Basic Elements
■ Program structures
■ Data types and operators
■ Variables and declaration statements
2
1. Program Structure
■ A C++ program is a collection of functions.
■ Example:
DegToRad intersect addNums
FindMax1 _density slope
4
The main() function
■ The main() function is a special function that runs automatically
firstly when a program executes.
■ All C++ programs must include one main() function. All other
functions in a C++ program are executed from the main().
■ The first line of the function, in this case int main() is called a
function header line.
■ The function header line contains three pieces of information:
1. What type of data, if any, is returned from the function.
2.The name of the function
3. What type of data, if any, is sent into the function.
int main()
int getNumber(void)
int totalScore(int score1, int score2)
5
The main() function (cont.)
int main()
{
program statements in here
return 0;
}
All statements in C++ must end with a semicolon.
The line
return 0;
is included at the end of every main function. C++ keyword
return is one of several means we will use to exit a
function. When the return statement is used at the end of
main(), the value 0 indicates that the program has
terminated successfully.
6
The cout Object
■ The cout object is an output object that sends data
given to it to the standard output display device.
7
A simple program
Example 2.1.1
#include <iostream> header file
using namespace std;
int main()
{
cout << "Hello world!";
return 0;
}
8
The iostream classes
9
Preprocessor directives
10
i/o manipulator
■ An i/o manipulator is a special function that can be
used with an i/o statement.
■ The endl i/o manipulator is part of iostream classes
and represents a new line character.
■ Example:
11
Comments
■ Comments are lines that you place in your code to
contain various type of remarks.
■ C++ line comments are created by adding two
slashes (// ) before the text you want to use as a
comment.
Ex:
// this program calculates a square root
■ Block comments span multiple lines. Such
comments begin with /* and end with the symbols */.
Ex:
/* this program solves a quadratic equation:
ax2 + bx +c = 0
*/
12
2. DATA TYPES AND OPERATORS
Data Types
14
The Character Data Type
■ To store text, you use the character data type. To store one
character in a variable, you use the char keyword and place the
character in single quotation marks.
■ Example:
char cLetter = ‘A’;
■ Escape Sequence
■ Example:
\n move to the next line
\t move to the next tab
15
Arithmetic Operators
■ Arithmetic operators are used to perform mathematical
calculations, such as addition, subtraction, multiplication, and
division.
Operator Description
----------------------------------------------------------------------
+ Add two operands
- Subtracts one operand from another operand
* Multiplies one operand by another operand
/ Divides one operand by another operand
% Divides two operands and returns the remainder
16
Examples:
3+7
18 – 3
12.62 + 9.8
12.6/2.0
The output of the program:
Example 2.2.1 15.0 plus 2.0 equals 17
#include <iostream> 15.0 minus 2.0 equals 13
using namespace std; 15.0 times 2.0 equals 30
15.0 divided by 2.0 equals 7.5
int main()
{
cout << "15.0 plus 2.0 equals " << (15.0 + 2.0) << '\n'
<< "15.0 minus 2.0 equals " << (15.0 - 2.0) << '\n'
<< "15.0 times 2.0 equals " << (15.0 * 2.0) << '\n'
<< "15.0 divided by 2.0 equals " << (15.0 / 2.0) << '\n';
return 0;
}
17
Integer Division and % operator
■ Example:
9%4 is 1
17%3 is 2
14%2 is 0
18
Operator Precedence and Associativity
■ Expressions containing multiple operators are evaluated by the
priority, or precedence, of the operators.
Operator Associativity
--------------------------------------------
unary - Right to left
*/% Left to right
+- Left to right
▪ Example: 8 +5*7%2*4
5 * 7 => 35
35 % 2 => 1
1 * 4 => 4
8 + 4 => 12
19
3. VARIABLES
■ One of the most important aspects of programming
is storing and manipulating the values stored in
variables.
■ Variable names are also selected according to the
rules of identifiers:
■ made up of any combination of letters (a-z or A-Z),
digits (0-9), or underscores (_);
■ must begin within a letter or an underscore;
■ is case-sensitive;
■ must be different from reserved words.
20
Identifiers
21
Declaration Statements
■ In C++ you can declare the data types of variables using the
syntax:
type name;
■ Example:
int count;
float sum;
22
Rules of variable declaration
■ Rules:
1. A variable must be declared before it can be
used.
2. Declaration statements can also be used to store
an initial value into declared variables.
Example:
int num = 15;
float grade1 = 87.0;
23
Example 2.2.1
#include <iostream>
using namespace std;
int main()
{
float grade1 = 85.5;
float grade2 = 97.0;
float total, average;
total = grade1 + grade2;
average = total/2.0; // divide the total by 2.0
cout << "The average grade is " << average << endl;
return 0;
}
The output of the above program:
The average grade is 91.25
Let notice the two assignment statements in the above program:
total = grade1 + grade2; average = total/2.0;
■ Each of these statements is called an assignment statement
because it tells the computer to assign (store) a value into a
variable.
24