S.DHAKSHANAMOORTHY
S.SARAVANAN
M.VISHALVENKAT
N.DINESHKUMAR
K.R.SHAARUKESH
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:
i. Shared by all objects of the class.
ii. Initialized to zero when the first object is created.
iii. Initialized outside the class.
EXAMPLE
//Static Member
#include <iostream>
using namespace std;
class Student {
public:
static int total;
Student() { total += 1; }
};
int Student::total = 0;
int main()
{
Student s1;
cout << "Number of students:" << s1.total << endl;
Student s2;
cout << "Number of students:" << s2.total << endl;
Student s3;
cout << "Number of students:" << s3.total << endl;
return 0;
}
OUTPUT
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
//Static Member
//Static Function
#include <iostream.h>
#include <conio.h>
class test
{
int code;
static int count;
public:
void set(void)
{
code=++count;
}
void show(void)
{
cout<<"Object number:"<<code<<endl;
}
static void showcount(void)
{
cout<<"Count:"<<count<<endl;
}
};
//Initializing Static Member
//Accessing Static Function
int test::count;
void main()
{
test t1,t2,t3;
clrscr();
t1.set();
test::showcount();
t2.set();
test::showcount();
t3.set();
test::showcount();
t1.show();
t2.show();
t3.show();
getch();
}
OUTPUT
EXAMPLE
#include <iostream>
using namespace std;
class Box
{
private:
static int length;
static int breadth;
static int height;
public:
static void print()
{
cout << "The value of the length is: " << length << endl;
cout << "The value of the breadth is: " << breadth << endl;
cout << "The value of the height is: " << height << endl;
}
};
int Box :: length = 10;
int Box :: breadth = 20;
int Box :: height = 30;
int main()
{
Box b;
cout << "Static member function is called through Object name: n" <<
endl;
b.print();
cout << "nStatic member function is called through Class name: n" <<
endl;
Box::print();
return 0;
}
OUTPUT
static data member and member function .pptx

static data member and member function .pptx

  • 1.
  • 2.
    CONTENTS Static Data Member StaticMember Function Example - Program
  • 3.
    STATIC DATA MEMBERS •We can define class members static using static keyword. • A static member variable has certain special characteristics: i. Shared by all objects of the class. ii. Initialized to zero when the first object is created. iii. Initialized outside the class.
  • 4.
    EXAMPLE //Static Member #include <iostream> usingnamespace std; class Student { public: static int total; Student() { total += 1; } }; int Student::total = 0; int main() { Student s1; cout << "Number of students:" << s1.total << endl; Student s2; cout << "Number of students:" << s2.total << endl; Student s3; cout << "Number of students:" << s3.total << endl; return 0; }
  • 5.
  • 6.
    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.
  • 7.
    EXAMPLE //Static Member //Static Function #include<iostream.h> #include <conio.h> class test { int code; static int count; public: void set(void) { code=++count; } void show(void) { cout<<"Object number:"<<code<<endl; } static void showcount(void) { cout<<"Count:"<<count<<endl; } };
  • 8.
    //Initializing Static Member //AccessingStatic Function int test::count; void main() { test t1,t2,t3; clrscr(); t1.set(); test::showcount(); t2.set(); test::showcount(); t3.set(); test::showcount(); t1.show(); t2.show(); t3.show(); getch(); }
  • 9.
  • 10.
    EXAMPLE #include <iostream> using namespacestd; class Box { private: static int length; static int breadth; static int height; public: static void print() { cout << "The value of the length is: " << length << endl; cout << "The value of the breadth is: " << breadth << endl; cout << "The value of the height is: " << height << endl; } }; int Box :: length = 10; int Box :: breadth = 20; int Box :: height = 30;
  • 11.
    int main() { Box b; cout<< "Static member function is called through Object name: n" << endl; b.print(); cout << "nStatic member function is called through Class name: n" << endl; Box::print(); return 0; }
  • 12.