Review :: Kisi-kisi UTS :: Pertemuan 5 Review :: Kisi-kisi UTS ::
BASIC CONCEPTS OF OOP OBJECTS CLASSES DATA ABSTRACTION ENCAPSULATION INHERITANCE POLYMORPHISM DYNAMIC BINDING MESSAGE PASSING
CLASSES Is the user defined data types A collection of objects of similar type Example: Object mango, apple, and orange is member of class fruit Object TF, TI, TK, TP is member of class Jurusan Object jip, sedan, bus, truk is member of class mobil
OBJECTS Is the variables of program Have a life cycle. They can be created and destroyed. Interacts by sending message to one another Contains data and code/ function to manipulate the data
CLASSES & OBJECTS SPECIFYING A CLASS class class_name { private: variable_declarations; function_declarations; public: } Keywords private and public known as visibility labels. private can be accessed only from within the class. Data member/ Variable is commonly private. public can be accessed from outside the class. Member function is commonly public.
Class Data/ Variabel Private Area No Entry ! Function/ Method Public Area Data/ Variabel Function/ Method Entry Allowed
Konversi Prosedural ke OOP Pastikan program sudah terbagi menjadi fungsi-fungsi/ modular. Identifikasi class yang bisa dibuat. Identifikasi data member dan member function untuk class yang bersangkutan. Tentukan bagian private dan public class (sesuaikan dengan skenario fungsi utama). Buat fungsi utamanya (void main()). Membuat obyek dari class. Menjalankan bagian public dari class.
//PROCEDURAL PROGRAMMING #include “stdio.h” void main() #include “iostream.h” class balok { private : int p, l, t, volume; public : void input(void) { printf(“Masukkan P = “); scanf(“%d”, &p); printf(“Masukkan L = “); scanf(“%d”, &l); printf(“Masukkan T = “); scanf(“%d”, &t); }; void hitung(void) { volume = p * l * t; }; void output(void) { printf(“Volume = %d\n”, volume); }; void main() { balok balokku; printf(“Program Balok\n”); balokku.input(); balokku.hitung(); balokku.output(); } //PROCEDURAL PROGRAMMING #include “stdio.h” void main() { int p, l, t, volume; printf(“Program Balok\n”); printf(“Masukkan P = “); scanf(“%d”, &p); printf(“Masukkan L = “); scanf(“%d”, &l); printf(“Masukkan T = “); scanf(“%d”, &t); volume = p * l * t; printf(“Volume = %d\n”, volume); }
Constructors and Destructors Parameterized Constructors Multiple Constructors Constructors with Default Arguments Copy Constructors Destructors
Simple Constructor & destructor #include<iostream.h> class coba { int kode; public : coba() { cout << "Kode = "<< kode << endl; cout << "konstruktor dijalankan...\n"; kode = 1; }; ~coba() { cout << "destruktor dijalankan...\n"; cout << "Kode = "<< kode << endl; void show() void main() { coba cobaku; cobaku.show(); }