Example1: (Using Friend Function With Class)
Example1: (Using Friend Function With Class)
Friend Function:
Sometimes we need a non-member function that has full access to the private data of a
class. We would like many classes to share a particular function. In such situations, we define
the function as a friend function with these classes. Such a function need not be a member of
any of these classes.
To declare an outside function “friendly” to a class, the function declaration should be preceded
by the keyword friend as shown below:
The friend function is defined elsewhere in the program like a normal C++ function. The
definition of friend function does not use either the keyword friend or the scope operator :: .
Example1: (Using Friend Function with Class)
#include <iostream>
#include <conio.h>
using namespace std;
class Sample
{
int x;
int y;
public:
void setval(int, int);
friend float mean(Sample);
};
20
Object Oriented Programming Lecture 2
2nd class Asst. Lecturer Omar Nowfal
void Sample::setval(int a, int b)
{
x=a;
y=b;
}
float mean (Sample S)
{
return (S.x+S.y)/2.0;
}
void main()
{
Sample S1;
S1.setval(25,40);
cout<<"Mean value ="<<mean(S1);
getch();
}
Example2: (Using Two Classes)
#include <iostream>
#include <conio.h>
using namespace std;
class ABC;
class XYZ
{
int a;
public:
void setval(int m)
{a=m;}
friend int max(ABC,XYZ);
21
Object Oriented Programming Lecture 2
2nd class Asst. Lecturer Omar Nowfal
};
class ABC
{
int b;
public:
void setval(int m)
{b=m;}
friend int max(ABC,XYZ);
};
int max(ABC A1,XYZ X1)
{
if (A1.b>=X1.a)
return A1.b;
else
return X1.a;
}
void main()
{
ABC N1;
N1.setval(50);
XYZ N2;
N2.setval(30);
cout<<"\n The max number of the two classes is =
"<<max(N1,N2);
getch();
}
The output of this program is: The max number of the two classes is = 50
Member functions of one class can be friend functions of another class. In such cases, they are
defined using the scope resolution operator as shown below:
22
Object Oriented Programming Lecture 2
2nd class Asst. Lecturer Omar Nowfal
23
Object Oriented Programming Lecture 2
2nd class Asst. Lecturer Omar Nowfal
Friend Class
We can also declare all the member functions of one class as the friend functions of
another class. In such cases, the class is called a friend class.
This can be specified as follows:
Example: (Using friend class)
#include <iostream>
#include <conio.h>
using namespace std;
class alpha
{
private:
int data;
public:
alpha()
{data=90;}
friend class beta;
};
class beta
{
public:
void printfunc(alpha X)
{cout<<"\n Data= "<<X.data;}
};
24
Object Oriented Programming Lecture 2
2nd class Asst. Lecturer Omar Nowfal
void main()
{
alpha A;
beta B;
B.printfunc(A);
getch();
}
The pointer-variable is pointer of type data-type. The new operator allocates sufficient memory
to hold data object of type data-type and return the address of the object.
Example:
int *p;
float *q;
p= new int ;
q= new float;
We can combine the declaration of pointers and their assignment as follows:
int *p = new int ;
float *q = new float ;
25
Object Oriented Programming Lecture 2
2nd class Asst. Lecturer Omar Nowfal
new can be used to create a memory space for any data type including user defined types such
as array, structure and class. The general form for one dimensional array is:
Example: (Using new)
#include <iostream>
#include <conio.h>
using namespace std;
void main()
{
int *Q=new int;
*Q=25;
int *P=new int(5);
cout<<"\n Q:"<<Q<<" ="<<*Q;
cout<<"\n P:"<<P<<" ="<<*P;
cout<<endl;
delete P;
delete Q;
cout<<"\n After Delete:"<<endl;
cout<<"\n Q:"<<Q<<" ="<<*Q;
cout<<"\n P:"<<P<<" ="<<*P;
getch();
}
26
Object Oriented Programming Lecture 2
2nd class Asst. Lecturer Omar Nowfal
Example: By using (new) command write a program to create 1D
array with initial of zeroes
#include <iostream>
#include <conio.h>
using namespace std;
const int S=10;
void main()
{
int *aray=new int[S]();
cout<<endl;
for (int i=0;i<S;i++)
cout<<" "<<aray[i]<<" ";
cout<<endl;
delete [] aray;
cout<<endl;
for (int i=0;i<S;i++)
cout<<" "<<aray[i]<<" ";
cout<<endl;
getch();
}
27
Object Oriented Programming Lecture 2
2nd class Asst. Lecturer Omar Nowfal
But when creating multi-dimensional arrays with new, the array must be decelerate as the
following example.
Example:
By using (new) command write a program to create 2D array with initial of zeroes?
#include <iostream>
#include <conio.h>
using namespace std;
const int M=3,N=4;
void main()
{
auto aray=new int [M][N]();
for(int i=0;i<M;i++)
{
for (int j=0;j<N;j++)
cout<<aray[i][j]<<" ";
cout<<endl;
}
delete [] aray;
cout<<endl;
for(int i=0;i<M;i++)
{
for (int j=0;j<N;j++)
cout<<aray[i][j]<<" ";
cout<<endl;
}
getch();
}
28
Object Oriented Programming Lecture 2
2nd class Asst. Lecturer Omar Nowfal
Another method:
//This program using dynamic allocation of 2D Matrix
#include <iostream>
#include <conio.h>
using namespace std;
void main()
{
int R, C;
cout << "Enter the no of rows: ";
cin >> R;
cout << "Enter the no of coumns: ";
cin >> C;
int **Matrix = new int *[R]; //to create R rows
for (int i = 0; i < R; i++)
Matrix[i] = new int[C](); //to create C coulms with initail value
zero of all matrix
for (int i = 0; i < R; i++)
{
for (int j = 0; j < C; j++)
cout << Matrix[i][j] << " ";
cout << endl;
}
for (int i = 0; i < C; i++)
delete[] Matrix;
_getch();
}
This program is used two methods to create 2D array with initial value with Zero by using
New Command.
The output of this program is:
0 0 0 0
0 0 0 0
0 0 0 0
29
Object Oriented Programming Lecture 2
2nd class Asst. Lecturer Omar Nowfal
0 0 0 0
0 0 0 0
0 0 0 0
When data object is no longer needed, it is destroyed to release the memory space for reuse.
The general form of its use is:
Array of Object
We can also array of variables that are of the type class. Such variables are called arrays
of objects. Consider the following class definition:
class employee
{
char name [30];
float age ;
public:
void getdata(void);
void putdata(void);
};
Example:
#include <iostream>
#include <conio.h>
using namespace std;
class Employee
30
Object Oriented Programming Lecture 2
2nd class Asst. Lecturer Omar Nowfal
{
char Name[10];
float age;
public:
void indata();
void printdata();
};
void Employee::indata()
{
cout<<"Enter the employee name? ";
cin>>Name;
cout<<"Enter employee age? ";
cin>>age;
}
void Employee::printdata()
{
cout<<"Name: "<<Name<<endl;
cout<<"Ege: "<<age<<endl;
cout<<endl;
}
void main()
{
Employee emp[3];
for(int i=0;i<3;i++)
emp[i].indata();
for(int i=0;i<3;i++)
emp[i].printdata();
getch();
}
31
Object Oriented Programming Lecture 2
2nd class Asst. Lecturer Omar Nowfal
#include <iostream>
#include <conio.h>
using namespace std;
class Time
{
int Hours;
int Minites;
public:
Time()
{Hours=0;Minites=0;}
void intime();
void Printtime();
void friend Addtime(Time,Time);
};
void Time::intime()
{
cout<<"Enter The Houres: ";
cin>>Hours;
cout<<"Enter The Minites: ";
cin>>Minites;
cout<<endl;
}
void Time::Printtime()
{
cout<<"The Time is: "<<Hours<<" : "<<Minites<<endl;
}
void Addtime(Time t1, Time t2)
32
Object Oriented Programming Lecture 2
2nd class Asst. Lecturer Omar Nowfal
{
int M,H;
M=t1.Minites+t2.Minites;
H=M/60;
M=M%60;
H+=t1.Hours+t2.Hours;
cout<<"The Addition of Two Time is: "<<H<<" : "<<M;
cout<<endl;
}
void main()
{
Time Ti1, Ti2;
Addtime(Ti1,Ti2);
Ti1.intime();
Ti2.intime();
Ti1.Printtime();
Ti2.Printtime();
Addtime(Ti1,Ti2);
getch();
}
33
Object Oriented Programming Lecture 2
2nd class Asst. Lecturer Omar Nowfal
34