Ch02 C++ Programming Basics
Ch02 C++ Programming Basics
//first.cpp
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
cout << "Welcome to this course\n";
system("PAUSE");
return 0;
}
Explanation:
This program consists of a single function
called main().
1
2
3
cout
<<"Welcome to this course\n"
;
Program Statements
String Constants
Directives
#include Directive
Header Files
using Directive
Comments
Comments help the person writing a program,
and anyone else who must read the source file,
understand whats going on.
The compiler ignores comments, so they do not
add to the file size or execution time of the
executable program.
Comments start with a double slash symbol (//)
and terminate at the end of the line
1
2
3
4
5
6
7
8
//first.cpp
#include <iostream> // preprocessor directive
using namespace std; // "using" directive
int main()
// function name "main"
{
// start function body
cout <<"Welcome to this course\n";
// statement
return 0;
// statement
}
// end of function body
Integer Variable
//
//
//
//
//
//
define
define
assign
assign
output
output
an integer var1
an integer var2
value 20 to var1
value 20+10 to var2
text
value of var2
system("PAUSE");
// Suspend the processing of Program
return 0;
// return the value 0 to whoever called it
}
Code Explanation
Variable Names
keyword
Integer constant
cout <<
Size
1 byte
Range
true(1) or false(0)
char or
signed char
1 byte
(8 bits)
unsigned char
1 byte
(8 bits)
0 ~ 28-1 = 0 to 255 or
256 Different ASCII Characters
short or
signed short
2 bytes
(16 bits)
unsigned short
2 bytes
(-215) ~ (+215-1) =
-32,768 ~ +32,767
0 ~ 216-1 = 0 to 65,535
int or
signed int
4 bytes
(32 bits)
(-231) ~ (+231-1) =
-2,14,74,83,648 ~ +2,14,74,83,647
unsigned int
4 bytes
(32 bits)
0 ~ (232 - 1) =
0 ~ 4,29,49,67,295
long
4 bytes
(32 bits)
(-231) ~ (+231-1) =
-2,14,74,83,648 ~ +2,14,74,83,647
Size
Range
unsigned long
4 bytes
0 ~ (232 - 1) =
(32 bits) 0 ~ 4,29,49,67,295
float
4 bytes
- (1.2 10-38 ~ 3.4 1038)
(32 bits) + (1.2 10-38 ~ 3.4 1038)
double
8 byte
- (2.2 10-308 ~ 1.7 10308)
(64 bits) + (2.3 10-308 ~ 1.7 10308)
long v1 = 7678L;
Dec
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Hex Char
Dec
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
10
11
12
13
14
15
16
17
18
19
1A
1B
1C
1D
1E
1F
20
21
22
23
24
25
26
27
28
29
2A
2B
2C
2D
2E
2F
30
31
32
33
34
35
36
37
38
39
3A
3B
3C
3D
3E
3F
40
41
42
43
44
45
46
47
48
49
4A
4B
4C
4D
4E
4F
50
51
52
53
54
55
56
57
58
59
5A
5B
5C
5D
5E
5F
60
61
62
63
64
65
66
67
68
69
6A
6B
6C
6D
6E
6F
70
71
72
73
74
75
76
77
78
79
7A
7B
7C
7D
7E
7F
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
80
81
82
83
84
85
86
87
88
89
8A
8B
8C
8D
8E
8F
90
91
92
93
94
95
96
97
98
99
9A
9B
9C
9D
9E
9F
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
!
"
#
$
%
&
'
(
)
*
+
,
.
/
0
1
2
3
4
5
6
7
8
9
:
;
<
=
>
?
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
@
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
[
\
]
^
_
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
`
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
{
|
}
~
160 A0
161 A1
162 A2
163 A3
164 A4
165 A5
166 A6
167 A7
168 A8
169 A9
170 AA
171 AB
172 AC
173 AD
174 AE
175 AF
176 B0
177 B1
178 B2
179 B3
180 B4
181 B5
182 B6
183 B7
184 B8
185 B9
186 BA
187 BB
188 BC
189 BD
190 BE
191 BF
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
Hex Char
C0
C1
C2
C3
C4
C5
C6
C7
C8
C9
CA
CB
CC
CD
CE
CF
D0
D1
D2
D3
D4
D5
D6
D7
D8
D9
DA
DB
DC
DD
DE
DF
E0
E1
E2
E3
E4
E5
E6
E7
E8
E9
EA
EB
EC
ED
EE
EF
F0
F1
F2
F3
F4
F5
F6
F7
F8
F9
FA
FB
FC
FD
FE
FF
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
Programming Example
// charvars.cpp
// demonstrates character variables
#include <iostream>//for cout, etc.
using namespace std;
int main()
{
char charvar1 = 'A';
// char charvar1 = 65;
char charvar2 = '\t'; // char charvar2 = 9;
cout << charvar1;
// display character
cout << charvar2;
// display character
charvar1 = 'B';
// char charvar1 = 66;
cout <<charvar1;
// display character
cout << '\n';
// try cout << char (10);
system("PAUSE");
return 0;
}
Escape sequence
Escape sequence
This translates to
"Run,
Forrest, run," she said.
Sometimes you need to represent a character
constant that doesnt appear on the keyboard,
such as the graphics characters above ASCII
code 127.
To do this, you can use the '\xdd' representation, where each d stands for a hexadecimal
digit. If you want to print a solid rectangle,
for example, youll find such a character
listed as decimal number 178, which is
hexadecimal number B2 in the ASCII table.
cout <<'\x01'; cout<<char(1);
// prints
cout <<'\a';
cout<<char(7);
// calls bell
cout << int('\a');
// Prints 7
ASCII TABLE
#include <iostream>
#include <conio.h>
using namespace std;
int main(){
unsigned char ch;
for(ch=0 ; ch<255 ; ch++)
cout << int(ch) << "="
<< ch << "\n" ;
getch();
return 0;
Float Example
// circarea.cpp
// demonstrates floating point variables
#include <iostream> //for cout, etc.
using namespace std;
int main()
{
float rad;
// prompt
// get radius
// find area
cout << "Area is " << area << endl; // display answer
system("PAUSE"); return 0;
}
Code Explanation
Quadratic Equation: Y
= X2-6X-7
// prompt
cin >> X;
// get value of x
Y = (X*X)-(6*X)-7;
// find y
// display answer
cout << "For X = " << X << ",Y = " << Y << endl;
system("PAUSE");
return 0;
}
Assignment #1
What is the correct variable type for storing the following
data:
1.
2.
3.
Type Conversion
// shows mixed expressions
#include <iostream>
using namespace std;
int main(){
int count = 7;
float avgWeight = 155.5F;
double totalWeight = count * avgWeight;
/* the lower-type variable is converted to the type of the
higher-type variable. Thus the int value of count is
converted to type float and stored in a temporary
variable before being multiplied by the float variable
avgWeight. The result (still of type float) is then
converted to double so that it can be assigned to the
double variable totalWeight
*********/
cout << "totalWeight=" << totalWeight << endl;
system("PAUSE");
return 0;
}
Type Conversion
Type Cast
Type Conversion
// cast.cpp
// tests signed and unsigned integers
#include <iostream>
using namespace std;
int main()
{
int intVar = 1500000000;
//1,500,000,000
intVar = (intVar * 10) / 10;
//result too large
cout << "intVar = " << intVar << endl; //wrong answer
intVar = 1500000000;
//cast to double
intVar = (static_cast<double>(intVar) * 10) / 10;
cout << "intVar = " << intVar << endl; //right answer
system("PAUSE"); return 0;
}
remainder operator
//
//
//
//
//
//
//
6
7
0
1
2
-2
-5
// displays 10
Assignment #2