0% found this document useful (0 votes)
8 views

Chap 0204

Uploaded by

udyadav430
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Chap 0204

Uploaded by

udyadav430
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Lecture – 9

• Static members of the class


• Instance
• Message Passing
Static Members of Class: Data member and Member function
1. Static data members

A data member of a class can be qualified as static. A static member variable has certain special
characteristics. These are:

• It is initialized to zero when the first object of its class is created. No other initialization is
permitted.
• Only one copy of that 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 the entire program.

Static variables are normally used to maintain values common to the entire class.

For example, a static data member can be used as a counter that records the occurrences of all
the objects.
Static data member program int main()
{
item a ,b,c;
#include<iostream> Output :
a.getcount();
using namespace std; Count: 0
b.getcount();
c.getcount(); Count: 0
class item Count: 0
{ After reading data
a.getdata(100);
static int count; Count: 3
b.getdata(200);
int number; Count: 3
c.getdata(300);
Count: 3
public:
cout<< "After reading data"<<"\n";
voidgetdata(int a)
a.getcount();
{
b.getcount();
number = a;
c.getcount();
count ++;
return 0;
}
}
voidgetcount(void)
{
cout<< "Count: ";
cout<< count <<"\n";
}
};
Static Member Function
Like static member variable, we can also have static member function.
A member function that is declared static has the following properties:

• A static function can have access to only static members declared in the class.
• A static member function can be called the class name as follows:

Class-name:: function-name

• Static member functions are used to maintain a single copy of a class member function
across various objects of the class.

• Static member functions can be called either by itself, independent of any object, by using
class name and :: (scope resolution operator) or in connection with an object.
Characteristic static member functions are:

• A static member function can only have access to other static data members and functions
declared in the same class.

• A static member function can be called using the class name with a scope resolution
operator instead of object name.

• Global functions and data may be accessed by static member function.

• A static member function does not have this Pointer.

• There can not be a static and a non-static version of the same function.

• They cannot be declared as const or volatile.

• A static member function may not be virtual.


Instance
An instance is an object of a class type created via the new operator.

For example :
String *str = new String();
str is a pointer to an instance of class String. if you won`t create memory for an object so we call
that object as instance.

example:
Student std;

here,Student is a class and std is a instance(means a just a copy that class),with this we won`t do
anything until unless we create a memory for that.
Message Passing
 Objects communicate with one another by sending and receiving information to each other.
 A message for an object is a request for execution of a procedure and therefore will invoke a
function in the receiving object that generates the desired results.
 Message passing involves specifying the name of the object, the name of the function and the
information to be sent.

Message Passing is nothing but sending and receiving of information by the objects same
as people exchange information. So this helps in building systems that simulate real life.

Following are the basic steps in message passing.


Creating classes that define objects and its behavior.
• Creating objects from class definitions
• Establishing communication among objects
In OOPs, Message Passing involves specifying the name of objects, the name of the function, and
the information to be sent
Do Subscribe

You might also like