Static Data Members and Member Functions

6,293 views 9 slides Sep 21, 2018
Slide 1
Slide 1 of 9
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9

About This Presentation

Concept of Static Keyword in C++


Slide Content

STATIC DATA MEMBERS AND STATIC FUNCTIONS MADE BY : MOHIT AGARWAL 161080107026 COMPUTER SEM - 4

CONTENTS Static Data Member Static Member Function Example - Program

STATIC DATA MEMBERS We can define class members static using static keyword. A static member variable has certain special characteristics: S hared by all objects of the class. I nitialized to zero when the first object is created. I nitialized outside the class .

A B A B OBJECT WITH TWO NORMAL DATA MEMBERS OBJECT WITH ONE NORMAL AND ONE STATIC DATA MEMBER

STATIC MEMBER FUNCTIONS A static function can access only other static members. It is called using class name and scope resolution operator. class name :: function name; It can not use this pointer.

EXAMPLE #include < iostream.h > #include < conio.h > class test { int code; static int count; //Static Member public: void set(void) { code=++count; } void show(void) { cout <<"Object number:"<<code<< endl ; } static void showcount (void) //Static Function { cout <<"Count:"<<count<< endl ; } };

int test::count; //Initializing Static Member void main() { test t1,t2,t3; clrscr (); t1.set(); test:: showcount (); t2.set(); test:: showcount (); //Accessing Static Function t3.set(); test:: showcount (); t1.show(); t2.show(); t3.show(); getch (); }

OUTPUT

THANKYOU