6. ď˝ Pakistan Air force Schools
ď˝ F.Sc. PAF Inter college Lahore
ď˝ B.E. Mechanical Engineering (CEME NUST)
ď˝ Pak Elektron Limtd. Lahore
ď˝ Millat Tractors Sheikhupura Road
ď˝ Descon Lahore
ď˝ Olayan Descon Saudia
ď˝ Descon Dubai
3/26/2015 6
7. ď˝ M.Sc. Sustainable Technology, Sweden
ď˝ Heritage-University of South Asia
3/26/2015 7
8. ď˝ C++ (See plus pluss)
ď˝ To follow syntax and rules of Turbo C++ and Borland
C+
ď˝ Text editors e.g. WordPad and note pad
ď˝ These are used to write the code
ď˝ Compilers are used to run the code
ď˝ Source code written and stored in first.cpp
ď˝ Compiled code stored/saved as first.obj
ď˝ Executed code stored as first.exe
3/26/2015 8
9. ď˝ Contains following three parts
⌠Preprocessor directives
⌠The main( ) Function
⌠C++ statement
3/26/2015 9
10. Preprocessor Directives
ď˝ Instructions given before beginning of actual program
ď˝ Also called Compiler Directives
ď˝ To define certain actions or special instructions (include
arithmetic equation)
ď˝ Normally begin with number sign (#)
ď˝ May include keywords like âincludeâ or âdefineâ
3/26/2015 10
13. Preprocessor Directives
Header file
ď˝ C++ source file
ď˝ Contains definitions of function/objects
ď˝ âincludeâ is used to add âiostreamâ into the program
ď˝ Has Large number of header files in which library
functions are defined
3/26/2015 13
14. ď˝ Example 1-01
#include <iostream>
using namespace std;
int main ( )
{
cout <<âthis is my first programâ;
return 0;
}
3/26/2015 14
17. Header file
ď˝ âiostreamâ is short of âinput output streamâ
ď˝ Has definitions of several input and output objects or
functions
ď˝ Which was it in the last example?
ď˝ âcoutâ (see out)
ď˝ Syntax
⌠#include <name of the header file>
3/26/2015 17
19. The main ( ) Function
ď˝ Indicates the beginning of a program
ď˝ After the preprocessor directives
ď˝ Must be included in every program
ď˝ Syntax
⌠main ( )
{
program statementsâŚ
}
3/26/2015 19
20. C++ Statements
ď˝ Syntax
⌠main ( )
{
program statementsâŚ
}
ď˝ The statements are written under main ( )
ď˝ Between { }
ď˝ Are the body of the program
ď˝ Each statement ends with â;â
3/26/2015 20
22. C++ Statements
Key words
ď˝ Words used by languages for
ď˝ Special purposes
ď˝ Also called âreserved wordsâ
ď˝ Cannot be used as variable names
3/26/2015 22
28. C++ Statements
Variables
ď˝ Not a constant
ď˝ Which may change during execution of a program
ď˝ Represented by a symbol or a name
ď˝ Nadeem, fox pro, x, y etc.
ď˝ It represents a storage location on a computer
ď˝ Data stored may change, the variable does not
ď˝ Also known as a object
ď˝ In C++ variable names consists of alphabets and digits
3/26/2015 28
29. C++ Statements
Rules for writing a variable name
ď˝ First character must be alphabetic, exception â_â
ď˝ No blank spaces
ď˝ Arithematic characters not allowed #, ^, etc
ď˝ Reserved words are not allowed
ď˝ Maximum length depends up on compiler
ď˝ Must be declared
ď˝ Again, case sensitive language
3/26/2015 29
30. Variables Valid/Invalid Remarks
Nadeem valid
perform valid
double invalid C++ reserved word
foxpro valid
switch invalid C++ reserved word
Marriam valid
int invalid C++ reserved word
3taq invalid Starts with numeral
unsigned invalid C++ reserved word
x-y invalid
Special character is not
allowed
Taq Ahd invalid Space is not allowed
3/26/2015 30
31. ď˝ Each variable (we define) is specified by the data types of
the data stored in it
ď˝ Each variable is Declared by its type
ď˝ C++ has 5 basic data types
⌠int Integer 25, 100, 5000
⌠float Floating Point 3.4Ă105
⌠double double precision 3.4Ă105
⌠char characters almost all
⌠bool Boolean logic type variables
3/26/2015 31
32. The âintâ Data Type
ď˝ Represents the integer type data
ď˝ Integer is a whole number!
ď˝ i.e. without fraction or decimal
⌠601, 250, -6, 501
ď˝ The range depends the computer system being used
⌠In MS-DOS range is -32768 to 32767
ď˝ Range can be changed by using following qualifiers
⌠short int
⌠long int
⌠unsigned int
3/26/2015 32
33. The âfloatâ Data Type
ď˝ Represents real or floating type data
ď˝ Real, decimal or exponential notation
ď˝ Float, signed or unsigned
3/26/2015 33
34. The âdoubleâ Data Type
ď˝ Represents real or floating type data
ď˝ Twice the storage capacity than âfloat data typeâ
ď˝ Long double Data type
3/26/2015 34
35. The âcharâ Data Type
ď˝ char stands for character
ď˝ Used to declare character type variables
ď˝ Variables, alphabetic characters, numeric digits and
special characters can be stored
ď˝ Twice the storage capacity than âfloat data typeâ
ď˝ Long double Data type
3/26/2015 35
36. Declaration of Variables
ď˝ Assigning the name a variable can hold
⌠Zohaib, sum, addition, x, xyz etc
ď˝ Assigning the data type a variable can hold
⌠int, float etc
ď˝ Example
⌠int a;
⌠int abc, xyz, d, s;
ď˝ Can be more than one variables but separated by a
comma (if of same data type)
3/26/2015 36
37. Declaration of Variables
ď˝ If not, written in each individual line where each
statement is ended with a â;â
ď˝ Syntax
⌠type list of variables
ď˝ Example
int a, xy;
Float b;
Char nm [15]
double sum;
3/26/2015 37
38. Initialization of Variables
ď˝ Before we declared a variable
ď˝ Declared variable gets a memory location assigned to it
that specifies it has a place in memory on the computer
ď˝ Now a value to the variable must also be assigned or
defined
ď˝ A known value is assigned to it
⌠int a = 110
ď˝ The statement can be written as
⌠int a = 110, b = 60, c;
3/26/2015 38
39. Example 1-03
#include<iostream.h>
using namespace std;
int main ( )
{
int abc = 4, b = 1997;
float xy = 3.4;
char name[15] = âMarriam Ahmedâ;
cout <<name<<endl;
cout<<abc<<endl;
cout<<b<<endl;
cout<<xy<<endl;
return 0;
}
3/26/2015 39
41. Constants
ď˝ A value that cannot be changed during execution of a
program
⌠int a = 44
ď˝ Four types of constants in C++
⌠Integer constants
⌠Floating point constants
⌠Character constants
⌠String constants
3/26/2015 41
42. Constants
Integer constants
ď˝ Numerical value without a decimal part
ď˝ + can also be used with it
ď˝ Integer constants are used in expressions for calculations
⌠int a = 44, b = 55;
sum = a+b;
cout<<sum<<endl;
3/26/2015 42
43. Constants
Floating-point contant
ď˝ Numeric values having both integer and decimal part
ď˝ Can also be written in exponential notation
ď˝ In exponential notation, can be written as
⌠123.5E2
⌠Where E represents the exponent
ď˝ both the constant and the E notation can be +
3/26/2015 43
44. Example 1-04
#include<iostream>
int main ( )
{
int r;
const float p = 3.14;
float peri;
r = 2;
peri = 2*p*r;
cout<<âResult is = â<<peri;
return 0;
}
3/26/2015 44
46. The âdefineâ Directive
ď˝ It is a preprocessor directive, used at the beginning of
the program
ď˝ Used to define a constant quantity
ď˝ Syntax
⌠#define identifier constant
⌠#define p 3.14
ď˝ Value defined is âpâ, where 3.14 is the constant assigned
to âpâ
ď˝ 3.14 remains same throughout the program
ď˝ âpâ can not be used again in the program to define other
3/26/2015 46
47. The âdefineâ Directive
ď˝ Identifier does not have any data type but any data type
can be assigned to it
3/26/2015 47
50. Arithmetic Operators
ď˝ Symbols that represent arithmetic operations
ď˝ Used in arithmetic expressions
ď˝ Following are the arithmetic operators in C++
Operators Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% For remainder
3/26/2015 50
51. Example 1-06
#include <iostream.h> redundant lines will be excluded
using namespace std;
int main ( )
{
int d, p, s, m, r;
p = 5+2;
s = 5-2;
m = 5*2;
d = 5/2;
r = 5%2;
cout<<âAddition of 5 & 2 is =â<<p<<endl;
cout<<âSubtraction of 5 & 2 is =â<<s<<endl;
cout<<âmultiplication of 5 & 2 is =â<<m<<endl;
cout<<âdivision of 5&2 is =â<<d<endl;
cout<<âremainder of 5/2 is =â<<r<<endl; }
3/26/2015 51
53. Arithmetic Expression
ď˝ It is a combination of variables, constants and arithmetic
operators
⌠p = m*x+100 where m=10 and x=5
ď˝ Used to calculate value of an arithmetic formula
ď˝ Returns a single value after evaluation
ď˝ Here â=â is called the assignment operator
ď˝ After evaluation the resultant variable is called the
âreceiving variableâ
⌠res = m*x+100
3/26/2015 53
54. Order of Precedence of Operation
ď˝ It is the order in which the expression is evaluated
ď˝ C++ only performs one operation at a time
ď˝ Following is the order of precedence
⌠All multiplications and divisions are performed first from left to
right
⌠All additions and subtractions are then performed left to right
⌠If parenthesis are used in an expression, the expressions within
parentheses are first computed from left to right
⌠When parentheses are used within parentheses, the expression
within innermost parentheses is evaluated first
3/26/2015 54
55. Order of Precedence of Operation
ď˝ Example (4-(3*5))+2 is evaluated as follows
ď˝ First?
ď˝ Solution
⌠(3*5) is computed and returns value of 15.
⌠4-15 is computed and returns value of -11
⌠-11+2 is computed and returns the value of -9
3/26/2015 55
56. ď˝ Statements used to get some data and assign to variables
are input statements
⌠Int a=10, b=510
ď˝ Statements used to receive data from computer memory
and send to output devices (monitor) are output
statements
⌠cout<<âyour total GPA is = â<<m<<endl;
3/26/2015 56
57. The âcoutâ object âOutput Stream
ď˝ The flow of data from-and-to a device is called a stream
ď˝ âcoutâ (see-out) stands for console out
ď˝ Here the console out is the display screen (monitor)
ď˝ âcoutâ is used as an output statement and is part of
iostream header file
ď˝ Example
⌠cout<<âOne kilobyte = â<<1024<<âbytesâ;
ď˝ There are two string constants, one numeric constants
and three put to operators
3/26/2015 57
60. Example 1-08
#include <iostream>
#include <conio> used for functions such as
clearing the screen
int main ( )
{
clrscr ( )
cout<<âC++is a powerful programming languageâ;
cout<<âUNIX operating system is written in C++â;
cout<<âit is an easy to learn languageâ;
return 0;
}
3/26/2015 60
64. The Escape Sequence
ď˝ âendlâ is similar to an escape sequence
ď˝ Do you remember what it did?
ď˝ These are special non-printing characters
ď˝ Used to control printing on the output device
ď˝ Combination of ââ and a code character
ď˝ For example, n is an escape sequence which is used to
transfer the printing control to a new line
ď˝ Used inside a string constant or independently
ď˝ In single or double quotes
3/26/2015 64
65. The Escape Sequence
ď˝ Can be used anywhere in the output stream
ď˝ for example
⌠cout<<âI Love Pakistannâ;
⌠cout<<âI n Love n Pakistanâ;
3/26/2015 65
66. The Escape Sequence
Escape Sequence Explaination
a sounds an alert or alarm
b
backspace, print sequnce moves
a space back
cout<<"Pakistanb";
cout<<"Punjab";
the ouptput will be
PakistaPunjab
3/26/2015 66
67. The âendlâ Manipulator
ď˝ Is an important and most common used manipulator
ď˝ These are the operators used with put to (<<) operators
ď˝ Stands for end of line
ď˝ Has the same effect as ânâ
ď˝ For example
⌠cout<<âC++ nâ<<âprogramming nâ<<âlanguageâ;
ď˝ Is equivalent to
⌠Cout<<âC++ â<<endl<<âprogramming â<<endl<<âlanguageâ;
3/26/2015 67
70. The âsetwâ Manipulator
ď˝ Stands for set width
ď˝ Used to set width of the output on the output on the
screen
ď˝ The output is left-justified
ď˝ Syntax
⌠setw(n) where n is the specified width and is an integer
ď˝ For example
⌠cout<<setw(10)<<âPakistanâ<<setw(15)<<âIslamabadâ;
ď˝ Is a part of âiomanip.hâ header file
3/26/2015 70
74. Example 1-12 (Assignment Statement)
#include <iostream.h>
int main ( )
{
int a, b, c;
a=200;
b=100;
c=a
a=b
b=c
cout<<âvalue of a = â<<a<<endl;
cout<<âvalue of b = â<<b<<endl;
}
3/26/2015 74
78. The âcinâ Objectâ Input Stream
ď˝ âcinâ (see-in) stands for console input
ď˝ This is an input stream
ď˝ It is a part of iostream header file
ď˝ It requires you to input from your keyboard during the execution
of a program
ď˝ Value is input and press Enter to input the value
ď˝ Syntax
ď˝ cin>>var1[>>var2âŚ.]; >> is an extraction operator or get from
operator
ď˝ Usually a separate statement is used for each variable
ď˝ For example
⌠cin>>a>>b>>c; and press Enter to for typing each data
3/26/2015 78
79. Example 1-14
#include <iostream.h>
int main ( )
{
int n1, n2, s, p;
cout<<âEnter the first number ? â;
cin>>n1;
cout<<âEnter the second number ? â;
cin>>n2;
s=n1+n2;
p=n1*n2
cout<<âSum of numbers = â<<s<<endl;
cout<<âProduct of numbers = â<<p<<endl;
}
3/26/2015 79
81. Example 1-15
#include <iostream>
{
int age;
long int age_mon;
char name[50];
cout<<âEnter the name of the person â;
cin>>name;
cout<<âEnter the age of person in years â;
cin>>age;
age_mon = age*12;
cout<<âName of the person = â<<name<<endl;
cout<<âAge in months = â<<age_mon<<endl;
}
3/26/2015 81
83. Example 1-16
float avg;
char name[20];
int total, cpp, os, edp;
cout<<âEnter the name of the student â;
cin>>name;
cout<<âEnter the marks obtained in C++â;
cin>>cpp;
cout<<âEnter the marks obtained in Operating Systemsâ;
cin>>os;
cout<<âEnter the marks obtained in EDPâ;
cin>>edp;
total = cpp+os+edp;
avg = total/3.0;
cout<<âName of the student = â<<name<<endl;
cout<<âTotal Marks = â<<total<<endl;
cout<<âAverage Marks = â<<avg<<endl;
3/26/2015 83
85. Example 1-17
{
float c, f;
cout << "Enter the temperature in Fahrenheit? ";
cin>>f;
c=(f-32)*5.0/9.0;
cout<<"The temperature in Celsius is = "<<c<<endl;
return 0;
}
3/26/2015 85
87. Example 1-18
{
float r,h,v;
cout << "Enter the radius of the cylinder = ";
cin>>r;
cout << "Enter the height of the cylinder = ";
cin>>h;
v=3.14*r*r*h;
cout<<"The volume of the cylinder is = "<<v;
return 0;
}
3/26/2015 87
89. Compound Assignment Statement
ď˝ We have previously used simple statements, to assign
values to a variable
ď˝ Like
⌠m=x*100+50
⌠v=p*r*r*h
ď˝ The same assignment statements can be used to assign
one value to more than one variable.
ď˝ E.g.
⌠x = y = 16;
3/26/2015 89
90. Example 1-19
{
int x, y, a, b, c, s;
x = y = a = b = c = 515;
s = x+y+a+b+c;
cout << "the sum is = " <<s<< endl;
return 0;
}
3/26/2015 90
92. Compound Assignment Expression
ď˝ Is used to add, subtract, multiply etc a value to or from a
variable
⌠Without writing on either side of op â=â
ď˝ The arithmetic operator is combined with the
assignment operator (=)
ď˝ Syntax
⌠var op = expression
ď˝ xy = xy + 10;
⌠Here 10 is added to the variable xy
⌠Where xy already has some value
3/26/2015 92
93. Compound Assignment Expression
ď˝ Which can also be written as
ď˝ xy + = 10;
ď˝ Similarly x += 9; is the same as?
⌠x = x + 9;
ď˝ x -=9; is the same as?
⌠x = x â 9;
3/26/2015 93
96. The Comment Statement
ď˝ Used to show comment on the coding statements in a
program
ď˝ Used to explain the logic of the program
ď˝ It is a non-executable statement
⌠// This is a my first program in C++
⌠// Java language is similar to C++
ď˝ Can be given in any line of the program
ď˝ e.g.
⌠sum=a+b //used to take sum of two variables
3/26/2015 96
97. Example 1-21
{
// declare the variables
int a,c; //a&c are variable names
a=10; //value 10 is assigned to a
c=a*a*a; //assignment statement to calculate cube
/* next statement is the output statement to
print the cube of 10 */
cout << "cube of "<<a <<" = "<<c<< endl;
// this is the end of the program
return 0;
}
3/26/2015 97