HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
Chapter 2
1. Structure of C++ program
Documentation section
Linking section
Definition section
Global declaration section
Function
main() functions
Chapter Two
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL1
2. Documentation Section: comment parts, which are
ignored by compiler. The comments are used to describe
the functionality of each statement in the program, its
copyright, author, and date of compilation and purpose of
the program to the user.
Syntax: //This statement is a single line comment
/*This is multiline comment*/
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL2
3. Link section:
link the necessary files and libraries to current files
Includes pre-processer directives as well as header
files
Syntax: #include<file or library name>
e.g. #include<iostream.h>
#include<iomap.h>
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL3
4. Definition Section:
It is possible to define symbolic constants.
A symbolic constant is a constant value given to a
name which can't be changed in program
Syntax: #define identifier constant
e.g #define PI 3.14
Global declaration Section: variables declared under
this section are available throughout the program.
syntax: data type variable
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL4
5. main ( ) function:
The execution of the program starts from main function.
Every program must have only one main function.
All other functions are called either directly or indirectly
from main
It is independent from whether it is at the beginning, at the
end or by the middle of the code
its content is always the first to be executed when a
program starts
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL5
6. Syntax and Semantics
Syntax :
consists of the rules for the correct use of the language.
This involves the correct grammatical construction and
arrangement of the language, correct spelling.
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL6
7. Semantics:
deal with the meanings given to syntactically correct
constructs of the language.
is defined in terms of the program’s run-time behavior
A program could be syntactically correct yet
meaningless
Example
sum=0;
while (sum!=-1)
sum=sum+10;
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL7
8. 11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL8
C++ Compilation Process A
program written in C++ must be translated into a
machine language in order to be executed by a
CPU.
The process of translating high-level language
into machine language
The compilation process consists of the following
steps. edit source code -> compile -> link -
> execute (editor) (compiler) (linker)
(loader)
9. The parts of a simple C++ Program
C++ program file should be saved with file name extension
“ .CPP ”
#-is a signal to the preprocessor
include - is a preprocessor instruction that directs the
compiler to include a copy of the file specified in the angle
brackets in the source code.
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL9
10. Cont’d
{ } : signals the beginning of the main function body and the end
of the main function body
; statement ends with semicolon
Cout: is an object used for printing data to the screen. followed
by the insertion operator also called output redirection operator
(<<)
Syntax: cout<<Object;
Cin:
used for taking input from the keyboard. followed by the
input redirection operator (>>) also called extraction
operator.
take value from the keyboard and store it in the memory
Syntax: cin>>Object
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL10
11. return 0;
makes the main() function to finish and return the code
that the instruction is followed by, in this case 0.
most usual way to terminate a program that has not found
any errors during its execution.
Valid names—Identifiers
Within a program names are used to represent variables,
constants and functions.
When choosing valid identifiers for your variables,
constants and functions, the following rules will be applied.
1) an identifier contains a series of letters, numbers, or
underscore ( _ )
2) the first character must be a letter or underscore
3) no restriction on the length of an identifier
4) C++ keywords are reserved and can not be used as names
Cont’d
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL11
12. Comments on C++ programs
text which explains some aspect of a program.
Ignored by the compiler
Their purpose is only to allow the programmer to insert
notes or descriptions embedded within the source code
C++ provides two types of comment
i. Single Line Comment:
Eg: cout<<var1; //this line prints the value of var1
ii. Multiple Line Comment:
Eg: /*this is a kind of comment where
Multiple lines can be enclosed in
one C++ program */
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL12
13. Variables and Constants
Variable:
is a reserved place in memory to store information in.
is used for holding data values
All variables have three important properties:
Data Type: describes the property of the data and the size
of the reserved memory
Name: used to refer to the value in the variable
Value: assigning a new value to the variable
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL13
14. Declaring Variables
Variables can be created in a process known as declaration.
Good variable names indicate the purpose of the variable or
they should be self descriptive.
E.g. int myAge
Syntax: Data type Variable Name;
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL14
Keywords
having a predefined meaning in the language
we cannot use them as variable names or any other
purpose other than their predefined usage.
15. Initializing Variables
When declaring a variable, its value is by default
undetermined.
When a variable is assigned a value at the time of
declaration, it is called variable initialization
The syntax is:
Data Type variable name = initial value;
e.g. int a = 0;
or: int a;
a=0;
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL15
16. Scope of Variables
There are two types of variable visibilities.
Global variables:
are variables that can be referred/ accessed anywhere
in the code, when any function, as long as it is declared
first.
A variable declared before any function immediately
after the include statements are global variables.
Local Variables:
the scope of the local variable is limited to the
code level or block within which they are declared.
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL16
17. Characters
Char in C++ are represented as any value inside
a single quote
E.g.: ‘x’, ‘A’, ‘5’, ‘a’, etc.
Expressions and Statements
All C++ statements end with a semicolon
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL17
18. Constants
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL18
A constant is any expression that has a fixed value.
Like variables, constants are data storage locations in
the computer memory.
But, constants, unlike variables their content cannot
be changed after the declaration.
Constants must be initialized when they are created
by the program, and the programmer can’t assign a
new value to a constant later.
C++ provides two types of constants: literal and
symbolic constant
e.g int studentperclass=15;
student=class*studentperclass
19. Operators
is a symbol that makes the
machine to take an action
C++ provides several
categories of operators
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL19
21. Assignment operator (=)
Syntax: Operand1=Operand2;
Compound assignment operators: is the
combination of the assignment operator with
other operators like arithmetic and bit wise
operators.
Example:
(+=, -=, *=, /=, %=, >>=, <<=, &=, ^=)
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL21
22. Arithmetic operators (+, -, *, /, %)
Except for remainder or modulo (%), all other arithmetic
operators can accept a mix of integers and real operands
Integer division always results in an integer outcome.
E.g.:
9/2 gives 4 not 4.5
-9/2 gives -4 not -4.5
Division of integer by integer will not round off to the next
integer
E.g.:
int cost = 100;
int volume = 80;
double unitPrice = cost/ (double) volume;
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL22
23. Cont’d
The modulo(%) is an operator that gives the remainder
of a division of two integer values
Relational operator (==, !=, > , <, >=, <=)
Is a comparison of two expressions
The result of a relational operator is a Boolean value
that can only be true or false
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL23
24. Logical Operators (!, &&, ||)
Logical negation (!)
is a unary operator
negates the logical value of its
operand.
If its operand is non zero, it
produces 0, and if it is 0 it
produce 1.
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL24
25. Cont’d
Logical AND (&&)
produces 0 if one or both of its operands
evaluate to 0 otherwise it produces 1.
Logical OR (||)
produces 0 if both of its operands
evaluate to 0 otherwise, it produces
1.
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL25
26. Increment/Decrement Operators: (++)
and (--)
The auto increment (++) provide a
convenient way of adding 1 from a
numeric variable.
auto decrement (--) operators
provide a convenient way of
subtracting 1 from a numeric
variable.
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL26
27. Prefix and Postfix:
prefix :
• is written before the variable.
• is evaluated before the assignment.
postfix :
• appears after the variable name.
• is evaluated after the assignment.
• Prefix and postfix operators can not be used
at once on a single variable.
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL27
28. The size of() Operator
used for calculating the size of any
data item or type.
It takes a single operands
The outcome is totally machine
dependent.
E.g.:
a = size of(char)
b = size of(int)
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL28
29. Explicit type casting operators
allows you to convert a datum of a
given type to another data type.
E.g.
int i;
float f = 3.14;
i = (int)f; equivalent to i = int(f);
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL29
30. Operator Precedence
Operators in higher levels take
precedence over operators in
lower levels.
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL30
33. Debugging
Correcting programming errors
Detecting ,checking abnormalities
Also ,we said step by step procedure to find out
errors and removing it
Programming errors
-three types of errors
Syntax error
Runtime error
Logical error
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL33
34. Syntax error
This error occurs due to following reason.
Not following the grammatical rules used in
declaration of identifier.
Not declaring an identifier used in program.
Not terminating statement by semicolon
Not providing equal number of opening and
closing braces etc.
These errors can be rectified by the user as it is
displayed while compiling the program.
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL34
35. Runtime error
This error occurs while running a program by displaying
the message listed below.
Division by 0.
Overflow,
E.g. ,memory issue
Harmful or malicious applications like computer viruses or
Hadware
when a program calls too many methods or nested
processes
Underflow
is a condition in a computer program where the result of a
calculation is a number of smaller
occur when the true result of a floating point operation is
smaller in magnitude (that is, closer to zero) than in the target
data type
underflow also results in not having the result saved in the
memory of the application or device.
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL35
36. Logical error
This error won’t be displayed on the screen. However it will lead
to display wrong results.
Example: An infinite loop.
#include <iostream.h>
int main(){
int n = 1;
while( n > 0){
cout << n << endl;
n++; }
return 0;
}
This error lead to abnormal termination of a program or infinite
loop.
These errors solely depend on the logical thinking of the
programmer and are easy to detect if we follow the line of
execution and determine why the program takes that path of
execution.
11/2/2019
DMU ,INTRON_ TO C++ ECEG SEE IT IN
DETAIL36