Static data member:
when a member variable declaration preceded with a keyword "static", then we are telling the compiler that only
Returning object:
when a member variable declaration preceded with a keyword "static", then we are telling the compiler that only
i)
One copy of that member (variable) will exists
and that all objects of the class will share that variable
Unlike regular data member, individual copy of
the static variable is not made for each object. No matter how many objects of
a class are created, only one copy of a static data member exists. Thus all
objects of that class use the same variable. All static variables are
initialized to zero (0) before the first object is created.
When we declared a static data member within a class, we are not defining it (i.e. we are not allocating a storage for it). Instead we must provide the global definition for it outside the class. This is done by reducing the static variable using the scope resolution operator (:: ) to identify the class to which it belongs. This causes storage for the variable to be allocated.
Properties
1) Only one copy of that member is created for entire class and is shared by all the objects of that class, no matter how many object are created.
2) It is initialized to zero when the first object of its class is created.
3) It is visible only within the class but its lifetime “is the entire program ( remain till entire program)
example
When we declared a static data member within a class, we are not defining it (i.e. we are not allocating a storage for it). Instead we must provide the global definition for it outside the class. This is done by reducing the static variable using the scope resolution operator (:: ) to identify the class to which it belongs. This causes storage for the variable to be allocated.
Properties
1) Only one copy of that member is created for entire class and is shared by all the objects of that class, no matter how many object are created.
2) It is initialized to zero when the first object of its class is created.
3) It is visible only within the class but its lifetime “is the entire program ( remain till entire program)
example
#include<iostream.h>
#include<conio.h>
class shared{
static int a; //static data
member
int b; //data member
public : //visibility label
void set (int i, int j)
{
a=i;
b=j;
}
void display ( ); // member function
};
int shared :: a;
void shared ::display ( ) //
function definition
{
cout<<”\n this is static variable
a:”<<a;
cout<<”\n this is static variable
b:”<<b;
}
void main ( )
{
clrscr( )
shared x,y //object
x.set(1,1);//calling the function
x.dispaly( );
y.set(2,2);
y.display( );
x.display();
getch();
}
In the above program static
integer ‘a’ is declared both inside class shared and outside of it. This is
necessary because the declaration if ‘a’ outside ‘share’ does not allocate
storage. As we know, static member variable exists before, any object of it class
is created. If the static member variable is both ‘public’ and static, it can
be directly accessed in ‘main ( )’.
#include<iostream.h>
#include<conio.h>
class shared
{
public:
static int a;
};
int shared :: a;//static data
member
void main ()
{
shared ::a=100;
cout<< “The value of a
before creating the shared object”<<shared::a;
shared x;
cout<< “The value of a
other creating the object”<<x.a;
getch( );
}
Static Member function:
If any member functions of a class is preceeded with a
keyword static known as static member function of that class. A member function
that is declared as static has the following properties
1)
A static function can have to only other static member
( funct or variables) declared in the same class
2)
A static member funct can be called using the class
name ( instead of its objects ) as follows
class_name :: function_name
Example
class_name :: function_name
Example
#include<iostream.h>
#include<conio.h>
class sample
{
static int a;
public:
static void
showcount ( );
};
int sample ::
a;
void
sample::showcount //function definition
{
a++;
cout<< “the
value of a=”<<a “\n”;
}
void main ()
{
sample ::showcount
( );
sample ::showcount
( );
getch( );
}
Object as a function arguments
(pass by value)
An object may be used
as a function argument. A copy of the entire object is passed to the function
is called as passed by value.
Since a copy of a
object is passed to the function, any change made to a object inside a function
do not effects the object used to called the function.
#include<iostream.h>
#include<conio.h>
class sample
{
int a;
float b;
public:
void getdata (int x, int y); //
member function
void display ( );
void addition (sample p, sample
q);
};
void sample::getdata (int x,
float y)
{
a=x;
b=y;
}
void sample::display( )
{
cout<<”Integer value=”<<a<<”\n”;
cout<<”float value=”<<b<<”\n”;
}
void sample:: addition (sample p,
sample q)
{
a=p.a+q.a;
b=p.b+q.b;
}
void main ( )
{
sample s1,s2,s3;
s1.getdata(5,3.37);//calling the
function
s2.getdata(6,2.5);//calling the
function
s3.addition(s1,s2);
s3.display( );
s1.display( );
s2.display( );
getch( );
}
Returning object:
A function can not only receive
object as arguments but also can return them.
class ABC
{
int a ;
float b;
pblic:
void getdata(int x, int y); //member
function
ABC sum(ABC p, ABC q);
void display( );
};
Void ABC::getdata(int x, int y)
{
a=x;
b=y;
}
ABC ABC::sum(ABC p, ABC q) //
function definition
{
ABC temp; // temporary object
temp.a=p.a+q.a;
temp.b = p.b +q.b;
return (temp);
}
void ABC::display ( ) // function
definition
{
cout<< “\n Integer value=”<<a<<
“\n”;
cout<< “\n float value=”<<b<<
“\n”;
}
void main( )
{
ABC obj1, obj2, obj3;//objects
obj1.getdata(5,2.5); //calling
function
obj2.getdata(2,3.7);
obj3=obj3.sum(obj1,obj2);
obj3.display( );
getch( );
}
Inline function
One of the objective of using
functions in the program is to save some memory space, which becomes
appreciable when as a function is likely to be called many times. However,
every time a function is called, it takes a lot of extra time in execution in
series of instruction for tasks such as jumping to the function, saving registers
pushing arguments to the stack and returning to the calling function. When a
function is small, a substantial percentage of execution time may be spent in
such overheads.
One
solution to this problem is to use micro-definition known as macros. The major
drawback with macro is that they are not really function and therefore the
usual error checking does not occur during the compilation.
C++
has different solution to this problem. To estimate the cost of calls to small
functions, C++ purpose a new features called “inline function”. An inline
function is a function that is expanded inline when it is invoked i.e. the
compiler replaces he function call with the corresponding function code.
The inline function defined as
follows
inline function_header
{
function body
}
To
make the function inline, prefix the keyword “inline” to the function definition.
All the inline function must be defined before they are called.
#include<iostream.h>
#include<conio.h>
class myclass
{
int a,b; //data member
public: //visibility label
void init (int i, int j) //
member function
void show( );
};
inline void myclass:: int(int i,
int j) // function definition
{
a=i;
b=j;
}
inline void myclass::show ( )
{
cout<< “\n a=” <<
a<<\t” << “b=” <<b;
}
void main ( )
{
clrscr ( );
myclass x; //object
x.init (10,20) //calling the
function
x.show( ); // calling the
fucntion
getch( );
}
Reference Variables:
A reference variable provides an
alternative name for a previously defined variable. E.g. if we make a variable ‘sum’,
a reference to the variable ‘total ‘ can be used inter changeably to represent
that variable. A reference variable is created as follows
General syntax:
data type & reference
variable=variable name
e. g.
float total=100;
float &sum=total;
Here, ‘total’ is a float type
variable that have been already been declared. ‘sum’ is an alternative name
declared to represent to variable ‘total’. Both the variable referred to the
same data object in the memory. A reference variable must be initialized at the
time of declaration. A major application of reference variable is passing argument
to a function.
#include<iostream.h>
#include<conio.h>
class ref
{
public
void refe(int &a);
};
void ref::refe(int &a)
{
a=a+10; //a+10
cout<< “\n a=” <<
a<< “\n”
}
void main ( )
{
clrscr ( );
int x=10;
ref r; //object
r.refe(x);
cout<< “\n x=” <<
x<< “\n”;
r.refe(x); // calling eh function
cout<< “\n x=” << x;
getch( );
}
Write a program to swap two
values using call by reference.
#include<iostream.h>
#include<conio.h>
class swap
{
public :
void swapping (int &a, int
&b);
};
void swap::swapping(int &a,
int &b)
{
int c; // local variable
c=a;
a=b;
b=c;
}
void main ( )
{
clrscr ( );
int x=10, y=20;
swap r;
r.swapping (x,y);
cout<< “\n x=” <<
x<< “\t”<< “y=”<<y;
getch( );
}
Friend function
We know that the private members
cannot be access form outside the class i.e. a non-member function cannot have
an access to a private data of a class.
A friend function has access to
all private and protected members of the class for which it is a friend. To declear
a friend function, inside the prototype within the class, preceding it with the keyword friend.
A function declaration in the class is
preceeded with the keyword friend is known as friend function. The function is
defined else where in the program like a normal c++ function. The function definition
does not use either the keyword friend or the scope resolution operators.
Friends function although not a
member functions have full access right to the private member of the class.
A friend function possesses
certain special characteristics
1.
It is not in the scope of the
class to which it has been declared as friend.
2. Since
it is not in the scope of the class, it cannot be called using the object of
the class.
3. It
can be invoked like a normal function without the help if any object.
4. It
can be declare either in the public or the private part of the class without
affecting its meaning.
5.
Usually, it has the object as
arguments.
#include<iostream.h>
#include<conio.h>
class myclass
{
int a,b;
public :
friend int sum (myclass x);
void set_ab (int i, int j);
};
void myclass::set_ab(int i, int
j)
{
a=i;
b=j;
}
int sum (myclass x)
{
return x.a+x.b);
}
void main ( )
{
clrscr( );
myclass n; //object
n.set_ab(3,4);
cout<< “sum=” sum (n);
getch( );
}
A function friendly
to two classes c2; // forward declaration
class c1
{
int a;
public :
void setvalue (int i);
{
a=i;
}
friend void max (c1,c2); //
member function
};
class c2
{
int b;
public:
void setvalue (int j)
{
b=j;
}
friend void max(c1,c2 )
};
void max (c1m,c2n);
{
if (m.a>=m.b)
{
cout<<m.a;
}
else
{
cout<<n.b;
}
}
void main ( )
{
c1 obj1;
c2 obj2;
obj1.setvalue(50);
obje2.setvalue(100);
max(obj1,obj2)
getch( );
}
The function max ( ) has arguments from both c1 and
c2. When the function max ( ) is declared as a friend in c1 for the first time,
the compiler will not acknowledge the presence of c2 unless its name is
declared in the beginning as class c2;
This is known as
“forward” declaration.
No comments:
Post a Comment