Docsity

User-Defined Manipulators and Static Variables in C++, Slides of Computer Programming

Partial preview of the text

Download User-Defined Manipulators and Static Variables in C++ and more Slides Computer Programming in PDF only on Docsity!

Introduction to Programming

Lecture 38

Today’s Lecture  User Define Manipulator  Static key word

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

Parameter Less Manipulators

ostream & manipulatorName ( ostream & os ) ;

Definition ostream & manipulatorName ( ostream & os ) { return os << userDefinedManipulator ; }

Static

Are variables which exist for a certain amount of time which is longer than an ordinary automatic variable. Static

int i ; void myfunction ( void ) { int i ; for ( i = 0 ; i < 10 ; i ++ ) cout << i << endl ; } Example

Automatic Variable

static int i ;

static int i = 0 ; //Initialization ......... i = 0 ; //Ordinary Assignment Statement

Example class Truck { char a ; public : Truck ( char c ) { a = c ; cout << "Inside constructor for object " << a << endl ; } ~ Truck ( ) { cout << "Inside destructor for object " << a << endl ; } } ;

Example Truck a ( 'A' ) ; main ( ) { Truck b ( 'B' ) ; f ( ) ; g ( ) ; cout << “Function g has been called " << endl ; }