0% found this document useful (0 votes)
79 views15 pages

Example1: (Using Friend Function With Class)

The document discusses friend functions in C++. A friend function has access to private and protected members of a class but is not a member of that class. It is declared using the friend keyword in the class definition. Two examples are given that demonstrate how to declare a friend function for a single class and for multiple classes. The document also discusses using the new operator to dynamically allocate memory for objects at runtime and the delete operator to free allocated memory. It provides examples of allocating memory for basic types, arrays, and multi-dimensional arrays.

Uploaded by

wisam
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)
79 views15 pages

Example1: (Using Friend Function With Class)

The document discusses friend functions in C++. A friend function has access to private and protected members of a class but is not a member of that class. It is declared using the friend keyword in the class definition. Two examples are given that demonstrate how to declare a friend function for a single class and for multiple classes. The document also discusses using the new operator to dynamically allocate memory for objects at runtime and the delete operator to free allocated memory. It provides examples of allocating memory for basic types, arrays, and multi-dimensional arrays.

Uploaded by

wisam
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/ 15

Object Oriented Programming Lecture 2

2nd class Asst. Lecturer Omar Nowfal

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();
}

The output of this program is: Mean Value = 32.5

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

Friend Function Features:


 It is not in the scope of the class to which it has been declared as friend; therefore it cannot
be called using the object of that class.
 It can be invoked like a normal function without the help of any object.
 Unlike member functions, it cannot access the member names directly and has to use an
object name and dot operator with each member name.
 It can be declared either in the public or private sections.
 Usually, it has the objects as arguments.

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 output of the above program is: Data= 90

Memory Management Operators


An object can be created by using new and destroyed by using delete as and when
required. A data object created inside a block with new will remain in existence until it is
explicitly destroyed by using delete. The new operator can be used to create objects of any type.
It takes the following general form :

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

There for the two statements after above:


*p = 25;  
*q = 7.5; 
assign 25 to the newly created int object and 7.5 to the float object . We can also initialize the
memory using new operator. This is done as follows:

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

The output of this program is:


Q:00C63D00 = 25
P:00C35190 = 5
After Delete:
Q:00C63D00 = -17891602
P:00C35190 = -17891602

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

The output of the above program is:


0 0 0 0 0 0 0 0 0 0
-17891602 -17891602 -17891602 -17891602 -17891602 -17891602 -17891602 -7891602
-17891602 -17891602

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

-17891602 -17891602 -17891602 -17891602


-17891602 -17891602 -17891602 -17891602
-17891602 -17891602 -17891602 -17891602

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);
};

In the main program we decelerate:


employee manager[3] ; / /the array manager contains three objects(manager) employee
foreman[15];
employee worker [75];

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

Objects as function arguments


Example: (Add Two Time in Hours, Minutes Using Friend Function)

#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();
}

The output of this program is:

The Addition of Two Time is: 0 : 0


Enter The Hours: 7
Enter The Minutes: 45

Enter The Hours: 8


Enter The Minutes: 30

The Time is: 7 : 45

33 
 
Object Oriented Programming Lecture 2
2nd class Asst. Lecturer Omar Nowfal

The Time is: 8 : 30


The Addition of Two Time is: 16 : 15

34 
 

You might also like