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


Tidak ada komentar:

Posting Komentar