60 likes | 183 Views
Static Members. Static data Members. When a data member is declared as static Only one copy of the data is maintained for all objects of the class Static data members are not part of objects of a given class type The declaration of a static data member is not considered as definition
E N D
Static data Members When a data member is declared as static Only one copy of the data is maintained for all objects of the class Static data members are not part of objects of a given class type The declaration of a static data member is not considered as definition Static data member can be defined using the scope resolution operator (::) out side the class
Static data Members single copy is shared by all objects of the same class Created before creating the objects of the class Initial value of static data is 0 class sample { static int a;//declaration }; int sample::a;//definition a member is 0
Static data Members Questions 1)demonstrate automatic initialization of static variable 3) sum of first n numbers 4)display how many instances of a class object has been created
Static Member Functions Static member functions can access only static member data Non static member function can access all member data including static data member Static member function cannot declared as virtual Static function can be defined by using the keyword static before the function return type Syn Static data type function name(argument list) { function body; }
Static Member Functions Static member functions cannot be invoked with class object Static member functions can be invoked with class name and :: class sample { static int a; static void display(); } int sample ::a=10; void sample:: display(){----} main() { sample::display(); }