Example of function friendly to two classes
#include
using namespace std;
class abc;
class xyz
{
int x;
public:
void setvalue(int i) { x= i; }
friend void max (xyz,abc);
};
17
class abc
{
int a;
public:
void setvalue( int i) {a=i; }
friend void max(xyz,abc);
};
void max( xyz m, abc n)
{
if(m . x >= n.a)
cout<else
cout<< n.a;
}
int main( )
{
abc j;
j . setvalue( 10);
xyz s;
s.setvalue(20);
max( s , j );
return(0);
}
Example of function friendly to three classes
#include
using namespace std;
18
class E2;
class E3;
class E1
{
char name[10];
float salary;
public:
void set()
{
cout<<"\n Enter first Employee name and salary";
cin>>name>>salary;
}
friend void process(E1,E2,E3);
};
class E2
{
char name[10];
float salary;
public:
void set()
{
cout<<"\n Enter second Employee name and salary";
cin>>name>>salary;
}
friend void process(E1,E2,E3);
};
class E3
{
char name[10];
float salary;
public:
void set()
{
cout<<"\n Enter third Employee name and salary";
cin>>name>>salary;
}
friend void process(E1,E2,E3);
19
};
void process(E1 x, E2 y, E3 z)
{
cout<<"\n\nFirst Employee name= "<< x.name;
cout<<"\nFirst Employee salary= "<cout<<"\n\nSecond Employee name= "<< y.name;
cout<<"\nSecond Employee salary= "<cout<<"\n\nThirdEmployee name= "<< z.name;
cout<<"\nThird Employee salary= "<float total = x.salary + y.salary + z.salary;
cout<<"\n\n Their total salary= "<}
int main()
{
E1 A;
E2 B;
E3 C;
A.set();
B.set();
C.set();
process(A,B,C);
return 0;
}
Output:
20
Static Data Member
• The properties of static member variable are similar to that of a C static variable.
• A static member variable has certain special characteristics. These are:
o It is initialized to zero when the first object of its class is created. No other initialization
is permitted.
o 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.
o It is visible only within the class, but its lifetime is the entire program.
o Static variable are normally used to maintain values common to the entire class. For
example, a static data member can be used as a counter that records the occurrences of
all the objects.
Example 1 :
#include
using namespace std;
class item{
static int X ;
public:
void get()
{
cout<<"X = "<X++;
}
21
};
int item :: X;//definitation of static data member. can also be initialized as
// int item :: count = 10;
int main()
{
item a,b,c;
a.get();
b.get();
c.get();
return 0;
}
Output:
X = 0
X = 1
X = 2
Example 2
#include
using namespace std;
class item
{
static int count; //count is static
int number;
public:
void getdata(int a)
{
number=a;
count++;
}
void get_count(void)
{
cout<<"count:";
cout<}
};
int item :: count ; //count defined
22
int main( )
{
item a,b,c;
a.get_count( );
b.get_count( );
c.get_count( );
a.getdata(100);
b.getdata(200);
c.getdata(300);
cout<<"after reading data : "<a.get_count( );
b.get_count( );
c.get_count( );
return(0);
}
Output:
Count: 0
Count: 0
Count: 0
after reading data :
Count: 3
Count: 3
Count: 3
Static Member Function:
A static function can have access to only other static members (functions or variables) declared in the
same class
A static function can be called using the class-name (instead of its objects) as follows:
Class-name : : function-name
Example:
#include
using namespace std;
class test
{
int code;
static int count; // static member variable
public:
void setcode(void)
23
{
code=++count;
}
void showcode(void)
{
cout<<"object member : "<
}
static void showcount(void)
{ cout<<"count="< //cout< this can not be done here because static function will access only static variables
}
};
int test:: count;
int main()
{
test t1,t2;
t1.setcode( );
t2.setcode( );
test :: showcount ( );
test t3;
t3.setcode( );
test:: showcount( );//accessing static member function
t1.showcode( );
t2.showcode( );
t3.showcode( );
//test t4;
//t4.showcount(); //it can also be done
return(0);
}
Output
Function Overloading
24
Overloading refers to the use of the same thing for different purposes. C++ also permits overloading of
functions. It means, we can use the same function name to create functions that perform a variety of
different tasks. This is known as function polymorphism in OOP.
Example:
#include
using namespace std;
void print(int i)
{
cout << " \nThis is int " << i << endl;
}
void print(double f)
{
cout << " \nThis is float " << f << endl;
}
void print(char c)
{
cout << " \nThis is character " << c << endl;
}
int main()
{
print(10);
print(10.10);
print('t');
return 0;
}
Dostları ilə paylaş: |