Lecture 5 C++
Lecture 5 C++
Lecture 5
By:
Faiz Ur Rehman
Lecturer in Computer Science
[email protected]
1
Contents
• How C++ program works
• Identifiers (User Defined Words)
• Keywords (Reserved Words)
• Data types in C++
• Variables in C++
• Variable declaration in C++
• Variable definition in C++
• Constants in C++
• Variable as memory locations
• Inputting with cin statement
• Problem Examples
• Programming Tasks
2
10
12
13
14
15
17
option
18
19
20
21
22
23
radius
56.214
24
26
Error
Message
27
28
29
30
31
32
33
34
SPEEDOFLIGHT
• In second example, we have a constant whose name is
SPEEDOFLIGHT, its data types is floating and its value
is 3 x 108. 3E8
35
cout<<"Enter Radius:";
cin>>radius;
areacircle =PI*radius*radius;
cout<<areacircle;
return 0;
}
37
2000H
2001H
int num = 26 ; 2002H
num
2003H
26 2004H
2005H
2006H 38
short V1 = 69 ; 2003H
2004H
2005H
2006H
2007H
2008H
2009H
200AH
200BH
200CH
200DH
200EH
200FH
2010H 40
2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Variables as Memory Locations
MEMORY
V1 2000H
69 2001H
V2 w 2002H
2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Variables as Memory Locations
MEMORY
V1 2000H
69 2001H
V2 w 2002H
int V3 = 5478 ; V3
5478
2003H
2004H
2005H
2006H
2007H
2008H
2009H
200AH
200BH
200CH
200DH
200EH
200FH
2010H 42
2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Variables as Memory Locations
MEMORY
V1 2000H
69 2001H
V2 w 2002H
float V4 = 87.245F ; V3
5478
2003H
2004H
2005H
2006H
V4 2007H
2008H
87.245
2009H
200AH
200BH
200CH
200DH
200EH
200FH
2010H 43
2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Variables as Memory Locations
MEMORY
• Consider following memory map: V1
2000H
2001H
int V3 = 1453 ; V3
200BH
200CH
1453
200DH
char V4 = ‘b’ ; 200EH
V4 b 200FH
2010H
44
2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Variables as Memory Locations
MEMORY
• Execute the following variables definition V1
2000H
2001H
statements in the same memory map: 2002H
69.258
2003H
2004H
short V7 = 74 ; V3
200AH
200BH
200CH
1453
200DH
200EH
V4 b 200FH
2010H 45
2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Variables as Memory Locations
MEMORY
2000H
V1 2001H
2002H
69.258
2003H
2004H
short V5 = 985 ; V5
985
2005H
2006H
V2 2007H
658
2008H
2009H
200AH
V3 200BH
200CH
1453
200DH
200EH
V4 b 200FH
2010H 46
2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Variables as Memory Locations
MEMORY
V6 r 2000H
V1 2001H
2002H
69.258
2003H
2004H
V5 2005H
985
char V6 = ‘r’ ; V2
2006H
2007H
658
2008H
2009H
200AH
V3 200BH
200CH
1453
200DH
200EH
V4 b 200FH
2010H 47
2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Variables as Memory Locations
MEMORY
V6 r 2000H
V1 2001H
2002H
69.258
2003H
2004H
short V7 = 74 ;
V5 2005H
985
2006H
V2 2007H
658
2008H
V7 2009H
74
200AH
V3 200BH
200CH
1453
200DH
200EH
V4 b 200FH
2010H 48
2011H
Faiz Ur Rehman, Lecturer in Computer Science, GPGC Kohat
Inputting with cin
49
50
51
52
53
54
55
56
57
58
59