![Page 1: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/1.jpg)
Introduction to Introduction to ProgrammingProgramming
Lecture 38Lecture 38
![Page 2: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/2.jpg)
Today’s LectureToday’s Lecture
User Define ManipulatorUser Define Manipulator Static key wordStatic key word
![Page 3: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/3.jpg)
User Define User Define ManipulatorManipulator
ss
![Page 4: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/4.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/5.jpg)
Parameter Parameter Less Less
ManipulatorsManipulators
![Page 6: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/6.jpg)
cout << manipulator << cout << manipulator << otherdata ;otherdata ;
![Page 7: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/7.jpg)
ostream & manipulatorName ( ostream & ostream & manipulatorName ( ostream & os ) ;os ) ;
![Page 8: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/8.jpg)
DefinitionDefinition
ostream & manipulatorName ( ostream & ostream & manipulatorName ( ostream & os )os )
{{
return os << userDefinedManipulator ;return os << userDefinedManipulator ;
}}
![Page 9: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/9.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/10.jpg)
StatiStaticc
![Page 11: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/11.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/12.jpg)
GlobaGloball
![Page 13: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/13.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/14.jpg)
AutomatiAutomatic Variablec Variable
![Page 15: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/15.jpg)
StateState
![Page 16: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/16.jpg)
static int static int i ;i ;
![Page 17: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/17.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/18.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/19.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/20.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/21.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/22.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/23.jpg)
Static Data Static Data Member Inside Member Inside
A ClassA Class
![Page 24: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/24.jpg)
File File ScopeScope
![Page 25: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/25.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/26.jpg)
Scope Resolution Scope Resolution
OperatorOperator
::::
![Page 27: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/27.jpg)
Truck :: nameOfStaticDatamember = Truck :: nameOfStaticDatamember = values ;values ;
![Page 28: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/28.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/29.jpg)
SavingsAccount :: profitRate = SavingsAccount :: profitRate = 3.0 ; 3.0 ;
![Page 30: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/30.jpg)
SavingsAccount A ;SavingsAccount A ;
A.profitRate ; // bad usageA.profitRate ; // bad usage
![Page 31: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/31.jpg)
A.profitRate = 4.0 ; // bad usageA.profitRate = 4.0 ; // bad usage
![Page 32: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/32.jpg)
SavingsAccount :: SavingsAccount :: profitRate ;profitRate ;
![Page 33: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/33.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/34.jpg)
DynamiDynamicc
![Page 35: CS201- Introduction to Programming- Lecture 38](https://reader035.vdocument.in/reader035/viewer/2022062616/5492e746b47959834a8b4672/html5/thumbnails/35.jpg)
What we covered What we covered todaytoday
Parameter Less ManipulationParameter Less Manipulation Static DataStatic Data