KEMBAR78
Arrays With in A Class: Const Int Size 10 Class Array (Int A (Size) Public: Void Setval (Void) Void Display (Void) ) | PDF | Class (Computer Programming) | Scope (Computer Science)
0% found this document useful (0 votes)
69 views19 pages

Arrays With in A Class: Const Int Size 10 Class Array (Int A (Size) Public: Void Setval (Void) Void Display (Void) )

1. The document discusses static data members and static member functions in C++ classes. Static data members are shared by all objects of the class and stored separately from the objects. Static member functions can access only static members and are called using the class name instead of object instances. 2. An example demonstrates defining and initializing a static data member outside the class, and accessing it through objects and with the class name. Static members follow usual access rules but are shared across all class objects. 3. Static members are used to maintain values common to the entire class. Their type and scope must be defined outside the class. They exist even if no class objects are created.

Uploaded by

bobsumit
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views19 pages

Arrays With in A Class: Const Int Size 10 Class Array (Int A (Size) Public: Void Setval (Void) Void Display (Void) )

1. The document discusses static data members and static member functions in C++ classes. Static data members are shared by all objects of the class and stored separately from the objects. Static member functions can access only static members and are called using the class name instead of object instances. 2. An example demonstrates defining and initializing a static data member outside the class, and accessing it through objects and with the class name. Static members follow usual access rules but are shared across all class objects. 3. Static members are used to maintain values common to the entire class. Their type and scope must be defined outside the class. They exist even if no class objects are created.

Uploaded by

bobsumit
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Arrays with in a class

const int size=10;

Class array
{
int a[size];
public:
void setval(void);
void display(void);
};
Memory allocation for objects
• Member functions are placed in memory
only once when they are defined as a part
of class specification, irrespective of
number of objects.
• All objects belonging to that class use
same member function.
• Data members hold separate memory
space for each object
Common for all objects

Member function1

Member function2 //memory created when function defined

object1 object2 object3


Data member1 data member1 data member1

Data member2 data member2 data member2

//memory created when objects defined


CLASS MEMBERS
Characteristics of Static data
member
• It is initialized to zero when the first object
of the class is created. No other
initialization is permitted.
• Only one copy of static member is created
for the entire class and is shared by all the
objects of that class, no matter how many
objects are created
• It is visible only within the class, but its
lifetime is in the entire program
Static Data members
• Static data members follow the usual class
access rules, except that they can be
initialized in file scope.
• Static variables are used to maintain
values common to the entire class. Every
object of same class is accessing common
static variable.
• They are declared with keyword static.
• The declaration of a static data member in the
member list of a class is not a definition.
• The definition of a static data member is
equivalent to an external variable definition.
• You must define the static member outside of
the class declaration in namespace scope.
For example:
class X
{
public: static int i;
};
int X::i = 0; // definition outside class declaration
• Once you define a static data member, it
exists even though no objects of the static
data member's class exist.
• In the above example, no objects of
class X exist even though the static data
member X::i has been defined.
Example
#include<iostream.h>
#include<conio.h>
class test
{
private:
static int count; //static member
Int x,y; //non static member
public:
int getcount()
{
count++; return count;
}};
int test::count; //static member definition, it is initialized before main()
void main()
{
test obj1;
cout<<“current count is” <<obj1.getcount();
test obj2;
cout<<“current count is”<<obj2.getcount();
}
output
Current count is 1
Current count is 2
Example
int main()
#include<iostream.h>
{
class item
item a,b,c;// when object is
{ created,value of static
static int count; variable count is initialized
int number; to zero
public: void getdata(int a) a.getcount();
{ b.getcount();
number=a; c.getcount();
count ++; a.getdata(100);
} b.getdata(200);
void getcount() c.getdata(300);
{ cout<<“after reading”;
cout<<“count:”<<count; a.getcount();
} b.getcount();
}; c.getcount();
int item::count;// definition of }
static data member
Output:
Count:0
Count:0
Count:0
After reading
Count:3
Count:3
Count:3
Object1 object2 object3
number number number

200 300 400

3
3
count //static variable
NOTE:

• The type and scope of each static member


variable must be defined outside class
definition.
• This is necessary because static data
members are stored separately rather than
a part of an object.
• Since they are associated with the class
itself rather than with any class object,
they are known as class variables.
• If a static data member is of const integral
or const enumeration type, you may specify a constant
initializer in the static data member's declaration.
• This constant initializer must be an integral constant
expression.
• Note that the constant initializer is not a definition.
• You still need to define the static member. The following
example demonstrates this:
#include <iostream.h>
class X
{
static const int a = 76;
};
const int X::a;
int main()
{ cout << X::a << endl; }
The tokens = 76 at the end of the declaration of static data
member a is a constant initializer.
• You can only have one definition of a
static member in a program. Unnamed
classes and classes contained within
unnamed classes cannot have static data
members.
• Local classes cannot have static data
members.
Static member functions
• A member function that is declared static
has following properties:
• A static function can have access to only
static members declared in the same
class.
• A static member function can be called
using the class name instead of its
objects.
Class-name::function-name;
#include<iostream.h> int main()
class test {
{ test t1,t2,t3;
int code; //non static t1.setcode();
static int count; //static t2.setcode();
public:
void setcode(void) test::showcount();/ /accessing
{ code=++count; } static function

void showcode(void) t3.setcode();


{ cout<<“object
number:”<<code; } test::showcount();

static void showcount(void) t1.showcode();


{ cout<<“count:”<<count; } }; t2.showcode();
int test::count; t3.showcode();
}
output
• Count: 2 Static void showcount()
• Count: 3 {
cout<<code;
• Object number: 1
}
• Object number: 2 //will not work
• Object number: 3 ?

You might also like