CS201- Introduction to Programming- Lecture 38

Preview:

DESCRIPTION

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

Citation preview

Introduction to Introduction to ProgrammingProgramming

Lecture 38Lecture 38

Today’s LectureToday’s Lecture

User Define ManipulatorUser Define Manipulator Static key wordStatic key word

User Define User Define ManipulatorManipulator

ss

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

ExampleExample

Parameter Parameter Less Less

ManipulatorsManipulators

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

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

DefinitionDefinition

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

{{

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

}}

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

}

StatiStaticc

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

GlobaGloball

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

AutomatiAutomatic Variablec Variable

StateState

static int static int i ;i ;

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

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

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

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

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

} ;} ;

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 ;

}}

ExampleExamplevoid f ( )void f ( )

{{

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

}}

void g ( )void g ( )

{{

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

}}

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

Static Data Static Data Member Inside Member Inside

A ClassA Class

File File ScopeScope

class Truckclass Truck{{

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

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

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

}}

ExampleExample

Scope Resolution Scope Resolution

OperatorOperator

::::

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

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 ( ) ;//...//...

} ;} ;

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

SavingsAccount A ;SavingsAccount A ;

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

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

SavingsAccount :: SavingsAccount :: profitRate ;profitRate ;

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 ;

DynamiDynamicc

What we covered What we covered todaytoday

Parameter Less ManipulationParameter Less Manipulation Static DataStatic Data

Recommended