Download presentation
Presentation is loading. Please wait.
1
Introduction to Programming
Lecture 38
2
Today’s Lecture User Define Manipulator Static key word
3
User Define Manipulators
4
Example int i = 10 ; cout << setwidth ( 7 ) << i <<endl ;
5
Parameter Less Manipulators
6
cout << manipulator << otherdata ;
7
ostream & manipulatorName ( ostream & os ) ;
8
Definition ostream & manipulatorName ( ostream & os ) {
return os << userDefinedManipulator ; }
9
User Defined Manipulators
A 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
10
Static
11
Static Are variables which exist for a certain amount of time which is
longer than an ordinary automatic variable.
12
Global
13
Example int i ; void myfunction ( void ) {
for ( i = 0 ; i < 10 ; i ++ ) cout << i << endl ; }
14
Automatic Variable
15
State
16
static int i ;
17
static int i = 0 ; //Initialization
i = 0 ; //Ordinary Assignment Statement
18
Example 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 ( ) ;
19
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 ; } ;
20
Example Truck a ( 'A' ) ; main ( ) { Truck b ( 'B' ) ; f ( ) ; g ( ) ;
cout << “Function g has been called " << endl ; }
21
Example void f ( ) { Truck c ( 'C' ) ; } void g ( )
static Truck g ( 'G' ) ;
22
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
23
Static Data Member Inside A Class
24
File Scope
25
Example class Truck { public : int wheels ; int seats ; } ; main ( )
Truck A ; A.seats ; }
26
Scope Resolution Operator
::
27
Truck :: nameOfStaticDatamember = values ;
28
Example class SavingsAccount { } ; private : char name [ 30 ] ;
float accountNumber ; float currentBalance ; static float profitRate ; // public : SavingsAccount ( ) ; //... } ;
29
SavingsAccount :: profitRate = 3.0 ;
30
SavingsAccount A ; A.profitRate ; // bad usage
31
A.profitRate = 4.0 ; // bad usage
32
SavingsAccount :: profitRate ;
33
Example class Student { public : static int howMany ;
Student ( ) { howMany ++ ; } ~ Student( ) { howMany -- ; } void displayHowMany ( ) cout << "Number of students are " << howMany ; } } ; int Student :: howMany = 0 ;
34
Dynamic
35
What we covered today Parameter Less Manipulation Static Data
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.