P. 1
Object Oriented Programming

Object Oriented Programming

|Views: 3|Likes:
Published by Supriyadi
Pemrograman berorientasi Object Dengan PHP
Pemrograman berorientasi Object Dengan PHP

More info:

Categories:Topics
Published by: Supriyadi on Dec 08, 2013
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

12/09/2014

pdf

text

original

Object-oriented Programming

FTI - Budi Luhur - Jakarta
1
konsep Obgect Oriented Progrumming

Erpal |orsep yarg rarus dapal d|r||||| o|er sualu oerlu| perrograrar yarg oeror|erlas| oo¡ecr
ada|ar:
Ercapsu|al|or
Aoslracl|or
lrrer|larce
Po|yrorpr|sr

Encapsu|ar|on ada|ar peryaluar arlara proses-proses yarg lerjad| dar sejur|ar dala yarg d||e|o|a |eda|ar
sualu oerlu| |e|orpo| lerlerlu. Perge|orpo|ar lerseoul ada|ar dergar d|oerlu|rya sualu ooye| secara aoslra|
(aosrracr|on).

/osrracr|on rer||||| pergerl|ar oarWa da|ar rergeroarg|ar perarg|al |ura| seorarg prograrrer a|ar
rerggura|ar alau rerc|pla|ar sejur|ar ooye| d|rara ooye| lerseoul a|ar ada dar oerper||a|u pada s|sler

lnner|rance alau peWar|sar ada|ar rerupa|ar |orsep d|rara sualu ooye| rer||||| sejur|ar per||a|u yarg
d|r||||| o|er ooye| |a|r da|ar sualu r|rar|| lerlerlu.
Po|yrorpr|sr ada|ar |orsep d|rara oeoerapa ooye| dapal rer||||| relode yarg sara dergar a|s| yarg
oeroeda.


CIuss Construct

0|ass 0onsrrucr ada|ar cara yarg d||a|u|ar da|ar reroerlu| sualu aosrracr oara r,pe. Pada C÷÷,
aoslracl lype d|de||aras||ar dergar cara
Class <nama class>
{
<deklarasi member privat>
Public
<deklarasi member publik>
};
<tipe> <nama class> :: <function member> ( [argumen])
{
<statement>
};

Vemoer c|ass dapal oerupa allr|oul dar| c|ass, da|ar ra| |r| rerupa|ar oara memoer dar sejur|ar relode
yarg rerupa|ar lurcl|or reroer. 3cope yarg dapal d|oer||ar urlu| c|ass reroer ada|ar pr|vare dar puo||c.
Veroer dergar Pr|vale scope rarya dapal d|a|ses dar| dar o|er serua reroer pada |e|as d|rara reroer
lerseoul d|de||aras||ar. Veroer dergar Puo||c scope a|ar dapal d|a|ses dar| da|ar dar d||uar c|ass d|rara reroer
lerseoul d|de||aras||ar.

Corlor
# include <iostream.h>
# include <conio.h>

class Lingkaran
{
public:
Object-oriented Programming
FTI - Budi Luhur - Jakarta
2
float jari_jari;
void diameter();
float luas();
};
void Lingkaran :: diameter()
{
float x;
x = jari_jari * 2;
cout << diameter << x <<endl ;
};
float lingkaran :: luas()
{
clrscr();
float x;
x = 3.14 * (jari_jari * jari_jari);
return x;
};

CIuss dun Obgect

0e|aras| oojecl d||a|u|ar dergar rerggura|ar oojecl var|aoe| yarg d|de||aras||ar pada pada prograr
ulara. Perde||aras|ar dapal d||a|u|ar dergar dua cara ya|lu dergar rerggura|ar nama dar dergar
rerggura|ar pointer. Perde||aras|ar dergar rerggura|ar rara pada c÷÷ d||a|u|ar dergar cara

void main()
{
Lingkaran coin;
float x;
coin jari_jari = 2;
coin.diameter();
x = coin.luas();
cout << “luas lingkaran =“<< x << endl ;
}

Perde||aras|ar dergar rerggura|ar po|rler pada C÷÷ d||a|u|ar dergar cara

void main()
{
Lingkaran *coin;
float x;
coin ->jari_jari = 2;
coin ->diameter();
x = coin->luas();
cout << “ luas Lingkaran = “<< x << endl ;
}

Constructor

0onsrrucror rerupa|ar sualu relode (lurgs|) |rusus yarg d|r||||| o|er sualu |e|as. Furgs| corslruclor
a|ar se|a|u d||erja|ar apao||a sualu oojecl dar| |e|as lerseoul d|-a|o|as||ar |e-reror|. Pada C÷÷ rara dar| lurgs|
corslruclor ada|ar sara dergar rara dar| |e|as d|rara lurgs| lerseoul oerada.

Object-oriented Programming
FTI - Budi Luhur - Jakarta
3
# include <iostream.h>
# include <conio.h>
class x
{
int x, y;
public:
x() ; // Constructor
void f1();
};
void x::x()
{
cout << “initial X”;
}
void x::f1()
{
cout << “metode F1() pada class X “<< endl ;
};

void main()
{
x obj;
obj.f1();
}

Corslruclor dapal rer||||| sejur|ar parareler. Peroer|ar r||a| pada argurer urlu| corlruclor d||a|u|ar pada saal
sualu oojecl var|aoe| d|de||aras||ar

Confoh:
# include <iostream.h>
# include <conio.h>

class x
{
int x,y;
public:
x ( int m, int n); // constructor
void f1();
};
void x::x ( int m, int n)
{
x ~ m,
y ~ n,
couf ·· "HosiI perkoIionnyo " ·· x^y ·· endI,
}

void x::fI()
{
couf ·· "FI() moduI yong odo podo cIoss X "·· endI ,
},

de||aras| oojecl var|aoe| pada oojecl yarg d|r||||| corslruclor dergar parareler d||a|u|ar dergar cara
void main()
Object-oriented Programming
FTI - Budi Luhur - Jakarta
4
{
x w(5,1);
w.f1();
// body of main program
}

Destructor

0eslruclor ada;ar sualu relode alau lurgs| |rusus yarg d|r||||| o|er sualu |e|as. 0eslruclor a|ar se|a|u
d||erja|ar apao||a sualu oojecl d|-a|o|as||ar dar| reror|.

Confoh
# include <iostream.h>
# include <conio.h>

class x
{
int x, y;
public:
~x() ; // Destructor
void f1();
};
void x::~x()
{
cout << “Destructor dijalankan” << endl;
cout << “terminate X ” << endl;
}
void x::f1() { cout << “metode F1() pada class X “<< endl };

void main()
{
clrscr();
x y;
y.f1();
}

Inheritunce

lrrer|larce alau peWar|sar rerurg||r|ar sualu oer|ve c|ass rer||||| s|lal-s|lal yarg sara dergar yarg
d|r||||| o|er oase c|ass . oase c|ass ada|ar |e|as yarg rerurur|ar sualu 0|ass oaru lerlerlu.
0er|ve c|ass ada|ar c|ass lururar. 0er|ve c|ass dapal pu|a oerl|rda| seoaga| oase c|ass apao||a lerdapal
c|ass |a|r yarg rerjad| lururarrya. 0e||aras| pada C÷÷ ada|ar:

Class < nama base class>
{
<deklarasi member untuk base class>
};

class <nama derive class> : public <nama base class>
{
<deklarasi member untuk derive class>;
Object-oriented Programming
FTI - Budi Luhur - Jakarta
5
}

3cope oaru yarg rurg||r l|rou| a||oal dar| |rrer|rlarce ada|ar d|rara sualu reroer dar| sualu |e|as
rarya dapal d|a|ases pada alau o|er c|ass-c|ass lururarrya. Pada C÷÷ scope seperl| |r| d|de||aras||ar seoaga|
protected

Confoh:
# include <iostream.h>
# include <conio.h>

class x
{
int a;
protected:
int b;
public:
int c
void f1();
};
void x::f1()
{
cout << “metode f1() dieksekusi ”<< endl;
};
class y : public x
{
int d;
public:
float e;
void f2();
};

void y::f2()
{
cout << “metode F2() dieksekusi “<< endl ;
};

void main()
{
clrscr();
x obj1;
y obj2;
obj2.f1();
obj2.f2();
... // tubuh dari program
}

Contructor dun Destructor pudu Inheritunce

Corslruclor pada oase c|ass rarya d||a|u|ar pada c|ass yarg lerseoul saja. Corslruclor pada der|ve c|ass
a|ar re||oal|ar corslruclor pada oase c|ass. 0eslruclor pada oase c|ass a|ar re||oal|ar der|ve c|ass. 0eslruclor
pada 0er|ve c|ass rarya a|ar re||oal|ar c|ass lerseoul.
Object-oriented Programming
FTI - Budi Luhur - Jakarta
6

Corslruclor tanpa parareler: corlor

# include <iostream.h>
# include <conio.h>
// deklarasi base class
class dasar
{
public:
dasar();
~dasar();
};
void dasar::dasar()
{
cout << “ Constructor kelas dasar “ << endl;
}
void dasar::~dasar()
{
cout << “ Destructor kelas dasar “ << endl;
}

//deklarasi kelas turunan
class turunan : public dasar
{
public:
turunan();
~turunan();
};
void turunan :: turunan()
{
cout << “ Constructor kelas turunan “ << endl;
}
void turunan :: ~turunan()
{
cout << “ Destructor kelas turunan “ << endl;
}

void main()
{
clrscr();
cout << “varibale X adalah kelas dasar “ <<endl ;
dasar x;
cout << “varibale Y adalah kelas Turunan “ <<endl ;
turunan y;
}

Corslruclor dengan parameter. Corlor:

// Dekarasi base class
class dasar {
int x;
public:
Object-oriented Programming
FTI - Budi Luhur - Jakarta
7
void dasar( int xx)
int get_x();
}

void dasar :: dasar (int xx) { x = xx;}

int dasar ::get_x() {return x;}

// deklarasi Derive class I
class turunan1 : public dasar {
int y;
public:
void turunan1 (int xx, int yy) : dasar (xx);
int get_y();
}

void turunan1::turunan1(int xx, int yy) : dasar (xx) { y = yy };

int turunan1 :: get_y() { return y;};

// deklarasi Derive class II
class turunan2 : public turunan1
{
int z;
public:
void turunan2 (int xx, int yy, int zz) : turunan1(xx, yy);
int get_z();
}
void turunan2::turunan2(int xx, int yy, int zz ) : turunan1(xx, yy)
{ z = zz };
int turunan2 :: get_z()
{ return z;};

//Main program
void main()
{
turunan2 obj(9, 3, 7);
cout << “Nilai X : << obj.get_x() << endl;
cout << “Nilai X : << obj.get_y() << endl;
cout << “Nilai X : << obj.get_z() << endl;
}


MuItipIe inheritunce

Pada |asus lerlerlu a|ar d|rurg||r|ar oarWa sualu oer|ve c|ass a|ar rer||||| |eo|r dar| salu oase c|ass,
dergar |ala |a|r oarWa oeoerapa c|ass a|ar rer||||| lururar pada salu c|ass yarg sara. Po|a peWar|sar seperl| |r|
d|seoul dergar Vu|r|p|e |nner|rance. Pada C÷÷ de||aras| yarg d|oer||ar urlu| |asus |r| ada|ar:

Class <nama base class>
{
< deklarasi member untuk base class>;
Object-oriented Programming
FTI - Budi Luhur - Jakarta
8
};

class <nama derive class>:public<nama base class I>,public<nama base class n>
{
< deklarasi member untuk derive class>;
};

Confoh:

# include <iostream.h>
# include <conio.h>

class dasar1 {
public:
int x;
void f1();
};

void dasar1::f1() {
cout << “Metode f1() pada class dasar” << endl ;
}

class dasar2 {
public:
int w;
void f2();
};
void dasar1::f1() {
cout << “ Metode f2() pada class dasar “ << endl ;
}

class turunan: public dasar1, public dasar2 {
public :
int z;
void f3();
}

void dasar1::f1() {
cout << “ Metode f3() pada class turunan “<< endl ;
}

void main() {
clrscr();
turunan zz;
zz.f3()
dasar1 c ;
c.f3(); // error karena f3() bukan member dari dasar1
...... //tubuh dari program
}


Object-oriented Programming
FTI - Budi Luhur - Jakarta
9
PoIymorphism

Po|yrorpr|sr rerurg||r|ar oeoerapa c|ass rer||||| relode yarg sara. Velode yarg sara dergar
c|ass yarg secara |ese|ururar oeroeda ada|ar ra| yarg urur. Kasus d|rara sualu der|ve c|ass rer||||| relode
yarg sara dergar oase c|ass . Kasus d|alas rarus d|largar| dergar perde||aras|ar relode lerseoul seoaga|
merooe v|rrua|. Velode v|rlua| a|ar dapal d|perral||ar apao||a peruju|ar yarg d||era|ar lerradap |e|as lerseoul
ada|ar dergar rerggura|ar po|rler. Peruju|ar lerradap sualu |e|as dapal d||a|u|ar dergar 2 (dua) cara ya|lu:

Verggura|ar rara alau ear|, o|no|ng alau srar|c o|no|ng
Verggura|ar po|rler alau |are o|no|ng alau o,nam|c o|no|ng

Ear|, o|no|ng, peruju|ar d||a|u|ar pada saal proses |orp||as| d||a|u|ar, o,nam|c o|no|ng, peruju|ar d||a|u|ar pada
saal prograr d|e|se|us|.

Perde||aras|ar relode v|rlua| pada oase c|ass a|ar ||ul reWa|||| se|urur der|ve c|ass urlu| relode yarg
sara. Perde||aras|ar relode v|rlua| pada der|ve c|ass rerga||oal|ar relode yarg d|a|o|as||ar ada|ar relode
yarg ada pada oase c|ass yarg apao||a relode yarg ada pada oase c|ass lerseoul l|da| d|de||aras||ar urlu| oerlu|
v|rlua|.

6ontoh 1

# include <iostream.h>
# include <conio.h>
class dasar {
public : void cetak();
};

void dasar :: cetak() {
cout << “metode cetak dari class dasar” << endl ;
};

class turunan: public dasar {
public : void cetak();
};

void turunan ::cetak() {
cout << “metode cetak dari class turunan” << endl ;
};

void main() {
clrscr();
dasar d ;
turunan t ;
t.cetak();
// coba ditambahkan statement d.cetak ()
}

Contoh Z

# include <iostream.h>
# include <conio.h>
class dasar {
Object-oriented Programming
FTI - Budi Luhur - Jakarta
10
public : void cetak();
};
void dasar::cetak() {
cout << “metode cetak dari class dasar” << endl ;
};
class turunan: public dasar {
public : void cetak();
};
void dasar::cetak() {
cout << “metode cetak dari class turunan” << endl ;
};
void main() {
dasar *d;
turunan t;
d = &t ;
d->cetak();
}

Contoh 3

# incIude ·iosfreom.h·
# incIude ·conio.h·

cIoss dosor {
pubIic : void cefok(),
},
cIoss dosor::cefok() {
couf ·· "mefode cefok dori cIoss dosor" ·· endI ,
}
cIoss furunon: pubIic dosor {
pubIic : virtuuI void cefok(),
},
void furunon ::cefok() {
couf ·· "mefode cefok dori cIoss furunon" ·· endI ,
},
void moin() {
dosor^ d:
furunon f:
d ~&f,
d -·cefok(),
}

Contoh 4

# incIude ·iosfreom.h·
# incIude ·conio.h·
cIoss dosor {
pubIic : virtuuI void cefok(),
},
void dosor::cefok() {
couf ·· "mefode cefok dori cIoss dosor" ·· endI ,
},
Object-oriented Programming
FTI - Budi Luhur - Jakarta
11
cIoss furunon: pubIic dosor {
pubIic : void cefok(),
},
void dosor::cefok() {
couf ·· "mefode cefok dori cIoss furunon" ·· endI ,
},

void moin() {
dosor^ d,
furunon f,
d~ &f,
f.cefok(),
d-·cefok(),
}




You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->