CS201- Introduction to Programming- Lecture 25

35
Introduction to Introduction to Programming Programming Lecture 25 Lecture 25

description

Virtual University Course CS201- Introduction to Programming Lecture No 25 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

Transcript of CS201- Introduction to Programming- Lecture 25

Page 1: CS201- Introduction to Programming- Lecture 25

Introduction to Introduction to ProgrammingProgramming

Lecture 25Lecture 25

Page 2: CS201- Introduction to Programming- Lecture 25

Introduction to C++ Introduction to C++

languagelanguage

Page 3: CS201- Introduction to Programming- Lecture 25

In Coming LecturesIn Coming Lectures

– C++ C++ FeaturesFeatures

– ClassesClasses– ObjectObject

Page 4: CS201- Introduction to Programming- Lecture 25

Today’s Today’s LectureLecture

Default Function ArgumentsDefault Function Arguments Inline FunctionsInline FunctionsClassesClasses

Page 5: CS201- Introduction to Programming- Lecture 25

Rules for Structured Rules for Structured ProgrammingProgramming

Do Modular programmingDo Modular programming– Write small function Write small function – Every function should perform Every function should perform

a specific well defined taska specific well defined task Function should obey single Function should obey single

entry entry

single exit rulesingle exit rule

Page 6: CS201- Introduction to Programming- Lecture 25

Liberally CommentLiberally Commentthe codethe code

Page 7: CS201- Introduction to Programming- Lecture 25

Object Object Oriented Oriented LanguageLanguage

Page 8: CS201- Introduction to Programming- Lecture 25

Early 1980's Early 1980's

Bjarne Bjarne Stroustrup Stroustrup

at Bell Labsat Bell Labs

Page 9: CS201- Introduction to Programming- Lecture 25

C with ClassesC with Classes C++C++ JavaJava

Page 10: CS201- Introduction to Programming- Lecture 25

Default Default Function Function

ArgumentsArguments

Page 11: CS201- Introduction to Programming- Lecture 25

power ( x , n ) power ( x , n ) ;;

Page 12: CS201- Introduction to Programming- Lecture 25

void f ( int i , double x ) ;void f ( int i , double x ) ;

Page 13: CS201- Introduction to Programming- Lecture 25

void f ( int i =1 , double x = 10.5 void f ( int i =1 , double x = 10.5 )){{

----}}

Page 14: CS201- Introduction to Programming- Lecture 25

Example 1Example 1void f ( int i = 1 , double x = 10.5 )void f ( int i = 1 , double x = 10.5 )

{{

cout<<“i = ”<<i;cout<<“i = ”<<i;

cout<<“ x = “<< x<<endl;cout<<“ x = “<< x<<endl;

}}

Page 15: CS201- Introduction to Programming- Lecture 25

Example 1Example 1main ( )main ( )

{{

f ( ) ; f ( ) ;

f ( 2 ) ; f ( 2 ) ;

f ( 2 , f ( 2 , 12 ) ; 12 ) ;

}}

Outputi = 1 x = 10.5

i = 2 x = 10.5

i = 2 x = 12

Page 16: CS201- Introduction to Programming- Lecture 25

void f ( double x=10.5 , int i ) ;void f ( double x=10.5 , int i ) ;

Page 17: CS201- Introduction to Programming- Lecture 25

The declaration of The declaration of variables inside the variables inside the

codecode

Page 18: CS201- Introduction to Programming- Lecture 25

while ( condition ) while ( condition )

{{

// body of the while // body of the while looploop

}}

Page 19: CS201- Introduction to Programming- Lecture 25

{{

int i ;int i ;

------

}}

Page 20: CS201- Introduction to Programming- Lecture 25

Scope of Scope of VariableVariable

When you declare a When you declare a

variable inside the block variable inside the block

then the variable exists then the variable exists in in

that block.that block.

Page 21: CS201- Introduction to Programming- Lecture 25

Example 2Example 2for ( int i = 0 ; i < 3 ; i++ )for ( int i = 0 ; i < 3 ; i++ )

{{

int temp = 22 ;int temp = 22 ;

cout << "\n i = " << i << "temp = " << cout << "\n i = " << i << "temp = " <<

temp temp ; ;

}}

cout << "i = " << i ;cout << "i = " << i ;

Page 22: CS201- Introduction to Programming- Lecture 25

Inline Inline functionsfunctions

Page 23: CS201- Introduction to Programming- Lecture 25

inlineinline

Page 24: CS201- Introduction to Programming- Lecture 25

Example 2 Example 2 ##define SQUARE ( X ) X * Xdefine SQUARE ( X ) X * Xmain ( )main ( ){{

int i = 5 , j = 10 , k ;int i = 5 , j = 10 , k ;::

k =k =}}

SQUARE ( i + j ) ; SQUARE ( i + j ) ; i + j * i + j ;i + j * i + j ;

Page 25: CS201- Introduction to Programming- Lecture 25

Example 3Example 3#define SQUARE ( X ) ( X ) * #define SQUARE ( X ) ( X ) *

( X )( X )

main ( )main ( )

{{

int i = 5 , j = 10 , k ;int i = 5 , j = 10 , k ;

k =k =

}}

SQUARE ( i + j ) ;SQUARE ( i + j ) ;( i + j ) * ( i + j ) ;( i + j ) * ( i + j ) ;

Page 26: CS201- Introduction to Programming- Lecture 25

Example 3Example 3 #define MAX ( A , B ) ( ( A ) > ( B ) ? ( A ) : ( B ) )#define MAX ( A , B ) ( ( A ) > ( B ) ? ( A ) : ( B ) )inline f ( int a, int b )inline f ( int a, int b ){{ if ( a > b ) if ( a > b )

return a ;return a ; return b ;return b ;}}void main ( )void main ( ){{ int i , x , y ;int i , x , y ; x = 23 ; y = 45 ;x = 23 ; y = 45 ; i = MAX ( i = MAX ( x++ ,x++ , y++ ) ; y++ ) ; // Side - effect : larger value incremented // Side - effect : larger value incremented

twicetwice cout << "x = " << x << " y = " << y << '\n' ;cout << "x = " << x << " y = " << y << '\n' ; x = 23 ; y = 45 ;x = 23 ; y = 45 ; i = f ( i = f ( x++ x++ , y++ ) ; // Works as expected, y++ ) ; // Works as expected cout << "x = " << x << " y = " << y << '\n' ;cout << "x = " << x << " y = " << y << '\n' ;}}

Page 27: CS201- Introduction to Programming- Lecture 25

Function Function OverloadingOverloading

Page 28: CS201- Introduction to Programming- Lecture 25

Operator Operator OverloadingOverloading

Page 29: CS201- Introduction to Programming- Lecture 25

OverloadingOverloadingUsing the same name to Using the same name to perform multiple tasks orperform multiple tasks ordifferent task depending on different task depending on the situationthe situation

Page 30: CS201- Introduction to Programming- Lecture 25

double Intsquareroot ( int i ) ;double Intsquareroot ( int i ) ;double Doublesquareroot ( double x ) ;double Doublesquareroot ( double x ) ;

Page 31: CS201- Introduction to Programming- Lecture 25

Example 4Example 4int squareroot ( int i )int squareroot ( int i ){{

// body of the function// body of the function}}

double squareroot ( double x )double squareroot ( double x ){{

// body of the function// body of the function}}

Page 32: CS201- Introduction to Programming- Lecture 25

Example 4Example 4main ( )main ( ){{

double i , x ;double i , x ;int j = 5 , k ;int j = 5 , k ;i = squareroot ( 10.5 ) ;i = squareroot ( 10.5 ) ;cout << i << endl ;cout << i << endl ;k = squareroot ( j ) ;k = squareroot ( j ) ;cout << k << endl ;cout << k << endl ;

}}

Page 33: CS201- Introduction to Programming- Lecture 25

ExampleExampleF ( int a , int b ) ;F ( int a , int b ) ;F ( int a , int b , int c ) ;F ( int a , int b , int c ) ;

main ( )main ( ){{

----------------------F ( 1 , 2 ) ; // F ( int a , int b ) ; is calledF ( 1 , 2 ) ; // F ( int a , int b ) ; is calledF ( 1 , 2 , 3 ) ; // F ( int a , int b , int c ) ; is calledF ( 1 , 2 , 3 ) ; // F ( int a , int b , int c ) ; is called

----------------------}}

Page 34: CS201- Introduction to Programming- Lecture 25

int f ( int a ) ;int f ( int a ) ;

double f ( int a ) ;double f ( int a ) ;

Page 35: CS201- Introduction to Programming- Lecture 25

Name Name ManglingMangling