1 / 17

Pertemuan 6 Encapsulation Lanjutan

Pertemuan 6 Encapsulation Lanjutan. Matakuliah : T0044/Pemrograman Berorientasi Obyek Tahun : 2005 Versi : 1.0. Learning Outcomes. Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Menghasilkan Program C++ dengan konsep Encapsulation. Outline Materi.

casta
Download Presentation

Pertemuan 6 Encapsulation Lanjutan

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pertemuan 6Encapsulation Lanjutan Matakuliah : T0044/Pemrograman Berorientasi Obyek Tahun : 2005 Versi : 1.0

  2. Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : • Menghasilkan Program C++ dengan konsep Encapsulation

  3. Outline Materi • Object constant dan Member Function constant • Sintaks Initializer Member Class • Nested Object • Pointer this • Static Class Member • Friend Function • Operator Overloading

  4. Object Constant • Untuk object yang tidak dapat di-modify • Jika di-modify akan menyebabkan Compiler error Contoh object konstant: const Time noon( 12, 0, 0 ); • deklarasi konstanta object noon dari class Time yg di-inisial 12

  5. Member Functions Constant • Member function utk const objects juga harus dibuat const • Tidak bisa me-modify object • const pada prototype dan definisinya: int getHour() const; // Prototype int Time::getHour() const // Definisi { return hour; } • Constructor dan Destructor tidak boleh di buat const Karena harus dapat me-modify object • Constructor  utk initialsasi objects • Destructor  utk termination object

  6. Sintaks Initializer Member Class • Initialisasi dg sintaks initializer member dapat digunakan untuk semua const data member. #include <iostream.h> class Increment { public: Increment (int c = 0, int i = 1 ); // default constructor void addIncrement ( ) { count += increment; } void print ( ) const; // const member function { cout << "count = " << count << ", increment = " << increment << endl; } private: int count; const int increment; // const data member };

  7. Sintaks Initializer Member Class // definisi constructor Increment::Increment (int c, int i ) : count( c ), // initializer non-const member increment( i ) // initializer const member { } //empty body void main() { Increment value( 10, 5 ); cout << "Before incrementing: "; value.print(); for ( int j = 0; j < 3; j++ ) { value.addIncrement(); cout << "After increment " << j + 1 << ": "; value.print(); } } Before incrementing: count = 10, increment = 5 After increment 1: count = 15, increment = 5 After increment 2: count = 20, increment = 5 After increment 3: count = 25, increment = 5

  8. Nested Object • Sebuah Class dapat berisi object dari class lain  Composite / Nested Object • Member object disusun berurutan • class dg member objects (host objects) berada setelah deklarasi class host object tsb  perlu class prototype • Contoh: class B; // class prototype class A { public: int i; } class B { public: int j; A obj_A; //host object } B obj_B; // create object class B Obj_B.Obj_A.i = 0; // inisialisasi member class A via object B Class B Berisi Class A

  9. Pointer “this” • Tiap class member function memiliki hidden parameter -> pointer "this“ • "this" menunjuk ke individual object. • pointer "this" dpt digunakan secara explicit

  10. Static Class Member • static Data Member • “Class-wide” data • property class tdk specific pada object tertentu • Seperti global variables, tapi punya class scope • Hanya accessible ke object dgn class yg sama • Efisien untuk single copy data • Dimana hanya static variable yg akan di-update • Initialized hanya 1 kali pada file scope • Berlaku walaupun suatu class tidak punya object • Berlaku independent terhadap objects • Berlaku utk public, private & protected

  11. Static Class Member • Dpt di akses lewat object apapun dari sebuah class • Utk public static variables • Dapat diakses dgn operator binary scope resolution (::) Employee::count • Utk private static variables • Jika suatu class tdk ada object  Hanya dapat diakses via public static member function dgn operator binary scope resolution (::) Employee::getCount() • static member functions Tdk dpt mengakses non-static data / function

  12. Friend Function • friend function di definisikan di luar dari class’s scope namum memiliki hak akses ke semua members dari class tersebut • Deklarasi friend funcion dg keyword ‘friend’ • Contoh: friend void setX( Count &, int );

  13. #include <iostream.h> class Count { friend void setX( Count &, int ); public: Count ( ) : x ( 0 ) { // empty body } void print() const { cout << x << endl; } private: int x; }; void setX( Count &c, int val ) { c.x = val; // legal: setX is a friend of Count } void main() { Count counter; cout << "counter.x after instantiation: "; counter.print(); setX( counter, 8 ); cout << "counter.x after call to setX friend function: "; counter.print(); } Contoh Program dg Friend Function

  14. Operator Overloading • Pemakaian Overloading operator ditujukan untuk mengoperasikan data pada object menjadi mudah • Menggunakan keyword operator

  15. Class Buah { int apel; int jeruk; public: Buah Buah(int A, int j) { apel = A; jeruk = j; } Buah operator + (Buah b) { Buah temp; temp.apel = apel + b.apel; temp.jeruk = jeruk + b.jeruk; return (temp) } Buah operator - (Buah b) }; Buah Buah::operator - (Buah b) { Buah temp; temp.apel = apel - b.apel; temp.jeruk = jeruk - b.jeruk; return (temp) } void main() { Buah B1 (2, 3) , B2 (7, 5), B3 ; cout << B1.apel << “ dan ” << B1.jeruk << “ + “ << B2.apel << “ dan “ << B2.jeruk << “ = “; B3 = B1 + B2 ; cout << B3.apel << “ dan ” << B3.jeruk; } Overloading Arithmatic Operator

  16. Operator yg tdk dpt di-overload

  17. Tugas 6 • Tugas 6A: Mahasiswa membuat ringkasan tentang friend function dan operator overloading • Tugas 6B: Mahasiswa menganalisis contoh program yang diberikan oleh dosen • Tugas 6C: Mahasiswa membuat contoh program dengan menggunakan friend function dan operator overloading

More Related