Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming

Similar presentations


Presentation on theme: "Introduction to Programming"— Presentation transcript:

1 Introduction to Programming
Lecture 25

2 Introduction to C++ language

3 In Coming Lectures C++ Features Classes Object

4 Today’s Lecture Default Function Arguments Inline Functions Classes

5 Rules for Structured Programming
Do Modular programming Write small function Every function should perform a specific well defined task Function should obey single entry single exit rule

6 Liberally Comment the code

7 Object Oriented Language

8 Early 1980's Bjarne Stroustrup at Bell Labs

9 C with Classes C++ Java

10 Default Function Arguments

11 power ( x , n ) ;

12 void f ( int i , double x ) ;

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

14 Example 1 void f ( int i = 1 , double x = 10.5 ) {
cout<<“i = ”<<i; cout<<“ x = “<< x<<endl; }

15 Example 1 main ( ) { f ( ) ; f ( 2 ) ; f ( 2 , 12 ) ; } Output
i = 1 x = 10.5 i = 2 x = 10.5 i = 2 x = 12

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

17 The declaration of variables inside the code

18 while ( condition ) { // body of the while loop }

19 { int i ; --- }

20 Scope of Variable When you declare a variable inside the block
then the variable exists in that block.

21 Example 2 for ( int i = 0 ; i < 3 ; i++ ) { int temp = 22 ;
cout << "\n i = " << i << "temp = " << temp ; } cout << "i = " << i ;

22 Inline functions

23 inline

24 Example 2 #define SQUARE ( X ) X * X main ( ) {
int i = 5 , j = 10 , k ; : k = } i + j * i + j ; SQUARE ( i + j ) ;

25 Example 3 #define SQUARE ( X ) ( X ) * ( X ) main ( ) {
int i = 5 , j = 10 , k ; k = } SQUARE ( i + j ) ; ( i + j ) * ( i + j ) ;

26 Example 3 #define MAX ( A , B ) ( ( A ) > ( B ) ? ( A ) : ( B ) )
inline f ( int a, int b ) { if ( a > b ) return a ; return b ; } void main ( ) int i , x , y ; x = 23 ; y = 45 ; i = MAX ( x++ , y++ ) ; // Side - effect : larger value incremented twice cout << "x = " << x << " y = " << y << '\n' ; i = f ( x++ , y++ ) ; // Works as expected

27 Function Overloading

28 Operator Overloading

29 Overloading Using the same name to perform multiple tasks or
different task depending on the situation

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

31 Example 4 int squareroot ( int i ) { // body of the function }
double squareroot ( double x )

32 Example 4 main ( ) { double i , x ; int j = 5 , k ;
i = squareroot ( 10.5 ) ; cout << i << endl ; k = squareroot ( j ) ; cout << k << endl ; }

33 Example F ( int a , int b ) ; F ( int a , int b , int c ) ; main ( ) {
F ( 1 , 2 ) ; // F ( int a , int b ) ; is called F ( 1 , 2 , 3 ) ; // F ( int a , int b , int c ) ; is called }

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

35 Name Mangling


Download ppt "Introduction to Programming"

Similar presentations


Ads by Google