Professional Documents
Culture Documents
Module5 Lect5,6 15 04 23
Module5 Lect5,6 15 04 23
Static Members
Static is a keyword in C++ used to give special
characteristics to an element.
Static elements are allocated storage only once in a
program lifetime in static storage area.
Static is a keyword which can be used with data members
as well as the member functions.
Static variables are created when the program starts and
destroyed when the program stops.
Visibility is similar to instance variables.
However, most static variables are declared public since
they must be available for users of the class.
• 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 as, using the scope
resolution operator :: to identify which class it belongs
to.
With and Without static
void counter() void counter()
{ {
static int count=0; int count=0;
cout << count++; cout << count++;
} }
int main()
Value of global x is 0
{
int x = 10; // Local x Value of local x is 10
cout << "Value of global x is " << ::x;
cout << "\nValue of local x is " << x;
return 0;
}
To define a function outside a class use scope resolution operator ::
#include <iostream>
using namespace std;
class A {
public:
// Only declaration
Output
void fun(); fun( ) called
};
// Definition of function outside the
class using ::
void A::fun()
{ cout << "fun() called"; }
int main()
{
A a;
a.fun();
return 0;
}
double Volume() {
#include <iostream>
return length * breadth * height;
}
using namespace std;
private:
class Box {
double length; // Length of a box
public:
double breadth; // Breadth of a box
static int objectCount;
double height; // Height of a box
};
// Constructor definition
// Initialize static member of class Box
Box(double l = 2.0, double b = 2.0, double
int Box::objectCount = 0;
h = 2.0) {
cout <<"Constructor called." << endl;
int main(void) {
length = l;
Box Box1(3.3, 1.2, 1.5); // Declare box1
breadth = b;
Box Box2(8.5, 6.0, 2.0); // Declare box2
height = h;
// Increase every time object is created
// Print total number of objects.
objectCount++;
cout << "Total objects: " <<
}
Box::objectCount << endl;
return 0;
}
Static Function Members
Friend
function
class TANK
Class Wall
n.a=n.a+1;
cout<<"the value of a using friend function is
:"<<n.a;
}
#include <iostream>
using namespace std; void display(car x)
class car {
{ cout<<"\nThe color of the car is : "<<x.color;
private: cout<<"\nThe speed of the car is :
"<<x.speed;
int speed;
}
char color[20];
int main( )
public:
{
void input( )
car mine;
{
mine.input( );
cout<<"\nEnter the color : ";
display(mine); //passing the object ‘mine’ to
cin>>color;
the friend function
cout<<"\nEnter the speed : ";
return 0;
cin>>speed;
}
}
friend void display(car);
};
#include<iostream>
using namespace std; int main()
class base { {
int val1, val2; base obj;
public:
obj.get();
void get() { cout << "\n Mean value is : "
cout << "Enter two values:"; << mean(obj);
cin >> val1>>val2; }
}
friend float mean(base ob);
};
} int main()
{
// friend function declaration ClassA objectA;
ClassB objectB;
friend int add(ClassA, ClassB); cout << "Sum: " << add(objectA, objectB);
}; return 0;
}
friend Class in C++
• We can also use a friend class in C++ using the friend keyword
class ClassB;
class ClassA
{
// ClassB is a friend class of ClassA
friend class ClassB;
... .. ...
}
class ClassB {
... .. ...
}
– When a class is declared a friend class, all the member functions of
the friend class become friend functions.
return 0;
}
void change(int data)
{
data = 5;
}
Call by reference in C++
#include<iostream>
int main()
using namespace std;
void swap(int *x, int *y)
{
{ int x=500, y=100;
int swap; swap(&x, &y);
swap=*x; cout<<"Value of x is: "<<x<<endl;
*x=*y; cout<<"Value of y is: "<<y<<endl;
*y=swap; return 0;
} }
Output
Value of x is: 100
Value of y is: 500