Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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)

55 documents

1 / 8

Toggle sidebar

Related documents


Partial preview of the text

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

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)

2

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

3

Data Types

 Eliminating the sign of char and integer types, sets the Low value at 0 and High value as twice its default

Keyword Low High Bytes in Memory char −128 127 1 short −32,768 32,767 2 int −2,147,483,648 2,147,483,647 4 long −2,147,483,648 2,147,483,647 4 float 3.4× 10 −38^ 3.4× 1038 4 double 1.7× 10 −308^ 1.7× 10308 8 long double 3.4× 10 −4932^ 3.4× 104932 10

Type conversion

 lower-type variables are automatically converted into higher-type variables  You can use static_cast(variable) to convert into your desired data type

ans = a (^) ***** b

double int float

a

float


ans temp double

4

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

5

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

6

Loops (contd..)

 The do loop

Body of Loop

Test True Expression^ Exit

False

Usman Younis

Decisions

 If statement

if (condition is true)

execute the statement; //single statement

Or

if (condition is true)

{

execute multiple statements;

statement ……. 1;

statement ……. 2;

and so on ….

}

7

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.

8

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