You are on page 1of 38

Static Data Members

Friend Functions
Friend Classes
Static Functions

1
https://www.youtube.com/watch?v=CO1RekmsBac

SEE THE VIDEO

2
3
Static Data Members
 A type of data member that is shared among all objects of
classes is known as static data member.
 The static data member is defined in the class with static
keyword.
 When a data member is defined as static, only one variable
is created in the memory even if there are many objects of
that class.

4
Static Data Members
 The type of a static data member is same as normal static
variable. It is only visible in the class in which it is defined
but its lifetime starts when the program starts its execution.

 Its lifetime ends when the entire program is terminated. It is


normally used to share same data among all objects of a
particular class.

5
Static Data Members
 The main difference between normal data member and
static data member is that each object has its own variable
of normal data member.
 On the other hand, the static data member is shared among
all objects of the class. Only one memory location is created
for static data member that is shared among all objects.

6
Static Data Members
Object 1 Object 2 Object 1 Object 2

a 1 a 2 a 1 a 2
b 10 b 20
b 10 b 20
n 100 n 200

Object with three normal data members n 200


Object with two normal data members a, b
And one static data member n

7
Example Program 1
Write a program that counts the number of objects created of a particular class.

#include <iostream>
using namespace std;

class Yahoo {
private: int main ()
static int n; {
public: Yahoo x,y;
Yahoo() x.show();
{ Yahoo z;
n++; x.show();
} return 0;
void show () }
{
cout<<"You created "<<n<<" objects so far. "<<endl;
}
};
int Yahoo::n=0;

8
9
10
How above program Works
 The above program declares a static data member n to count the
number of objects that have been created. The following
statement defines the variable.

int yahoo::n=0;
 The above statement defines the variable and initializes it to 0
value.
 The variable is defined outside the class because it will be not
part of any object. It is created only once in the memory and is
shared among all objects of the class.

11
How above program Works
 The variable definition outside the class must be preceded with
the class name and scope resolution operator :: . The compiler
dose not display any error if the static data member is not
defined. The linker will generate error when the program is
executed.
 The above program creates three objects x, y and z. each time
object is created, the constructor is executed that increases the
value of n by 1.

12
Example Program 2
Write a program that creates three objects of class Student. Each object of
class must be assigned a unique roll number. (Hint: Use static data member
for unique roll number)

13
#include <iostream> void show ()
{ cout<<endl;
using namespace std;
cout<<"Roll no= "<<rno<<endl;
 class Student { cout<<"Name= "<<name<<endl;
private: cout<<"Marks= "<<marks;
static int r; } };
int rno, marks;
char name[50]; int Student::r=0;
public: int main () {
Student() Student s1, s2, s3;
{ r++;
s1.in();
rno=r; }
s2.in();
void in ()
{ cout<<"Enter name: "; s3.in();
cin>>name; s1.show();
cout<<"Enter Marks: "; s2.show();
cin>>marks; } s3.show();
return 0;
}

14
15
16
How above program Works
 The program uses the static data member r to assign unique roll
number to each object of the class Student . The static data
member is initialized to 0.
 The constructors increment the value by 1 when an object is
created and then assigns the updated value of r to the data
member rno. It ensures that each object gets a unique roll
number.

17
Friend Functions
 A type of function that is allowed to access the private and
protected members of a particular class from outside the
class is called friend function.
 Normally, the private and protected members of any class
cannot be accessed from outside the class. In some
situations, a program may require to access these members.

18
Friend Functions
 A function that is declared in a class with friend keyword
becomes the friend function of that class. It enables that
function to access the private and protected members of the
class.

19
Friend Functions
Example:
Suppose a friend function accepts two objects of different classes as
parameters. It has to process the private data members of these
classes and then displays the result.
It is not possible to perform the task because a function is not a
member of a class cannot access the private or protected members of
the class.
The problem can be solved by declaring that function as friend
function of both class. It will enable the function to access the private
and protected members of both classes.

20
Example Program 3
Write a program that demonstrate the use of Friend Function

#include <iostream>
using namespace std; void show (A x, B y)
class B; {
class A { int r;
private: r=x.a+y.b;
int a; cout<<"The value of class A object= "<<x.a<<endl;
public: cout<<"The value of class B object= "<<y.b<<endl;
A() cout<<"The sum of both values= "<<r<<endl;
{ a=10; }
friend void show (A, B); }
};
class B { int main () {
private: A obj1;
int b; B obj2;
public: show(obj1, obj2);
B() return 0;
{ b=20; } }
friend void show (A, B);
}; 21
22
23
Friend Classes
 A type of class all of whose member functiona are allowed to
access the private and proteced members of a particular class is
called friend class.
 Normally, the private and proteced members of any class cannot
be accessed from outside the the class.
 In some situations, a program may require to access these
members. The use of friend classes allows a class to accesses
these members of another class.
 A class that is declared in another class with friend keyword
becomes the friend of that class.

24
Example Program 4
Write a program that demonstrates the use of Friend Classes.
#include <iostream>
using namespace std;
class B;
class A {
private: int main ()
int a,b; {
public: A x;
A() B y;
{ a=10; y.showA(x);
b=20; } y.showB(x);
friend class B; return 0;
}; }
class B {
public:
void showA (A obj)
{ cout<<"The value of a: "<<obj.a<<endl; }
void showB (A obj)
{ cout<<"The value of b: "<<obj.b<<endl; }
};
25
26
27
Static Functions
 A type of member function that can be accessed without
any object of the class is called static function.
 Normally, a member function of any class cannot be
accessed or executed without creating an object of that
class.
 In some situations, a member function has to be executed
without referencing any object.

28
Static Functions
 The static data members of a class are not created for each
object. The class creates only one data member for all
objects.
 The static data member is defined when the program is
executed.
 They may require to access a static data members before
creating an object. The static member functions can be used
to access a static data members.

29
Example Program 5
Write a program that demonstrate the use of Static Functions.

#include <iostream>
using namespace std;

class Test { int main () {


private:
static int n; Test::show();
public: return 0;
static void show () }
{
cout<<"n= "<<n<<endl;
}
};
int Test::n=10;

30
31
32
How above program Works
 The above program declares a class Test with a static data
member n. The following statement defines the data member
with an initial value of 10:

int Test::n=10;
 The static data member exists in the memory even before
creating any object. The programs also declares a static member
function show() that displays the value of n.
 The program calls the static member function without creating
any object of the class as follows:

33
Example Program 6
Write a program that counts the number of objects created for a particular
class. The program must be able to display the results even if no object is
created so far.

#include <iostream> int main () {


using namespace std; Yahoo::show();
class Yahoo { Yahoo x,y;
private: x.show();
static int n; Yahoo z;
public: x.show();
Yahoo() return 0;
{ n++; } }
static void show ()
{ cout<<"You have created "<<n<<" objects so far. "<<endl; }
};
int Yahoo::n=0;

34
35
36
How above program Works
 The above program declares a static data member n to count the
number of objects that have been created. The following
statement defines the variable.

int yahoo::n=0;
 The above statement defines the variable and initializes it to 0
value.
 The above program creates three objects x, y and z. each time
object is created, the constructor is executed that increases the
value of n by 1.

37
How above program Works
 The program also declares a static member function show() to
displays the value of data member. It can be accessed directly by
using class name followed by scope resolution operator :: and
function name as follows:

yahoo::show();

38

You might also like