Rabu, 23 September 2009

Polimorfisme

adalah kemampuan untuk memanggil fungsi yang berbeda dengan hanya menggunakan satu jenis fungsi panggil. It is a lot useful since it can group classes and their functions together. Ini banyak berguna karena dapat mengelompokkan kelas dan fungsi mereka bersama-sama. It is the most important part of Object-Oriented Programming. Ini adalah bagian terpenting dari Object-Oriented Programming. Polymorphism is the core of object-oriented programming .C++ supports polymorphism by allowing member functions defined in classes to be overridden with member functions having the same names, but different implementations, in derived classes. Polimorfisme adalah inti dari pemrograman berorientasi obyek. C + + mendukung polimorfisme dengan memungkinkan fungsi-fungsi anggota kelas didefinisikan dalam harus diganti dengan fungsi-fungsi anggota memiliki nama yang sama, tetapi implementasi yang berbeda, di kelas turunan. In selecting the appropriate member function to call in response to a function invocation, C++ distinguishes between the static type of a reference and the dynamic type of the object it refers to at a given point. Dalam memilih fungsi anggota yang sesuai untuk menanggapi panggilan ke salah satu fungsi doa, C + + membedakan antara jenis yang statis dan dinamis referensi jenis objek yang mengacu pada suatu titik tertentu. The dynamic type must be a descendant of the static type The invocation is type-checked based on the static type of the reference. Jenis yang dinamis harus merupakan keturunan dari jenis statis Doa adalah jenis-diperiksa berdasarkan jenis statis referensi. If the function called is a virtual member function, the member function associated with the actual object pointed to is called dynamically at run time. Jika fungsi dipanggil adalah fungsi anggota virtual, fungsi anggota yang terkait dengan menunjuk objek yang sebenarnya disebut secara dinamis pada saat run time. If the function is non-virtual, the call will have been statically bound to the member function of the reference's class at compile time. Jika fungsi non-virtual, panggilan akan telah terikat statis fungsi anggota kelas referensi pada waktu kompilasi.

Polymorphism allows an entity (for example, variable, function or object) to take a variety of representations. Polimorfisme memungkinkan sebuah entitas (misalnya, variabel, fungsi atau objek) untuk mengambil berbagai representasi. Therefore we have to distinguish different types of polymorphism which will be outlined here. Oleh karena itu kita harus membedakan jenis polimorfisme yang akan diuraikan di sini.

The first type is similar to the concept of dynamic binding. Tipe pertama adalah serupa dengan konsep dinamis mengikat. Here, the type of a variable depends on its content. Di sini, jenis variabel tergantung pada isinya. Thus, its type depends on the content at a specific time: Dengan demikian, jenisnya tergantung pada konten pada waktu tertentu:
Code: Kode:
v := 123 /* v is integer */ v: = 123 / * v adalah integer * /
... ... /* use v as integer */ / * Menggunakan v sebagai integer * /
v := 'abc' /* v "switches" to string */ v: = 'abc' / * v "switch" untuk string * /
... ... /* use v as string */ / * Menggunakan v sebagai string * /

Another type of polymorphism can be defined for functions. Jenis lain polimorfisme dapat didefinisikan untuk fungsi-fungsi. For example, suppose you want to define a function isNull() which returns TRUE if its argument is 0 (zero) and FALSE otherwise. Misalnya, anda ingin mendefinisikan sebuah fungsi IsNull () yang mengembalikan TRUE jika argumen adalah 0 (nol) dan FALSE jika tidak.

For integer numbers this is easy: Untuk nomor integer mudah ini:
Code: Kode:
boolean isNull(int a) boolean IsNull (int a)
{ (
if (a == 0) then jika (a == 0) maka
return TRUE return TRUE
else lain
return FALSE return
endif endif
} )
However, if we want to check this for real numbers, we should use another comparison due to the precision problem: Namun, jika kita ingin memeriksa ini untuk bilangan real, kita harus menggunakan perbandingan lain karena masalah presisi:
Code: Kode:
boolean isNull(real x) { if (x < 0.01 and x > -0.99) then return TRUE else return FALSE endif } boolean IsNull (real x) (if (x <0,01 dan x> -0,99) kemudian kembali TRUE else return FALSE endif)

In both cases we want the function to have the name isNull. Dalam kedua kasus kita ingin fungsi memiliki nama IsNull. In programming languages without polymorphism for functions we cannot declare these two functions because the name isNull would be doubly defined. Dalam bahasa pemrograman tanpa polimorfisme untuk fungsi-fungsi kita tidak dapat mendeklarasikan dua fungsi ini karena nama IsNull akan didefinisikan dua kali lipat. Without polymorphism for functions, doubly defined names would be ambiguous. Tanpa polimorfisme untuk fungsi-fungsi, nama didefinisikan berlipat ganda akan ambigu. However, if the language would take the parameters of the function into account it would work. Namun, jika bahasa akan mengambil parameter dari fungsi ke dalam rekening itu akan berhasil.

Thus, functions (or methods) are uniquely identified by: Demikian, fungsi (atau metode) secara unik diidentifikasi oleh:
• the name and • nama dan
• the types of its parameter list. • jenis parameter dari daftar.

Since the parameter list of both isNull functions differ, the compiler is able to figure out the correct function call by using the actual types of the arguments: Karena daftar parameter fungsi IsNull keduanya berbeda, compiler dapat mengetahui fungsi yang benar panggilan dengan menggunakan jenis sebenarnya argumen:
Code: Kode:
var a : integer var a: integer
var x : real var x: real

a = 0 a = 0
x = 0.0 x = 0,0

... ...

if (isNull(a)) then ... if (IsNull (a)) kemudian ... /* Use isNull(int) */ / * Gunakan IsNull (int) * /
... ...
if (isNull(x)) then ... if (IsNull (x)) kemudian ... /* Use isNull(real) */ / * Gunakan IsNull (real) * /

If a function (or method) is defined by the combination of Jika suatu fungsi (atau metode) ditentukan oleh kombinasi dari
• its name and • nama dan
• the list of types of its parameters • daftar jenis parameternya

we can say polymorphism. kita dapat mengatakan polimorfisme. This type of polymorphism allows us to reuse the same name for functions (or methods) as long as the parameter list differs. Polimorfisme jenis ini memungkinkan kita untuk menggunakan kembali nama yang sama untuk fungsi-fungsi (atau metode) sepanjang daftar parameter berbeda. Sometimes this type of polymorphism is called overloading. Kadang-kadang jenis ini polimorfisme disebut overloading.

The last type of polymorphism allows an object to choose correct methods. Jenis terakhir polimorfisme memungkinkan sebuah objek untuk memilih metode yang tepat. Consider the function move() again, which takes an object of class Point as its argument. Pertimbangkan fungsi bergerak () lagi, yang mengambil kelas objek Point sebagai argumen. We have used this function with any object of derived classes, because the is-a relation holds. Kami telah menggunakan fungsi ini dengan setiap objek kelas turunan, karena adalah-relasi berlaku.

Now consider a function display() which should be used to display drawable objects. Sekarang perhatikan fungsi display () yang harus digunakan untuk menampilkan objek drawable. The declaration of this function might look like this: Deklarasi fungsi ini mungkin terlihat seperti ini:
Code: Kode:
display(DrawableObject o) display (DrawableObject o)
{ (
... ...
o.print() o.print ()
... ...
} )

We would like to use this function with objects of classes derived from DrawableObject: Kami ingin menggunakan fungsi ini dengan objek yang berasal dari kelas DrawableObject:
Circle acircle Circle acircle
Point apoint Point apoint
Rectangle arectangle Rectangle arectangle

display(apoint) /* Should invoke apoint.print() */ display (apoint) / * Jika memanggil apoint.print () * /
display(acircle) /* Should invoke acircle.print() */ display (acircle) / * Jika memanggil acircle.print () * /
display(arectangle) /* Should invoke arectangle.print() */ display (arectangle) / * Jika memanggil arectangle.print () * /

The actual method should be defined by the content of the object o of function display(). Metode yang sebenarnya harus didefinisikan oleh isi dari objek fungsi o display ().

Since this is somewhat complicated, here is a more abstract example: Karena ini agak rumit, di sini adalah contoh yang lebih abstrak:
Code: Kode:
class Base { class Base (
attributes: atribut:
methods: metode:
virtual f () virtual f ()
funy() funy ()
} )

class Derived inherits from Base { Berasal kelas mewarisi dari Base (
attributes: atribut:
methods: metode:
virtual funx () virtual funx ()
funy() funy ()
} )

demo(Base o) { demo (Base o) (
o.funx () o.funx ()
o.funy() o.funy ()
} )

Base abase Base merendahkan
Derived aderived Berasal aderived

demo(abase) demo (merendahkan)
demo(aderived) demo (aderived)

In this example we define two classes Base and Derived. Dalam contoh ini kita mendefinisikan dua kelas Base dan Derived. Each class defines two methods funx() and funy(). Setiap kelas mendefinisikan dua metode funx () dan funy (). The first method is defined as virtual. Metode pertama didefinisikan sebagai virtual. This means that if this method is invoked its definition should be evaluated by the content of the object. Ini berarti bahwa jika metode ini dipanggil definisi harus dievaluasi oleh isi dari objek.

We then define a function demo() which takes a Base object as its argument. Kami kemudian menentukan fungsi demo () yang membutuhkan objek Basis sebagai argumen. Consequently, we can use this function with objects of class Derived as the is-a relation holds. Akibatnya, kita dapat menggunakan fungsi ini dengan objek kelas Berasal sebagai adalah-relasi berlaku. We call this function with a Base object and a Derived object, respectively. Kami menyebut fungsi ini dengan sebuah objek Base dan Derived objek, masing-masing.

Suppose, that funx() and funy() are defined to just print out their name and the class in which they are defined. Misalkan, bahwa funx () dan funy () yang didefinisikan untuk mencetak nama dan kelas di mana mereka ditetapkan.

Then the output is as follows: Maka output adalah sebagai berikut:
funx() of Base called. funx () of Base menelepon.
funy() of Base called. funy () of Base menelepon.
funx() of Derived called. funx () dari Berasal disebut.
funy() of Base called. funy () of Base menelepon.

The first call to demo() uses a Base object. Panggilan pertama untuk demo () menggunakan objek Base. Thus, the function's argument is ``filled'' with an object of class Base. Dengan demikian, fungsi argumen adalah''`` diisi dengan class Base. When it is time to invoke method funx() it's actual functionality is chosen based on the current content of the corresponding object o. Ketika saatnya untuk memanggil metode funx () itu fungsi sebenarnya dipilih berdasarkan konten saat ini objek yang bersangkutan o. This time, it is a Base object. Kali ini, itu adalah sebuah objek Base. Consequently, funx() as defined in class Base is called. Akibatnya, funx () seperti yang didefinisikan di kelas Base disebut.

The call to funy() is not subject to this content resolution. Panggilan untuk funy () tidak tunduk pada resolusi konten ini. It is not marked as virtual. Hal ini tidak ditandai sebagai virtual. Consequently, funy() is called in the scope of class Base. Akibatnya, funy () dipanggil dalam lingkup kelas Base.

The second call to demo() takes a Derived object as its argument. Panggilan kedua untuk demo () memerlukan objek Berasal sebagai argumen. Thus, the argument o is filled with a Derived object. Dengan demikian, argumen o diisi dengan objek Derived. However, o itself just represents the Base part of the provided object aderived. Namun, o itu sendiri hanya merupakan bagian Base objek yang disediakan aderived.

Now, the call to funx() is evaluated by examining the content of o, hence, it is called within the scope of Derived. Sekarang, panggilan ke funx () dievaluasi dengan memeriksa isi o, maka, hal itu disebut dalam lingkup Derived. On the other hand, funy() is still evaluated within the scope of Base. Di sisi lain, funy () masih dievaluasi dalam lingkup Base.

SOURCE CODE
#include

class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
};

class CRectangle: public CPolygon {
public:
int area (void)
{ return (width * height); }
};

class CTriangle: public CPolygon {
public:
int area (void)
{ return (width * height / 2); }
};

int main () {
CRectangle rect;
CTriangle trgl;
CPolygon * ppoly1 = ▭
CPolygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << "Rectangle area = "< cout << "Triangle area = "< return 0;
}

OUTPUT


Konstruktor dan Destructors global dalam C + +

Global constructors and destructors in C++ have to be handled very carefully to meet the language specification. Constructors have to be called before the main function. Global konstruktor dan destructors di C + + harus ditangani sangat hati-hati untuk memenuhi spesifikasi bahasa. Konstruktor harus dipanggil sebelum fungsi utama. Destructors have to be executed after it returns. Under ELF, this can be treated gracefully by the compiler. Destructors harus dieksekusi setelah kembali. Di bawah ELF, hal ini dapat diobati dengan anggun oleh kompilator. For example, the GNU C/C++ compiler, gcc , provides two auxiliary start up files called crtbegin.o and crtend.o , in addition to two normal auxiliary files crti.o and crtn.o . Together with the .ctors and .dtors sections described below, the C++ global constructors and destructors can be executed in the proper order with minimal run-time overhead. Sebagai contoh, GNU C / C + + compiler, gcc, menyediakan dua bantu start up file bernama crtbegin.o dan crtend.o, selain dua file bantu normal crti.o dan crtn.o. Bersama-sama dengan. Ctors dan. Dtors bagian yang dijelaskan di bawah ini, C + + global destructors konstruktor dan dapat dilaksanakan dalam urutan yang tepat dengan sedikit run-time overhead.
.ctors . ctors

This section holds an array of the global constructor function pointers of a program. Bagian ini memegang sebuah array dari fungsi constructor global pointer dari sebuah program.
.dtors . dtors

This section holds an array of the global destructor function pointers of a program. Bagian ini memegang sebuah array dari fungsi destructor global pointer dari sebuah program.
crtbegin.o crtbegin.o

There are four sections: Ada empat bagian:
• The .ctors section. The. Ctors bagian. It has a local symbol, __CTOR_LIST__ , which is the head of the global constructor function pointer array. Memiliki simbol lokal, __CTOR_LIST__ yang adalah kepala fungsi constructor global pointer array. This array in crtbegin.o only has one dummy element. Array dalam crtbegin.o ini hanya memiliki satu elemen dummy.
• The .dtors section. The. Dtors bagian. It has a local symbol, __DTOR_LIST__ , which is the head of the global destructor function pointer array. Memiliki simbol lokal, __DTOR_LIST__ yang adalah kepala dari fungsi destructor global pointer array. This array in crtbegin.o only has only one dummy element. Array dalam crtbegin.o ini hanya memiliki hanya satu elemen dummy.
• The .text section. Yang. Teks bagian. It contains only one function, __do_global_dtors_aux , which goes through __DTOR_LIST__ from the head and calls each destructor function on the list. Hanya berisi satu fungsi, __do_global_dtors_aux yang berjalan melalui __DTOR_LIST__ dari kepala dan panggilan masing-masing fungsi destructor pada daftar.
• The .fini section. The. Fini bagian. It contains only a call to __do_global_dtors_aux . Hanya berisi ajakan untuk __do_global_dtors_aux Please remember it has just a function call without return since the .fini section in crtbegin.o is part of the body of a function. Harap diingat itu baru saja fungsi panggilan tanpa kembali sejak. Fini crtbegin.o bagian dalam adalah bagian dari tubuh fungsi.
crtend.o crtend.o

There are also four sections: Ada juga empat bagian:
• The .ctors section. The. Ctors bagian. It has a local symbol, __CTOR_END__ , which is the label for the tail of the global constructor function pointer array. Memiliki simbol lokal, __CTOR_END__ yang merupakan label untuk ekor fungsi constructor global pointer array.
• The .dtors section. The. Dtors bagian. It has a local symbol, __DTOR_END__ , which is the label for the tail of the global destructor function pointer array. Memiliki simbol lokal, __DTOR_END__ yang merupakan label untuk ekor destructor global fungsi pointer array.
• The .text section. Yang. Teks bagian. It contains only one function, __do_global_ctors_aux , which goes through __CTOR_LIST__ from the tail and calls each constructor function on the list. Hanya berisi satu fungsi, __do_global_ctors_aux yang berjalan melalui __CTOR_LIST__ dari ekor dan panggilan masing-masing pembina fungsi dalam daftar.
• The .init section. The. Init bagian. It contains only a function call to __do_global_ctors_aux . Hanya berisi fungsi panggil ke __do_global_ctors_aux Please remember it has just a function call without return since the .init section in crtend.o is part of the body of a function. Harap diingat itu baru saja fungsi panggilan tanpa kembali sejak. Init bagian dalam crtend.o adalah bagian dari tubuh fungsi.
crti.o crti.o

It has only a function label _init in the .init section and a function label _fini in the .fini section. Itu hanya memiliki fungsi label _init dalam. Init bagian dan fungsi label _fini dalam. Fini bagian.
crtn.o crtn.o

It has only a return instruction each in the .init and .fini sections. Itu hanya memiliki instruksi kembali masing-masing dalam. Init dan. Fini bagian.
At compile time while generating the relocatable files, gcc puts each global constructor on __CTOR_LIST__ by putting a pointer to the constructor function in the .ctors section. It also puts each global destructor on __DTOR_LIST__ by putting a pointer to the destructor function in the .dtors section. Pada saat waktu kompilasi relocatable menghasilkan file, gcc global yang menempatkan masing-masing konstruktor pada __CTOR_LIST__ dengan meletakkan sebuah pointer ke dalam fungsi konstruktor. Ctors bagian. Hal itu juga menempatkan masing-masing destructor global pada __DTOR_LIST__ dengan meletakkan sebuah pointer ke dalam fungsi destructor. Dtors bagian.
At link time, the gcc driver places crtbegin.o immediately before all the relocatable files and crtend.o immediately after all the relocatable files. Link pada waktu, tempat-tempat sopir gcc crtbegin.o segera sebelum semua relocatable file dan crtend.o segera setelah semua file relocatable. In addition, crti.o was placed before crtbegin.o and crtn.o was placed after crtend.o . Selain itu, crti.o ditempatkan sebelum crtbegin.o dan crtn.o ditempatkan setelah crtend.o.
While generating the executable file, the link editor, ld , concatenates the .ctors sections and the .dtors sections from all the relocatable files to form __CTOR_LIST__ and __DTOR_LIST__ , respectively. Sementara menghasilkan file eksekusi, link editor, ld, yang concatenates. Ctors bagian dan. Dtors bagian dari semua file relocatable membentuk __CTOR_LIST__ dan __DTOR_LIST__ masing-masing. The .init sections from all the relocatable files form the _init function and the .fini sections form the _fini function. The. Init bagian dari semua file relocatable membentuk _init fungsi dan. Fini membentuk bagian _fini fungsi.
At run time, the system will execute the _init function before the main function and execute the _fini function after the main function returns. Pada saat run time, sistem akan mengeksekusi _init fungsi sebelum fungsi utama dan jalankan _fini fungsi setelah fungsi utama kembali.


SOURCE CODE

#include
class CRectangle {
int width, height;
public:
CRectangle (int,int);
int area (void) {return (width*height);}
};
CRectangle::CRectangle (int a, int b) {
width = a;
height = b;
}
int main () {
CRectangle rect (3,4);
CRectangle rectb (5,6);
cout << "rect area: " << rect.area() << endl;
cout << "rectb area: " << rectb.area() << endl;
}


OUTPUT

Rabu, 09 September 2009

OOP

NPM : 0834010155

NAMA : MASYITHA

KELAS : TF C





OBJECT ORIENTED PROGRAMING (OOP)

OOP atau Object Oriented Programming, dalam Bahasa Indonesia diterjemahkan sebagai Pemrograman Berorientasi Objek. Boleh dikatakan hampir semua bahasa pemrograman modern memiliki sifat orientasi objek ini. Suatu bahasa pemrograman dikatakan murni bersifat object oriented apabila setidaknya memenuhi unsur-unsur inheritance, polymorphism, dan encapsulation. Demikian pula dengan PHP yang boleh dikatakan juga memenuhi ketiga unsur tersebut, sekalipun bisa jadi tidak semua programmer setuju. Kita tidak perlu memperdebatkan hal tersebut, yang pasti sekarang kita akan mempelajari kelas dan objek di PHP.

Kelas dan objek memang merupakan sesuatu yang agak sulit didefinisikan, tetapi untuk membantu membayangkannya dapat dipakai ilustrasi berikut:

Misalnya sebuah kelas dibayangkan sebagai sebuah perabotan, sementara itu meja, kursi, sofa, lemari dapat dibayangkan sebagai objek-objeknya. Jadi setiap dibicarakan sebuah sedan, sedan tersebut tidak dapat terlepas dari kelasnya yaitu sebuah mobil yang memiliki mesin, roda, kemudi, tempat duduk, dan lain-lain. Dari sudut pandang yang sebaliknya dapat dikatakan bahwa jika sebuah kelas mobil didefinisikan, maka dapat didefinisikan pula sebuah objek sedan atau minibus atau jeep yang memiliki seluruh sifat dari mobil.

Kelas (class)
Kelas adalah sebuah kumpulan variabel dan fungsi-fungsi yang bekerja dengan variabel tersebut. Untuk mendefinisikan kelas, sintaks yang digunakan adalah sebagai berikut:

class namakelas {
var $namavariabel;
...Kumpulan fungsi...
}

Sebagai batasan, namakelas tidak boleh menggunakan nama stdClass karena telah digunakan oleh Zend Engine, yaitu "mesin" yang merupakan motor penggerak PHP.

Di dalam kelas, variabel didefinisikan dengan pernyataan var. Di dalam sebuah kelas secara default dikenal sebuah variabel bernama $this yang mereferensikan kelas itu sendiri. Kemudian untuk merujuk kepada variabel yang didefinisikan dengan pernyataan var digunakan tanda ->.

Untuk lebih jelasnya berikut contoh dari sebuah kelas yang berisi fungsi-fungsi perhitungan sederhana:

hasil = $x * $y;
}

function bagi($x,$y) {
$this->hasil = $x / $y;
}

function tambah($x,$y) {
$this->hasil = $x + $y;
}

function kurang($x,$y) {
$this->hasil = $x - $y;
}
}
?>

Sebuah kelas dapat merupakan perluasan dari kelas yang ada sebelumnya. Misalnya pada contoh kelas di atas fungsi-fungsi yang ada hanyalah kali(), bagi(), tambah(), kurang() dan mungkin itu dirasakan kurang. Jika ingin didefinisikan sebuah kelas lain yang juga mengandung fungsi-fungsi kali(), bagi(), tambah(), dan kurang() serta ditambah sebuah fungsi lagi misalnya pangkat(), maka tidak perlu dibuat sebuah kelas yang baru sama sekali yang mengandung kelima fungsi tersebut. Kelas tersebut dapat merupakan perluasan dari kelas hitung dan hanya mendefinisikan sebuah fungsi baru yaitu pangkat(). Dalam PHP untuk melakukan perluasan terhadap sebuah kelas digunakan pernyataan extends.

class namakelas extends kelaslain {
var $namavariabel;
...Kumpulan fungsi lain...
}


Contoh:

hasil = pow($x,$y)
}
}
?>

Dari contoh ini dapat disimpulkan bahwa kelas hitung_juga akan memiliki variabel dan fungsi-fungsi yang terdapat pada kelas hitung ditambah dengan sebuah fungsi baru yaitu pangkat().

Perluasan kelas semacam ini dapat dikategorikan sebagai sifat inheritance dari pemrograman berorientasi objek. Kelas hitung_juga pada contoh di atas merupakan inherit dari kelas hitung.

Dalam melakukan perluasan ini, kelas yang merupakan inherit dari kelas lain dapat mendefinisikan fungsi dengan nama yang sama. Contoh:

class A {
function contoh() {
echo "Aku adalah fungsi contoh yang asli";
}
}

class B extends A {
function contoh() {
echo "Aku adalah fungsi contoh yang didefinisikan ulang";
}
}

Dalam hal ini fungsi contoh() di kelas A disebut dengan shadowed dan tidak dapat dipergunakan lagi. Unsur polymorphism terpenuhi di sini.

Objek (object)
Menurut ilustrasi perabotan di atas dapat dikatakan bahwa objek "diperanakan" dari kelas. Untuk mendefinisikan sebuah objek digunakan sintaks sebagai berikut:

$namaobjek = new namakelas;

Setelah objek didefinisikan, maka seluruh variabel dan fungsi yang terdapat dalam sebuah kelas namakelas otomatis terdapat pula dalam objek namaobjek tersebut. Untuk merujuk pada fungsi atau variabel yang dimiliki oleh kelasnya, maka digunakan juga tanda ->. Berikut akan diberikan contoh bagaimana menggunakan objek dan kelas dengan menggunakan contoh kelas hitung dan hitung_juga yang telah diberikan di atas.



Object Oriented Programming (bagian 1)

hasil = $x * $y;
}

function bagi($x,$y) {
$this->hasil = $x / $y;
}

function tambah($x,$y) {
$this->hasil = $x + $y;
}

function kurang($x,$y) {
$this->hasil = $x - $y;
}
}
$a = 5;
$b = 2;
echo "Nilai \$a = $a
";
echo "Nilai \$b = $b
";
$calc = new hitung;

echo "\$a kali \$b sama dengan ";
$calc->kali($a,$b);
print $calc->hasil;
echo "
";

echo "\$a bagi \$b sama dengan ";
$calc->bagi($a,$b);
print $calc->hasil;
echo "
";

echo "\$a tambah \$b sama dengan ";
$calc->tambah($a,$b);
print $calc->hasil;
echo "
";

echo "\$a kurang \$b sama dengan ";
$calc->kurang($a,$b);
print $calc->hasil;
echo "

";

class hitung_juga extends hitung {
function pangkat($x,$y) {
$this->hasil = pow($x,$y);
}
}

$calc_juga = new hitung_juga;

echo "\$a kali \$b sama dengan ";
$calc_juga->kali($a,$b);
print $calc_juga->hasil;
echo "
";

echo "\$a bagi \$b sama dengan ";
$calc_juga->bagi($a,$b);
print $calc_juga->hasil;
echo "
";

echo "\$a tambah \$b sama dengan ";
$calc_juga->tambah($a,$b);
print $calc_juga->hasil;
echo "
";

echo "\$a kurang \$b sama dengan ";
$calc_juga->kurang($a,$b);
print $calc_juga->hasil;
echo "
";

echo "\$a pangkat \$b sama dengan ";
$calc_juga->pangkat($a,$b);
print $calc_juga->hasil;
echo "
";
?>


Demikian pembahasan OOP di PHP (bagian 1), artikel selanjutnya kita akan belajar lebih jauh lagi mengenai OOP, selamat belajar & berlatih.