[go: up one dir, main page]

0% found this document useful (0 votes)
66 views15 pages

Kalinga Institute of Industrial Technology School of Computer Engineering

Static data members and static member functions allow data and functions to be shared across all objects of a class. Static data members are initialized to zero when the first object is created. Static member functions can only access other static members of the class and are called using the class name instead of an object. Friend functions in C++ can access private and protected members of a class. They are declared with the friend keyword inside the class definition.
Copyright
© © All Rights Reserved
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)
66 views15 pages

Kalinga Institute of Industrial Technology School of Computer Engineering

Static data members and static member functions allow data and functions to be shared across all objects of a class. Static data members are initialized to zero when the first object is created. Static member functions can only access other static members of the class and are called using the class name instead of an object. Friend functions in C++ can access private and protected members of a class. They are declared with the friend keyword inside the class definition.
Copyright
© © All Rights Reserved
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/ 15

Static Data Member & Member Function

KALINGA INSTITUTE OF INDUSTRIAL


TECHNOLOGY

School Of Computer Engineering

Dr. Pradeep Kumar Mallick


Associate Professor [II]
School of Computer  Engineering,
Kalinga Institute of Industrial Technology (KIIT),
Deemed to be University,Odisha

3 Credit Lecture Note 09


Chapter Contents
2

 Static Data member


 Static Member Function.
Specifying Class
3

 We can define class members static using static keyword.


 When we declare a member of a class as static it means no matter how many
objects of the class are created, there is only one copy of the static member.
 A static member is shared by all objects of the class.
 All static data is initialized to zero when the first object is created, if no other
initialization is present.
 We can't put it in the class definition but it can be initialized outside the class
using the scope resolution operator :: to identify which class it belongs to.
 It is visible only within the class, but its life time is the entire program.
 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.
Example
4
#include <iostream>
using namespace std; int item::count;
class item int main()
{ {
static int count; item a ,b,c;// count initialised to
int number; zero
public: a.getcount();
void getdata(int a) b.getcount();
{
c.getcount();
number=a;
a.getdata(100);
count ++;
b.getdata(200);
}
c.getdata(300);
void getcount() cout<< “After reading ”<<endl
{
cout<<”Count=”<< count<<endl; a.getcount();
} b.getcount();
}; c.getcount();
}
Static Member Function
5
 A static member function can have access to only other static members
(Function and variable) declared in the same class.
 A static member function can be called using the class name (instead of its
objects) as follows:
 Syntax :
class-name:: function-name;
Example
6
#include <iostream> static void showcount()
using namespace std; {
cout<<”Count=”<< count;
class test }
{ };//end of class
int code; int test::count;
int main
static int count; {
public: test t1,t2;
void setcode( ) t1.setcode();
t2.setcode();
{ test::showcount();
code=++count;
test t3;
}
t3.setcode();
void showcode() test::showcount();
{ t1.showcode();
t2.showcode();
cout<<”object no=”<< code<<endl; t3.showcode();
}
}
Array of Objects in C++
7
#include <iostream>
int main()
#include <string>
{
using namespace std;
Student st[5];
class Student
for( int i=0; i<5; i++ )
{
{
string name; cout << "Student " << i + 1 << endl;
int marks; cout << "Enter name" << endl;
public: st[i].getName();
void getName() cout << "Enter marks" << endl;
{ st[i].getMarks();
getline( cin, name ); }
} for( int i=0; i<5; i++ )
void getMarks() {
{ cout << "Student " << i + 1 << endl;
cin >> marks; st[i].displayInfo();
} }
void displayInfo() return 0;
{ }
cout << "Name : " << name << endl;
cout << "Marks : " << marks << endl;
}
};
Object as Function arguments
8
 A copy of the entire object is passed to the function.
 Only the address of the object is transfered to the function.

 To pass an object as an argument we write the object name as the argument


while calling the function the same way we do it for other variables.

Syntax:
function_name(object_name);
Object as Function arguments
9
 A copy of the entire object is passed to the function.
 Only the address of the object is transfered to the function.

 To pass an object as an argument we write the object name as the argument


while calling the function the same way we do it for other variables.

Syntax:
function_name(object_name);
C++ Friend function
10
 If a function is defined as a friend function in C++, then the protected and
private data of a class can be accessed using the function.
 By using the keyword friend compiler knows the given function is a friend
function.
 For accessing the data, the declaration of a friend function should be done
inside the body of a class starting with the keyword friend.
 Declaration of friend function in C++:
class class_name
{
friend data_type function_name(argument/s); // syntax of friend
function.
};
Characteristics of a Friend function:
11
 The function is not in the scope of the class to which it has been declared as a
friend.
 It cannot be called using the object as it is not in the scope of that class.
 It can be invoked like a normal function without using the object.
 It cannot access the member names directly and has to use an object name and
dot membership operator with the member name.
 It can be declared either in the private or the public part.
 The function defination does not use either the keyword friend or the scope
operator :: .
 A function can be declared as a friend in any number of classes.
 A friend function, although not a member function , has full rights to the
private members of the class.
Characteristics of a Friend function:
12
#include <iostream>
float mean( sample s)
#include <string>
{
using namespace std;
return float(s.a+s.b)/2.0;
class Sample
}
{ int main()
int a,b; {
sample x;
public: x.setvalue();
void setvalue() cout<< “Mean value=”<<mean(x);
{ return 0
a=25,b=40; }
}
friend float mean(sample s)
};
Member function one class can be friend function
of another class:
13
 Syntax:
class X
{
....
int fun1();
....
};
class Y
{
....
friend int X::fun1();
....
};
Member function one class can be friend function
of another class:
14
#include <iostream>
using namespace std; class ABC{
int data;
class ABC;
public:
class XYZ
void setvalue(int value)
{ {
int data; data=value;
public: }
void setvalue(int value) friend void add(XYZ,ABC)
{ };
void add (XYZ obj1, ABC obj2 )
data=value;
{
}
cout<<”sum=”<<obj1.data+obj2.data;
friend void add(XYZ,ABC)
}
}; int main(){
XYZ X; ABC A;
X.setvalue(5);
A.setvalue(10); add(X,A); }
15

You might also like