Basics-Object Oriented Programming-Lecture Slides, Slides of Object Oriented Programming

Objective of this cours is to develop effective computer programming skills in solving complex problems and to learn adequate and operational software organization in developing real life engineering solutions using powerful object oriented paradigm of the language. It includes: Object, Oriented, Programming, Namespace, Identifies, Library, Parenthesis, Function, Compiler, Text, String, Constant

Typology: Slides

2011/2012

Uploaded on 07/31/2012

netu
netu 🇮🇳

4.5

(4)

50 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
2/10/2011
1
Object Oriented
Programming using C++
Lecture – 2
Basics
Usman Younis
Program Basics
Namespace identifies the part of a program in which certain names
are recognized, std namespace includes all entities of standard
C++ library
Parenthesis follow the function name
Function body is surrounded in braces
Compiler ignores the whitespaces
Text with in “quotation marks” is a string constant
\n is an escape sequence (causes following text to be printed in a
new line)
docsity.com
pf3
pf4
pf5
pf8

Partial preview of the text

Download Basics-Object Oriented Programming-Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity!

Object Oriented

Programming using C++

Lecture – 2

Basics

Usman Younis

Program Basics

 Namespace identifies the part of a program in which certain names are recognized, std namespace includes all entities of standard C++ library  Parenthesis follow the function name  Function body is surrounded in braces  Compiler ignores the whitespaces  Text with in “quotation marks” is a string constant  \n is an escape sequence (causes following text to be printed in a new line)

Program Basics (contd..)

 cout serves the similar purpose as printf () in C

 cout is actually an object in C++, and corresponds to

standard output stream (normally flows to display)

 The operator << is called insertion operator

 << is a left shift bitwise operator!

 However, it is overloaded when used with cout

Program Basics (contd..)

 cin serves the similar purpose as scanf () in C  cin is also an object in C++  >> is extraction operator

endl manipulator , same as using \n

Arithmetic operators

 Remainder operator (%), e.g.

 6 % 8 will result in 6  8 % 8 will result in 0  10 % 8 will result in 2

 Assignment operator (=), e.g.

 A = A + B, can be written as  A += B  Similar applies for: *, −, /, and %

 Increment operator (++)

 A = A + 1, can be written as  A += 1, or simply ++A  Remember the prefix (++A) and postfix (A++) precedence. In first the increment is performed before using the variable; andin second, the increment is performed after.

 Decrement operator (−−)

Relational operators

Operator Meaning

Greater than < Less than == Equal to != Not equal to = Greater than or equal to <= Less than or equal to

A = 20

B = 34

A > B (false)

A < B (true)

A == B (false)

A+14 == B (true)

A != B (true)

B >= A (true)

A <= 21 (true)

A != 20 (false)

(0) (false) by definition

(12) (true) as it’s not 0

Arithmetic operators have a higher

precedence than relational operator

Usman Younis

Loops

 The for loop

Initialization Expression

Body of Loop

Increment Expression

Test Expression Exit

True

False

Loops (contd..)

 The while loop

Body of Loop

Test Expression Exit

True

False

Decisions (contd..)

 If else statement

if (condition is true) execute the statement; //single statement else execute this statement; Or if (condition is true) { execute multiple statements; statement ……. 1; statement ……. 2; and so on …. } else { execute multiple statements; }

Body of if

Test Expression

True

False

Body of else

Exit

Decisions (contd..)

 Nested If else statements

if (condition is true) execute the statement; else if (this condition is true) execute this statement; else if (this conditions is true) execute this statement; else execute this statement;

Body of if

Test Expression

True

False

Exit Body of if

Test Expression

True Body of else

False

Remember to match the else with required if. It is a best practice to properly indent your code.

Decisions (contd..)

 The switch statement

switch ( integer or character variable ){ case 1: execute statement; execute statement;break; case 2: execute statement; execute statement;break; case 3: execute statement; execute statement;break; .. default: execute statement; } //no break, as it is the only choice left

Switch 1st caseequals

False

True (^) Case Body

Exit

Switch 2nd caseequals False

True (^) Case Body

Default Body

You can use the break statement in a loop as well!

Decisions (contd..)

 The conditional operator result = (check a condition)? first expression : second expression; if the condition is true, then first expression is assigned to the result; otherwise, the second expression is assigned e.g., k = ( a < 20)? a : b; (^) Test expression False

Exit

True Value of expression 1 Value of expression 2