Basic Elements in C++: Programming Fundamentals 1
Basic Elements in C++: Programming Fundamentals 1
Programming Fundamentals 1
Chapter 2
n Program structures
n Data types and operators
n Variables and declaration statements
n Integer quantifiers
n Some sample programs
Programming Fundamentals 2
Modular programs
Programming Fundamentals 3
Function
n Each function must have a name.
n Names or identifiers in C++ can made up of any
combination of letters, digits, or underscores selected
according to the following rules:
- Identifiers must begin within an uppercase or lowercase ASCII
letter or an underscore (_).
- You can use digits in an identifier, but not as the first character.
You are not allowed to use special characters such as $, &, * or %.
- Reserved words cannot be used for variable names.
n Example:
Programming Fundamentals 4
The main() function
n The main() function is a special function that runs automatically
when a program first executes.
n All C++ programs must include one main() function. All other
functions in a C++ program are executed from the main().
n The first line of the function, in this case int main() is called a
function header line.
Programming Fundamentals 5
The main() function (cont.)
int main()
{
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
terminates successfully.
Programming Fundamentals 6
The cout Object
n The cout object is an output object that sends data
given to it to the standard output display device.
Programming Fundamentals 7
A simple program
Example 2.1.1
#include <iostream.h> header file
int main()
{
cout << "Hello world!";
return 0;
}
Programming Fundamentals 8
The iostream classes
#include <iostream.h>
Programming Fundamentals 9
Preprocessor directives
Programming Fundamentals 10
i/o manipulator
n An i/o manipulator is a special function that can be
used with an i/o statement.
n The endl i/o manipulator is part of iostream classes
and represents a new line character.
n Example:
Programming Fundamentals 11
Comments
Programming Fundamentals 12
Example:
int main()
{
/*
This line is part of the block comment.
This line is also part of the block
comment.
*/
cout << “Line comment 1 “;
cout << “Line comment 2 “;
// This line comment takes up an entire line.
return 0;
}
Programming Fundamentals 13
DATA TYPES AND OPERATORS
Data Types
Programming Fundamentals 14
Integer data type
Programming Fundamentals 15
Floating Point Numbers
Programming Fundamentals 16
The Character Data Type
n 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.
n Example:
char cLetter = ‘A’;
n Escape Sequence
n Example:
\n move to the next line
\t move to the next tab
Programming Fundamentals 17
Arithmetic Operators
n 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
Programming Fundamentals 18
Examples:
3+7
18 – 3
12.62 + 9.8
12.6/2.0
Example 2.2.1
#include <iostream.h>
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;
}
Programming Fundamentals 19
Integer Division
n Example:
9%4 is 1
17%3 is 2
14%2 is 0
Programming Fundamentals 20
Operator Precedence and Associativity
n 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
¯ ¯¯ ¯
4 1 2 3
Programming Fundamentals 21
VARIABLES
n One of the most important aspects of programming
is storing and manipulating the values stored in
variables.
n Variable names are also selected according to the
rules of identifiers:
- Identifiers must begin with an uppercase or
lowercase ASCII letter or an underscore (_).
- You can use digits in an identifier, but not as the first
character. You are not allowed to use special characters
such as $, &, * or %.
- Reserved words cannot be used for variable
names.
Programming Fundamentals 22
Identifiers
Programming Fundamentals 23
Declaration Statements
n In C++ you can declare the data types of variables using the
syntax:
type name;
n Example:
int sum;
long datenem;
double secnum;
Programming Fundamentals 24
Rules of variable declaration
n 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;
Programming Fundamentals 25
Example 2.2.1
#include <iostream.h>
int main()
{
float grade1 = 85.5;
float grade2 = 97.0;
float total, average;
Programming Fundamentals 27
Display a Variable’s Address
n Every variable has three major items associated with
it:
- its data type
- value
- the address of the variable.
Programming Fundamentals 28
Example 2.2.2
#include <iostream.h>
int main()
{
int num;
num = 22;
cout << "The value stored in num is " << num << endl;
cout << "The address of num = " << &num << endl;
return 0;
}
n Example:
long integer days;
unsigned int num_of_days;
Programming Fundamentals 30
Unsigned integers
Programming Fundamentals 31
Data Type Conversions
n An expression that contains only integer operands is called an
integer expression, and the result of the expression is an
integer value.
Programming Fundamentals 32
Data Type Conversion Rules
The general rules for converting integer and floating point
operands in mixed mode expressions were as follows:
Programming Fundamentals 33
n Note: Converting values to lower types can result in incorrect
values. For example, the floating point value 4.5 gives the value
4 when it is converted to an integer value.
Data types
--------------
long double ¬ highest type
double
float
unsigned long
long int
unsigned int
int
short in
char ¬ lowest type
Programming Fundamentals 34
Determining Storage Size
n C++ provides an operator for determining the
amount of storage your compiler allocates for each
data type. This operator is the sizeof() operator.
n Example:
sizeof(num1)
sizeof(int)
sizeof(float)
Programming Fundamentals 35
Example 2.4.1
#include <iostream.h>
int main()
{
char c;
short s;
int i;
long l;
float f;
double d;
long double ld;
cout << "sizeof c = " << sizeof(c)
<< "\tsizeof(char) = " << sizeof( char )
Programming Fundamentals 36
<< "\nsizeof s = " << sizeof(s)
<< "\tsizeof(short) = " << sizeof( short )
<< "\nsizeof i = " << sizeof (i)
<< "\tsizeof(int) = " << sizeof( int )
<< "\nsizeof l = " << sizeof(l)
<< "\tsizeof(long) = " << sizeof( long )
<< "\nsizeof f = " << sizeof (f)
<< "\tsizeof(float) = " << sizeof(float)
<< "\nsizeof d = " << sizeof (d)
<< "\tsizeof(double) = " << sizeof(double)
<< endl;
return 0;
}
Programming Fundamentals 37
The output of the above program:
sizeof c = 1 sizeof(char) = 1
sizeof s = 2 sizeof(short) = 2
sizeof i = 4 sizeof(int) = 4
sizeof l = 4 sizeof(long) = 4
sizeof f = 4 sizeof(float) = 4
sizeof d = 8 sizeof(double) = 8
Programming Fundamentals 38