CS201-38 - Introduction to Programming Lecture 38 Today’s...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Programming Lecture 38 Today’s Lecture User Define Manipulator Static key word User Define Manipulators Example int i = 10 ; cout << setwidth ( 7 ) << i <<endl ; Parameter Less Manipulators cout << manipulator << otherdata ; ostream & manipulatorName ( ostream & os ) ; Definition ostream & manipulatorName ( ostream & os ) { return os << userDefinedManipulator ; } User Defined Manipulators // Tab A Short Example 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 } Static Static Are variables which exist for a certain amount of time which is longer than an ordinary automatic variable. Global int i ; void myfunction ( void ) { int i ; for ( i = 0 ; i < 10 ; i ++ ) cout << i << endl ; } Example Automatic Variable State static int i ; static int i = 0 ; //Initialization ......... i = 0 ; //Ordinary Assignment Statement void f ( void ) { static int i = 0 ; i ++ ; cout << “Inside function f value of i : ” << i << endl ; } main ( ) { int j ; for ( j = 0 ; j < 10 ; j ++ ) ; f ( ) ; } Example 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 ; } void f ( ) { Truck c ( 'C' ) ; } void g ( ) { static Truck g ( 'G' ) ; } Example Output Inside constructor for object A Inside constructor for object B Inside constructor for object C Inside destructor for object C Inside constructor for object G function g has been called Inside destructor for object B Inside destructor for object G Inside destructor for object A Example Static Data Member Inside A Class File Scope class Truck { public : int wheels ; int seats ; } ; main ( ) { Truck A ; A.seats ; } Example Scope Resolution Operator :: Truck :: nameOfStaticDatamember = values ; class SavingsAccount { Example } ; private : char name [ 30 ] ; float accountNumber ; float currentBalance ; static float profitRate ; // ... public : SavingsAccount ( ) ; //... SavingsAccount :: profitRate = 3.0 ; SavingsAccount A ; A.profitRate ; // bad usage A.profitRate = 4.0 ; // bad usage SavingsAccount :: profitRate ; class Student { public : static int howMany ; Student ( ) { howMany ++ ; } ~ Student( ) { howMany ­­ ; } void displayHowMany ( ) { cout << "Number of students are " << howMany ; } } ; int Student :: howMany = 0 ; Example Dynamic What we covered today Parameter Less Manipulation Static Data ...
View Full Document

Ask a homework question - tutors are online