cs201- introduction to programming- lecture 25

Post on 18-Dec-2014

22 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 25 Instructor's Name: Dr. Naveed A. Malik Course Email: cs201@vu.edu.pk

TRANSCRIPT

Introduction to Introduction to ProgrammingProgramming

Lecture 25Lecture 25

Introduction to C++ Introduction to C++

languagelanguage

In Coming LecturesIn Coming Lectures

– C++ C++ FeaturesFeatures

– ClassesClasses– ObjectObject

Today’s Today’s LectureLecture

Default Function ArgumentsDefault Function Arguments Inline FunctionsInline FunctionsClassesClasses

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

Liberally CommentLiberally Commentthe codethe code

Object Object Oriented Oriented LanguageLanguage

Early 1980's Early 1980's

Bjarne Bjarne Stroustrup Stroustrup

at Bell Labsat Bell Labs

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

Default Default Function Function

ArgumentsArguments

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

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

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

----}}

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;

}}

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

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

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

codecode

while ( condition ) while ( condition )

{{

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

}}

{{

int i ;int i ;

------

}}

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.

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 ;

Inline Inline functionsfunctions

inlineinline

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 ;

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 ) ;

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' ;}}

Function Function OverloadingOverloading

Operator Operator OverloadingOverloading

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

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

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}}

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 ;

}}

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

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

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

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

Name Name ManglingMangling

top related