0% found this document useful (0 votes)
63 views19 pages

Static Data Member & Member Function, Array of Objects

The document discusses static data members and static member functions in C++, explaining that static members are shared across all objects of a class rather than being unique to each object. It provides examples of declaring static data members and calling static member functions, as well as examples of using arrays of objects and passing objects as arguments to functions by value and by reference. The document also covers friend functions and how a member function of one class can be declared a friend of another class.

Uploaded by

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

Static Data Member & Member Function, Array of Objects

The document discusses static data members and static member functions in C++, explaining that static members are shared across all objects of a class rather than being unique to each object. It provides examples of declaring static data members and calling static member functions, as well as examples of using arrays of objects and passing objects as arguments to functions by value and by reference. The document also covers friend functions and how a member function of one class can be declared a friend of another class.

Uploaded by

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

Static Data Member & Member Function,

Array of Objects
KALINGA INSTITUTE OF INDUSTRIAL
TECHNOLOGY

School Of Computer Engineering

Mr. Abhaya Kumar Sahoo


Assistant 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();
cout<<”object no=”<< code<<endl; t2.showcode();
t3.showcode();
}
}
Array of Objects in C++
7
Example: Array of Objects in C++
8
#include <iostream>
#include <string> int main()
using namespace std;
{
Student st[5];
class Student
for( int i=0; i<5; i++ )
{ string name;
{
int marks;
cout << "Student " << i + 1 << endl;
public: void getName()
st[i].getName();
{ st[i].getMarks();
cout << "Enter name" << endl; }
cin>>name; for( int i=0; i<5; i++ )
} {
void getMarks(){ cout << "Student " << i + 1 << endl;
cout << "Enter marks" << endl; st[i].displayInfo();
cin >> marks; }
} return 0;
void displayInfo() }
{
cout << "Name : " << name << endl;
cout << "Marks : " << marks << endl;
}
};
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);
Object as Function arguments
10
Object as Function arguments-Pass by Value
11
Object as Function arguments-Pass by Address
12
Example: Object as Function arguments
13
C++ Friend function
14
 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:
15
 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:
16
#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:
17
 Syntax:
class X
{
....
int fun1();
....
};
class Y
{
....
friend int X::fun1();
....
};
Member function one class can be friend function
of another class:
18
#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)
{ };
data=value; void add (XYZ obj1, ABC obj2 )
{
}
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); }
19

You might also like