F3031 Object Oriented Programming
1.0
2.0
3.0
2.0 CLASSES AND OBJECTS
OBJECTIVES TOPIC 2.4
1
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.
1. The declaration for friend can be done through numerous ways:
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
The example below shows how the calculation of area for the rectangle is
made using friend.
Ahli kelas SegiEmpat yang disimpan ditempat yang sulit (data-data private)
Kelas SegiEmpat Fungsi
Kira_Luas
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.
#include<iostream.h>
class SegiEmpat{ int panjang,lebar; public:
SegiEmpat(int a,int b){panjang=a; lebar=b;}
friend int Kira_Luas(SegiEmpat x); };
int Kira_Luas(SegiEmpat x) {return x.panjang * x.lebar; }
void main() {int a, b;
cout<<"Sila masukkan panjang:"; cin>>a;
cout<<"Sila masukkan lebar:"; cin>>b; SegiEmpat tepat(a,b); cout<<Kira_Luas(tepat); } Friend function declaration to
enable Kira_Luas () function to access member private data from SegiEmpat class.
Defining function
Kira_Luas() that receive object as rectangle shows it can access length and width value for the mentioned object.
#include<iostream.h>
class SegiEmpat{ int panjang,lebar; public:
SegiEmpat(int a,int b){panjang=a; lebar=b;}
friend int Kira_Luas(SegiEmpat x); };
int Kira_Luas(SegiEmpat x) {return x.panjang * x.lebar; }
void main() {int a, b;
cout<<"Sila masukkan panjang:"; cin>>a;
cout<<"Sila masukkan lebar:"; cin>>b; SegiEmpat tepat(a,b); cout<<Kira_Luas(tepat); } Friend function declaration to
enable Kira_Luas () function to access member private data from SegiEmpat class.
Defining function
Kira_Luas() that receive object as rectangle shows it can access length and width value for the mentioned object.
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
data in practical and theory
class.
It is possible for this function to
access private data directly. ?
It not possible. The only solution
is by usini a friend.
In here, a friend can be used
compute() function to access
private data from practical and
Acceptini data’s from 2 classes by
function compute()
class theory
private : mark
class practical
private : mark
compute( )
practical mark theory mark
Cont...
An example of an complete program
# include <iostream.h>
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 :
void enter_dataPractical() {
cout << “Practical’s mark : \n”; cin >> mark;
}
friend void compute ( theory A, practical B );
Forward declaration
Cont...
NOTES
You must do forward declaration because theory
class refers to practical class but code for practical
class appears after theory class.
int main () {
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; }
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.
DECLARING CLASS AS A
FRIEND
class bentuk{
friend class segiempat; …..
public: …..};
class segiempat {
private: ….
public:
Shape class declared
class Square as a friend to
class Shape. This means
the class Square can
achieve private member
for class Shape.
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)
void markah_pelajar::kiraMarkah() {
jumlah=markah1 + markah2; }
void markah_pelajar::paparan(info_pelajar a) {
cout<<"Nama Pelajar:"<<a.nama<<"\n"; cout<<"No IC:"<<a.ic<<"\n";
cout<<"Markah Ujian 1:"<<markah1<<"\n"; cout<<"Markah Ujian 2:"<<markah2<<"\n"; cout<<"Jumlah Markah:"<<jumlah<<"\n"; } void main() { char nama[30],ic[10]; float markah1,markah2; markah_pelajar a; info_pelajar b; cout<<"Nama:"; cin.getline(nama,30); cout<<"No KP:"; cin.getline(ic,10);
cout<<"Markah Ujian 1:"; cin>>markah1;
The example below shows how an object from
class Bclass
access private
member from
class AClass
.
#include <iostream.h> class AClass
{
friend class BClass ;/* BClass ialah friend
kepada Aclass */
private : int x;
protected :
void doublex(void){ x *= x; }
public :
AClass() // fungsi penyaratan constructor
{x= 10;}
AClass(int n) // penyaratan fungsi
//Aclass yang sebelumnya
{
x = n;
class BClass {
private :
AClass Avariable; /* Avariable
sekarang ialah objek bagi Aclass */
public :
void showValues(void); };
int main(){ BClass BC;
BC.showValues(); return 0;
}
void BClass::showValues(void) {
AClass AC(123);
/* mencapai medan dan fungsi private serta
protected bagif Avariable.*/
cout<<”\n Before, Avariable.x = “<<Avariable.x;
Avariable.doublex();
cout<< “\nAfter,Avariable.x = “<<Avariable.x;
cout<<”\nAC.x = “<<AC.x;// merujuk kepada //pembolehubah tempatan //bagi data private
}
The program above shows how members for each class achieve private member from class friend. Since
Bclass declared as a friend in Aclass, member of
Bclass(showvalues()) can accesss private member in Aclass. Function showValues in Bclass enables to achieve directly into private x in Aclass.
EXPLANATIO
N