Static Members in Class, Friend Function and Class

Static Data Member

• When a class is instantiated, memory is allocated to the created object by constructor.
• The memory allocated to object is equal to the total size of data members defined in class.
• Storage space for the data members which are declared as static is allocated by once during the class declaration and definition of static members.
• All the objects of this class have access to this data member, that is all instances of the class (objects) access the same data member.
• When one of them modifies the static data member, the effect is visible to all the objects.

Memory allocation for members


• Thus each object has a separate copy of the automatic data members and they share static data members among them.

Static Data Member continue…

• The type and scope of each static member variable must be defined outside the class definition.
• This is necessary because the static data members are stored separately rather than as a part of an object.
• Since they are associated with the class itself rather than with any class object, they are also known as class variables.

Static Data Member(syntax)

• The static data members of class are declared inside the class with keyword static as,

Static Data Member: Example

Access Rule for static data member

• If data member are public static, then they can be accessed by using :: operator or member access operator through objects as

Static Member Function

• A static function is one which is declared as static in a class.
• A static member function can only access static data member, other static member functions and any other functions from outside the class.
• By declaring a function member as static, we make it independent of any particular object of the class.
• A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::.
• Till now, we knew that the private member data of a class can be accessed only by member functions of that class and not by the object outside the class.
• But there is one exception- A friend function
• A friend function is a function that is not a member of a class but declared as friend inside the class either in private or public section.
• The definition of function is always made outside the class as a non member function.
• The friend function can access the private members of a class since it is declared as friend inside class.
• It can work as a bridge between two or more classes by declaring it as friend into multiple classes.
• The declaration and definition of friend function can be written as :

Friend Functions: An Example

Friend Class

• Like a friend function , a class can be made friend to another class by declaring it inside the class to which it has to be made friend.
• The declaration of class as friend can be done as :

• Alternatively, the declaration syntax can be,

0 Comments