Computer Programming: Lecture #1
Computer Programming: Lecture #1
Computer Programming
Lecture #1
Introduction
Muhammad Arif
[email protected]
Programming
The process of writing computer programs.
How can we write a program?
What we need?
LANGUAGE
Language
What is a language?
What is a programming language?
Basic types of computer languages:
Machine Language
Assembly Language
High-Level Language
Extension of C
Early 1980s: Bjarne Stroustrup (Bell Laboratories)
Spruces up C
Provides capabilities for object-oriented programming
Objects: reusable software components
Model items in real world
Object-oriented programs
Easy to understand, correct and modify
Hybrid language
C-like style
Object-oriented style
Both
2003 Prentice Hall, Inc. All rights reserved.
Software reuse
10
Structured Programming
Structured programming (1960s)
Disciplined approach to writing programs
Clear, easy to test and debug, and easy to modify
Pascal
1971: Niklaus Wirth
Ada
1970s - early 1980s: US Department of Defense (DoD)
Multitasking
11
Software reuse
Libraries
MFC (Microsoft Foundation Classes)
Rogue Wave
2003 Prentice Hall, Inc. All rights reserved.
12
Program-development environment
Language
C++ Standard Library
13
Disk
Programiscreatedin
theeditorandstored
ondisk.
Disk
Preprocessorprogram
processesthecode.
Compiler
Disk
Compilercreates
objectcodeandstores
itondisk.
Linker
Disk
Editor
Preprocessor
4. Link
Loader
5. Load
Disk
6. Execute
Primary
Memory
Loaderputsprogram
inmemory.
..
..
..
Primary
Memory
CPU
..
..
..
Linkerlinkstheobject
codewiththelibraries,
createsa.outand
storesitondisk
CPUtakeseach
instructionand
executesit,possibly
storingnewdata
valuesastheprogram
executes.
14
cout
Standard output stream
Normally computer screen
cerr
15
Structured programming
Object-oriented programming
A Simple Program:
Printing a Line of Text
Comments
Document programs
Improve program readability
Ignored by compiler
Single-line comment
Begin with //
Preprocessor directives
16
Outline
1
2
3
4
5
6
7
8
9
10
11
12
returns an
directive to
integer
value.
Left brace
{ begins Preprocessor
function
include
input/output Statements
stream
begins
execution
body. program
end with a
Function
main appears
header
file
<iostream>.
exactly once in every C++ semicolon ;.
program..
// function main
int main()
{
std::cout << "Welcome to C++!\n";
return 0;
//
} // end function
Welcome to C++!
Single-line comments.
17
A Simple Program:
Printing a Line of Text
Standard output stream object
std::cout
Connected to screen
<<
Stream insertion operator
Value to right (right operand) inserted into output stream
Namespace
std:: specifies using name that belongs to namespace
std
std:: removed through use of using statements
Escape characters
\
Indicates special character output
2003 Prentice Hall, Inc. All rights reserved.
18
A Simple Program:
Printing a Line of Text
Escape Sequence
Description
\n
\t
\r
\a
\\
\"
19
Outline
1
2
3
4
5
6
7
8
9
10
11
12
13
Welcome to C++!
20
Outline
1
2
3
4
5
6
7
8
9
10
11
12
to
Welcome
to
C++!
21
Comma-separated list
int integer1, integer2, sum;
2003 Prentice Hall, Inc. All rights reserved.
22
Valid identifier
Series of characters (letters, digits, underscores)
Cannot begin with digit
Case sensitive
23
= (assignment operator)
Assigns value to variable
Binary operator (two operands)
Example:
24
Outline
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
// read an integer
statements:
Stream manipulator
alternative for lines 18 and 20: std::endl outputs a
sum = integer1 + integer2; // assign result to sum
newline, then flushes
std::cout << "Sum is " << integer1 + integer2 <<
std::endl;
std::cout << "Sum is " << sum
<< std::endl; // print sum output buffer.
return 0;
Concatenating, chaining or
cascading stream insertion
operations.
Outline
26
27
Memory Concepts
Variable names
28
Memory Concepts
std::cin >> integer1;
integer1
45
integer1
45
integer2
72
integer1
45
integer2
72
sum
117
29
Arithmetic
Arithmetic calculations
*
Multiplication
/
Division
Integer division truncates remainder
7 / 5 evaluates to 1
30
Arithmetic
Rules of operator precedence
Operators in parentheses evaluated first
Nested/embedded parentheses
Operators in innermost pair first
*, /, or %
+ or -
Operation(s)
Operators
applied fromOrder
leftoftoevaluation
right (precedence)
Parentheses
Relational operators
Same level of precedence
31
C++ equality
or relational
operator
Example
of C++
condition
Meaning of
C++ condition
Relationaloperators
>
>
x>y
xisgreaterthany
<
<
x<y
xislessthany
>=
x>=y
xisgreaterthanorequaltoy
<=
x<=y
xislessthanorequaltoy
Equalityoperators
==
x==y
xisequaltoy
!=
x!=y
xisnotequaltoy
32
33
Outline
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
variables.
// function main begins programDeclare
execution
int main()
{
Can write
cin
int num1; // first number
to becout
read and
from
user
withouttostd::
prefix.
int num2; // second number
be read
from user
cout << "Enter two integers, and I will tell you\n"
if structure compares values
<< "the relationships they satisfy: ";
of num1 and num2
to test for
If condition
is true
cin >> num1 >> num2;
// read two integers
if ( num1 == num2 )
cout << num1 << " is
(i.e.,
equality.
values are equal), execute this
if structure compares
values
statement.
If condition
is true (i.e.,
of num1
andnum2
num2
test for
equal
to " <<
<< to
endl;
values are not equal), execute
inequality.
this statement.
if ( num1 != num2 )
cout << num1 << " is not equal to " << num2 << endl;
34
Outline
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
35
Outline
36
Inheritance
New classes of objects absorb characteristics from existing classes
Objects
37
Member functions
Function components of class
Association
Reuse classes
38
39
40