DEFINITION OF A FRIEND
F3031 OBJECT ORIENTED PROGRAMMING
F3031 Object Oriented Programming
1.0
2.0
3.0
4.0
2.4 Create classes and functions as FRIEND
F3031 OBJECT ORIENTED PROGRAMMING
OBJECTIVES TOPIC 2.4
1
2
DEFINITION OF A FRIEND
1. A function or class can be defned as a friend to other classes
2. If a function or class becomes the friend to another class, for example class or function A becomes the friend to class C, class or function A can access private member of class C.
3. This concept is considered important to apply if there is a situation, which we want a class or function wants to access the private data from the class.
DECLARING A FRIEND
I. Declaring function as the friend
II. Declaring class as the friend The keyword friend should be placed in front of the function or class, which wants to announce as the friend in the class. For example, if the function calculate() as the friend to Maths class, so the announcement of a friend should be done in the Maths class.
1. By placing the keyword friend in front of the function’s name, the function can achieve private member of the class. The declaration of friend function is done in public.
2. The program below stated is an example of the mean for function, which become friend for staf class. DECLARING FUNCTION AS A FRIEND class pekerja { // ….. public: friend void kira_gaji( pekerja a); //…. }
Example: Function void
count_salary();which has become the friend for the class staff. By this declaration, this function can achieve private member; which is staff class.
Cont..
The example below shows how the calculation of area for the rectangle is
made using friend.Kelas SegiEmpat Fungsi Kira_Luas
Ahli kelas SegiEmpat yang disimpan ditempat yang sulit (data-data private)
Pengawal keselamatan akan memeriksa sama ada fungsi Kira_luas berhak atau tidak mencapai data-data dari kelas SegiEmpat dan jika fungsi kira_luas diisytiharkan sebagai rakan oleh kelas SegiEmpat, maka ia boleh mencapai data-data dari kelas SegiEmpat.
Cont..
#include<iostream.h> class SegiEmpat{
Friend function
int panjang,lebar;
declaration to
public:
enable
Kira_Luas SegiEmpat(int a,int b){panjang=a;
() function to access
lebar=b;}
member private data
friend int Kira_Luas(SegiEmpat x);
from
SegiEmpat }; class. int Kira_Luas(SegiEmpat x) {return x.panjang * x.lebar; } void main()
Defining function
{int a, b;
() that receive
Kira_Luas cout<<"Sila masukkan panjang:";
object as rectangle shows it
cin>>a;
can access length and width
cout<<"Sila masukkan lebar:";
value for the mentioned
cin>>b; object. SegiEmpat tepat(a,b); cout<<Kira_Luas(tepat); }
example without using friend, Cont...( declare function as member for class )
#include<iostream.h>
Friend function declaration to
class SegiEmpat{
enable
Kira_Luas int panjang,lebar;
() function to access
public:
member private data
SegiEmpat(int a,int b){panjang=a;
from
SegiEmpat lebar=b;} class. friend int Kira_Luas(SegiEmpat x); }; int Kira_Luas(SegiEmpat x) {return x.panjang * x.lebar; }
Defining function () that receive
Kira_Luas void main()
object as rectangle shows it
{int a, b;
can access length and width
cout<<"Sila masukkan panjang:";
value for the mentioned
cin>>a; object. cout<<"Sila masukkan lebar:"; cin>>b; SegiEmpat tepat(a,b); cout<<Kira_Luas(tepat); }
Cont..
The example below shows how the use of two diferent types of class in function of a friend.
See fiure, to do compute() function. It can achieve private class practical class theory data in practical and theory private : mark private : mark class.
It is possible for this function to access private data directly. ? compute( ) practical mark
It not possible. The only solution theory mark is by usini a friend.
In here, a friend can be used Acceptini data’s from 2 classes by compute() function to access function compute() private data from practical and theory class.
Cont..
An example of an complete program
# include <iostream.h>
Forward declaration
class practical;
class theory{ private : int mark; public : void enter_dataTheory(){ cout << “Theory’s mark : \n”; cin >> mark;} friend void compute ( theory A, practical B ); };
class practical{ private : int mark; public : cont next page.. void enter_dataPractical() { cout << “Practical’s mark : \n”; cin >> mark; }
friend void compute ( theory A, practical B );
};
Cont..
int main () {
Output
theory suzi1; practical suzi2; suzi1.enter_dataTheory (); suzi2.enter_dataPractical(); compute (suzi1,suzi2); return 0; } void compute (theory A, practical B) { cout <<”Result :\n”; cout << A.mark + B.mark; }
NOTES
You must do forward declaration because theory class refers to practical class but code for practical class appears after theory class.
DECLARING CLASS AS A FRIEND
1. To make a class as a friend for another class, declaration of a friend has to be done on a prototype class.
2. Declaration of friend has to been done using keyword friend.
3. Example below shows how class square was declared as a friend to class shape.
class bentuk{ friend class segiempat;
Shape class declared …..
public:
class Square as a friend to
…..};
class Shape. This means
class segiempat
the class Square can
{
achieve private member
private: for class Shape. ….
.
public: ……};
Below is an example which shows class markah_pelajar becomes friend to class info_pelajar to enable data member info_pelajar access data member from class markah_pelajar.
#include <iostream.h> #include <string.h> class markah_pelajar; class info_pelajar { friend class markah_pelajar; private: char nama[30],ic[10]; public: void setdata(char *,char *); }; void info_pelajar::setdata(char * a,char *b) { strcpy(nama,a); strcpy(ic,b); } class markah_pelajar{ float markah1,markah2,jumlah; public: void setmarkah(float,float); void kiraMarkah(); void paparan(info_pelajar); }; void markah_pelajar::setmarkah(float ujian1, float ujian2) { markah1=ujian1; markah2=ujian2; }
Cont next Page…
void main() void markah_pelajar::kiraMarkah() { { char nama[30],ic[10]; jumlah=markah1 + markah2; float markah1,markah2; } markah_pelajar a; void markah_pelajar::paparan(info_pelajar a) info_pelajar b; { cout<<"Nama:"; cout<<"Nama Pelajar:"<<a.nama<<"\n"; cin.getline(nama,30); cout<<"No IC:"<<a.ic<<"\n"; cout<<"No KP:"; cout<<"Markah Ujian 1:"<<markah1<<"\n"; cin.getline(ic,10); cout<<"Markah Ujian 2:"<<markah2<<"\n"; cout<<"Markah Ujian 1:"; cout<<"Jumlah Markah:"<<jumlah<<"\n"; cin>>markah1; }
Output
cout<<"Markah Ujian 2:"; cin>>markah2; cout<<'\n'; b.setdata(nama,ic); a.setmarkah(markah1,markah2); a.kiraMarkah(); a.paparan(b); }
The example below shows how an object from class Bclass access private
member from class AClass.#include <iostream.h> class AClass { class BClass friend class BClass ;/* BClass ialah friend {
kepada Aclass */
private : private : AClass Avariable; /* Avariable int x; protected :
sekarang ialah objek bagi
void doublex(void){
Aclass */
x *= x; } public : public : AClass() // fungsi penyaratan constructor void showValues(void); {x= 10;} }; AClass(int n) // penyaratan fungsi
//Aclass yang sebelumnya
{ x = n; Cont Next Page.. } };
int main(){ BClass BC; BC.showValues();
EXPLANATIO
return 0; }
N The program above shows how members for each
void BClass::showValues(void)
class achieve private member from class friend. Since
{
Bclass declared as a friend in Aclass, member of
AClass AC(123);
Bclass(showvalues()) can accesss private member /* mencapai medan dan fungsi in Aclass. Function showValues in Bclass enables private serta to achieve directly into private x in Aclass. protected bagif Avariable.*/
cout<<”\n Before, Avariable.x = “<<Avariable.x; Avariable.doublex();
Output
cout<< “\nAfter,Avariable.x = “<<Avariable.x; cout<<”\nAC.x = “<<AC.x ;// merujuk
kepada //pembolehubah tempatan //bagi data private
}