35
Introduction to Introduction to Programming Programming Lecture 38 Lecture 38

CS201- Introduction to Programming- Lecture 38

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: CS201- Introduction to Programming- Lecture 38

Introduction to Introduction to ProgrammingProgramming

Lecture 38Lecture 38

Page 2: CS201- Introduction to Programming- Lecture 38

Today’s LectureToday’s Lecture

User Define ManipulatorUser Define Manipulator Static key wordStatic key word

Page 3: CS201- Introduction to Programming- Lecture 38

User Define User Define ManipulatorManipulator

ss

Page 4: CS201- Introduction to Programming- Lecture 38

int i = 10 ;int i = 10 ;cout << setwidth ( 7 ) << i <<endl ;cout << setwidth ( 7 ) << i <<endl ;

ExampleExample

Page 5: CS201- Introduction to Programming- Lecture 38

Parameter Parameter Less Less

ManipulatorsManipulators

Page 6: CS201- Introduction to Programming- Lecture 38

cout << manipulator << cout << manipulator << otherdata ;otherdata ;

Page 7: CS201- Introduction to Programming- Lecture 38

ostream & manipulatorName ( ostream & ostream & manipulatorName ( ostream & os ) ;os ) ;

Page 8: CS201- Introduction to Programming- Lecture 38

DefinitionDefinition

ostream & manipulatorName ( ostream & ostream & manipulatorName ( ostream & os )os )

{{

return os << userDefinedManipulator ;return os << userDefinedManipulator ;

}}

Page 9: CS201- Introduction to Programming- Lecture 38

User Defined User Defined ManipulatorsManipulatorsA Short Example// Tab

ostream & tab ( ostream & output ) { return output << '\t' ;}

// bell

ostream & bell ( ostream & output ){ return output << '\a' ;}

// Takes the cursr to next line

ostream & endLine ( ostream & output ){ return output << '\n' << flush ;}

void main ( ){

cout << "Virtual " << tab << "University" << bell << endLine ; // Use of Mainpulator

}

Page 10: CS201- Introduction to Programming- Lecture 38

StatiStaticc

Page 11: CS201- Introduction to Programming- Lecture 38

Are variables which exist for a Are variables which exist for a

certain amount of time which is certain amount of time which is

longer than an ordinary automatic longer than an ordinary automatic

variable.variable.

StaticStatic

Page 12: CS201- Introduction to Programming- Lecture 38

GlobaGloball

Page 13: CS201- Introduction to Programming- Lecture 38

int i ;int i ;

void myfunction ( void )void myfunction ( void )

{{

int i ;int i ;

for ( i = 0 ; i < 10 ; i ++ )for ( i = 0 ; i < 10 ; i ++ )

cout << i << endl ;cout << i << endl ;

}}

ExampleExample

Page 14: CS201- Introduction to Programming- Lecture 38

AutomatiAutomatic Variablec Variable

Page 15: CS201- Introduction to Programming- Lecture 38

StateState

Page 16: CS201- Introduction to Programming- Lecture 38

static int static int i ;i ;

Page 17: CS201- Introduction to Programming- Lecture 38

static int i = 0 ; //Initializationstatic int i = 0 ; //Initialization

..................

i = 0 ; //Ordinary Assignment Statementi = 0 ; //Ordinary Assignment Statement

Page 18: CS201- Introduction to Programming- Lecture 38

void f ( void )void f ( void ){{

static int i = 0 ;static int i = 0 ;i ++ ;i ++ ;cout << “Inside function f value of i : ” << i << cout << “Inside function f value of i : ” << i << endl ;endl ;

}}main ( )main ( ){{

int j ;int j ;for ( j = 0 ; j < 10 ; j ++ ) ;for ( j = 0 ; j < 10 ; j ++ ) ;

f ( ) ;f ( ) ;}}

ExampleExample

Page 19: CS201- Introduction to Programming- Lecture 38

Example Example class Truckclass Truck{{ char a ;char a ;

public :public : Truck ( char c )Truck ( char c ) {{

a = c ;a = c ; cout << "Inside constructor for object " << a << endl ;cout << "Inside constructor for object " << a << endl ;

}}

~ Truck ( )~ Truck ( ) {{

cout << "Inside destructor for object " << a << cout << "Inside destructor for object " << a << endl ;endl ; }}

} ;} ;

Page 20: CS201- Introduction to Programming- Lecture 38

ExampleExampleTruck a ( 'A' ) ;Truck a ( 'A' ) ;

main ( )main ( )

{{

Truck b ( 'B' ) ;Truck b ( 'B' ) ;

f ( ) ;f ( ) ;

g ( ) ;g ( ) ;

cout << “Function g has been called " << endl ;cout << “Function g has been called " << endl ;

}}

Page 21: CS201- Introduction to Programming- Lecture 38

ExampleExamplevoid f ( )void f ( )

{{

Truck c ( 'C' ) ;Truck c ( 'C' ) ;

}}

void g ( )void g ( )

{{

static Truck g ( 'G' ) ;static Truck g ( 'G' ) ;

}}

Page 22: CS201- Introduction to Programming- Lecture 38

ExampleExampleOutputOutputInside constructor for object AInside constructor for object AInside constructor for object BInside constructor for object BInside constructor for object CInside constructor for object CInside destructor for object CInside destructor for object CInside constructor for object GInside constructor for object Gfunction g has been calledfunction g has been calledInside destructor for object BInside destructor for object BInside destructor for object GInside destructor for object GInside destructor for object AInside destructor for object A

Page 23: CS201- Introduction to Programming- Lecture 38

Static Data Static Data Member Inside Member Inside

A ClassA Class

Page 24: CS201- Introduction to Programming- Lecture 38

File File ScopeScope

Page 25: CS201- Introduction to Programming- Lecture 38

class Truckclass Truck{{

public :public : int wheels ;int wheels ; int seats ;int seats ;

} ;} ;main ( )main ( ){{

Truck A ;Truck A ;A.seats ;A.seats ;

}}

ExampleExample

Page 26: CS201- Introduction to Programming- Lecture 38

Scope Resolution Scope Resolution

OperatorOperator

::::

Page 27: CS201- Introduction to Programming- Lecture 38

Truck :: nameOfStaticDatamember = Truck :: nameOfStaticDatamember = values ;values ;

Page 28: CS201- Introduction to Programming- Lecture 38

Example Example class SavingsAccountclass SavingsAccount{{

private :private : char name [ 30 ] ;char name [ 30 ] ; float accountNumber ;float accountNumber ; float currentBalance ;float currentBalance ; static float profitRate ;static float profitRate ; // ...// ...public :public : SavingsAccount ( ) ;SavingsAccount ( ) ;//...//...

} ;} ;

Page 29: CS201- Introduction to Programming- Lecture 38

SavingsAccount :: profitRate = SavingsAccount :: profitRate = 3.0 ; 3.0 ;

Page 30: CS201- Introduction to Programming- Lecture 38

SavingsAccount A ;SavingsAccount A ;

A.profitRate ; // bad usageA.profitRate ; // bad usage

Page 31: CS201- Introduction to Programming- Lecture 38

A.profitRate = 4.0 ; // bad usageA.profitRate = 4.0 ; // bad usage

Page 32: CS201- Introduction to Programming- Lecture 38

SavingsAccount :: SavingsAccount :: profitRate ;profitRate ;

Page 33: CS201- Introduction to Programming- Lecture 38

ExampleExampleclass Studentclass Student{{

public :public :static int howMany ;static int howMany ;

Student ( ) { howMany ++ ; }Student ( ) { howMany ++ ; } ~ Student( ) { howMany -- ; }~ Student( ) { howMany -- ; }

void displayHowMany ( )void displayHowMany ( ) {{

cout << "Number of students are " << howMany ;cout << "Number of students are " << howMany ; }}} ;} ;int Student :: howMany = 0 ;int Student :: howMany = 0 ;

Page 34: CS201- Introduction to Programming- Lecture 38

DynamiDynamicc

Page 35: CS201- Introduction to Programming- Lecture 38

What we covered What we covered todaytoday

Parameter Less ManipulationParameter Less Manipulation Static DataStatic Data