Professional Documents
Culture Documents
Pertemuan 10
Pemrograman Berorientasi Objek
1
Learning Outcomes
• Konsep Encapsulation
• Struct vs Class
• Class Access Control
• Constructor & Destructor
• Object Class
• Access with scope resolution operator
(::)
2
Konsep Encapsulation
3
Struct VS Class
Default
Default
Access
Access
member:
member:
public
private
• Public:
– Bagian class yang dapat diakses oleh umum
baik dari dalam maupun dari luar class
• Protected:
– Bagian class yang dapat diakses oleh
linkungan internal class dan class turunannya
• Private:
– Bagian class yang hanya dapat diakses oleh
linkungan internal class itu sendiri
5
Constructor & Destructor
• class Time
• { private: int hour;
• int minute;
Constructor
• int second;
• public: Time();
• void setTime( int, int, int );
• void printUniversal();
• void printStandard();
• ~Time();
• };
Destructor 6
Constructor & Destructor
9
Access with scope resolution
operator (::)
ClassName :: classMemberName
void Time :: setTime( int h, int m, int s )
class Time { hour = ( h >= 0 && h < 24 ) ? h : 0;
{ private: minute = ( m >= 0 && m < 60 ) ? m : 0;
int hour; second = ( s >= 0 && s < 60 ) ? s : 0;
int minute; }
int second; void Time :: printUniversal()
public: { cout << setfill( '0' ) << setw( 2 ) << hour << ":"
<< setw( 2 ) << minute << ":"
Time()
<< setw( 2 ) << second;
{ hour = minute = second = 0; }
} void Time :: printStandard()
void setTime( int, int, int ); { cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour
void printUniversal(); % 12 )
void printStandard(); << ":" << setfill( '0' ) << setw( 2 ) << minute
~Time(){ } << ":" << setw( 2 ) << second
}; << ( hour < 12 ? " AM" : " PM" );
}
10
Topik Minggu Depan
• Inheritance (Turunan)
• Tugas: Mahasiswa membuat program
sederhana dengan mengggunakan
konsep Encapsulation: Sistem Elevator
Sederhana
• Tugas(optional): Studi Kasus-
Intercomputer Communications (Ref:
buku-C is for control)
11