Encapsulation adalah sebuah paradigma dalam bidang pemrograman di mana data member atau properties dan member functions atau methods dikemas dalam sebuah kemasan yang dinamai objek.
Dalam bahasa pemrograman C++, objek memiliki abstraksi yang bernama class. Di dalam class, kita dapat membangun "blue print" dari sebuah objek. Pada C++ kita tidak hanya dapat menggunakan class untuk membuat abstraksi dari objek, tetapi kita juga dapat menggunakan struct, mengingat C++ merupakan superset dari C.
Pada penggunaannya antara class dan struct hanya berbeda pada default member accessnya, pada struct semua member secara default akan bersifat public, sedangkan pada class secara default member akan bersifat private. Contoh :
class tes{
int angka; // access private
char huruf; // access private
void get_angka(); // access private
public :
int angka2; // access public
char huruf2; // access public
void get_huruf(); // access public
};
struct tes{
int angka; // access public
char huruf; // access public
void get_angka(); // access public
private :
int angka2; // access private
char huruf2; // access private
void get_huruf(); // access private
};
Berbicara tentang public dan private, kita berbicara tentang access spesifier. Access spesifier adalah cara member sebuah class atau struct diakses. Pada bahasa C++ sebenarnya ada 3 jenis access spesifier, yaitu public, private, dan protected, tetapi pada pembahasan ini hanya akan dibahas public dan private, sedangkan protected akan dibahas pada pembahasan berikutnya.
Access spesifier public berarti semua member di bawah tanda tersebut akan dapat diakses dari luar class, sedangkan private hanya dapat diakses oleh method pada class itu sendiri, misalnya :
class tes{
int angka; // access private
char huruf; // access private
void get_angka(); // access private
public :
int angka2; // access public
char huruf2; // access public
void get_huruf(); // access public
};
int main()
{
tes objek1;
objek1.angka2 = 1; //diperbolehkan karena access public
objek1.get_huruf() // diperbolehkan karena access public
objek1.angka = 1; //compile-error karena access private
objek1.get_angka() // compile-error karena access private
return 0;
}
Kamis, 29 Oktober 2009
Jumat, 16 Oktober 2009
Iostream library adalah object-oriented library yang menyediakan fungsi-fungsi input/output menggunakan stream.
Stream adalah sebuah abstraksi yang dapat diartikan sebuah device yang melakukan proses input/output. Stream juga dapat diartikan sebagai sumber atau tujuan dari karakter yang tidak terbatas.
Stream secara umum dapat diasosiasikan ke sumber atau tujuan dari karakter-karakter secara fisik, seperti disk file atau keyboard.
Elemen-elemen dari library iostream adalah :
Classes:
Objects:
Types:
Manipulators:
Stream adalah sebuah abstraksi yang dapat diartikan sebuah device yang melakukan proses input/output. Stream juga dapat diartikan sebagai sumber atau tujuan dari karakter yang tidak terbatas.
Stream secara umum dapat diasosiasikan ke sumber atau tujuan dari karakter-karakter secara fisik, seperti disk file atau keyboard.
Elemen-elemen dari library iostream adalah :
Classes:
| ios_base | Base class with type-independent members for the standard stream classes (class) |
| ios | Base class with type-dependent members for the standard stream classes (class) |
| istream | Input stream (class) |
| ostream | Output Stream (class) |
| iostream | Input/Output Stream (class) |
| ifstream | Input file stream class (class) |
| ofstream | Output file stream (class) |
| fstream | Input/output file stream class (class) |
| istringstream | Input string stream class (class) |
| ostringstream | Output string stream class (class) |
| stringstream | Input/output string stream class (class) |
| streambuf | Base buffer class for streams (class) |
| filebuf | File stream buffer (class) |
| stringbuf | String stream buffer (class) |
Objects:
| cin | Standard input stream (object) |
| cout | Standard output stream (object) |
| cerr | Standard output stream for errors (object) |
| clog | Standard output stream for logging (object) |
Types:
| fpos | Stream position class template (class template) |
| streamoff | Stream offset type (type) |
| streampos | Stream position type (type) |
| streamsize | Stream size type (types) |
Manipulators:
| boolalpha | Alphanumerical bool values (manipulator function) |
| dec | Use decimal base (manipulator function) |
| endl | Insert newline and flush (manipulator function) |
| ends | Insert null character (manipulator function) |
| fixed | Use fixed-point notation (manipulator function) |
| flush | Flush stream buffer (manipulator function) |
| hex | Use hexadecimal base (manipulator function) |
| internal | Adjust field by inserting characters at an internal position (manipulator function) |
| left | Adjust output to the left (manipulator function) |
| noboolalpha | No alphanumerical bool values (manipulator function) |
| noshowbase | Do not show numerical base prefixes (manipulator function) |
| noshowpoint | Do not show decimal point (manipulator function) |
| noshowpos | Do not show positive signs (manipulator function) |
| noskipws | Do not skip whitespaces (manipulator function) |
| nounitbuf | Do not force flushes after insertions (manipulator function) |
| nouppercase | Do not generate upper case letters (manipulator function) |
| oct | Use octal base (manipulator function) |
| resetiosflags | Reset format flags (manipulator function) |
| right | Adjust output to the right (manipulator function) |
| scientific | Use scientific notation (manipulator function) |
| setbase | Set basefield flag (manipulator function) |
| setfill | Set fill character (manipulator function) |
| setiosflags | Set format flags (manipulator function) |
| setprecision | Set decimal precision (manipulator function) |
| setw | Set field width (manipulator function) |
| showbase | Show numerical base prefixes (manipulator function) |
| showpoint | Show decimal point (manipulator function) |
| showpos | Show positive signs (manipulator function) |
| skipws | Skip whitespaces (manipulator function) |
| unitbuf | Flush buffer after insertions (manipulator function) |
| uppercase | Generate upper-case letters (manipulator function) |
| ws | Extract whitespaces (manipulator function) |
Jumat, 09 Oktober 2009
Function Overloading
Function Overloading adalah fitur dari C++ yang memungkinkan Anda untuk membuat fungsi dengan nama yang sama tetapi dengan return type dan parameter yang berbeda, dengan demikian dapat memudahkan Anda untuk menggunakan fungsi dengan tugas yang sama tetapi dengan return type maupun parameter yang berbeda.
Contoh :
________________________________________________________
#include
using namespace std;
/*Di bawah ini adalah prototype fungsi dengan
nama yang sama tetapi dengan
tipe data pada parameter yang berbeda.*/
int test(int i);
double test(double i);
int main()
{
cout << test(10) << " "; // memanggil test(int i)
cout << test(5.4); // memanggil test(double i)
getchar();
return 0;
}
double test(double i)
{
return i;
}
int test(int i)
{
return i;
}
_________________________________________________________
OUTPUT :
10 5.4
_________________________________________________________
#include
using namespace std;
/* Di bawah ini adalah prototype fungsi dengan nama,
tipe data pada parameter yang sama tetapi dengan
jumlah parameter yang berbeda*/
int test(int i);
int test(int i, int j);
int main()
{
cout << test(10) << " "; // memanggil test(int i)
cout << test(4, 5); // memanggil test(int i, int j)
getchar();
return 0;
}
int test(int i)
{
return i;
}
int test(int i, int j)
{
return i*j;
}
___________________________________________________
OUTPUT :
10 20
___________________________________________________
Contoh :
________________________________________________________
#include
using namespace std;
/*Di bawah ini adalah prototype fungsi dengan
nama yang sama tetapi dengan
tipe data pada parameter yang berbeda.*/
int test(int i);
double test(double i);
int main()
{
cout << test(10) << " "; // memanggil test(int i)
cout << test(5.4); // memanggil test(double i)
getchar();
return 0;
}
double test(double i)
{
return i;
}
int test(int i)
{
return i;
}
_________________________________________________________
OUTPUT :
10 5.4
_________________________________________________________
#include
using namespace std;
/* Di bawah ini adalah prototype fungsi dengan nama,
tipe data pada parameter yang sama tetapi dengan
jumlah parameter yang berbeda*/
int test(int i);
int test(int i, int j);
int main()
{
cout << test(10) << " "; // memanggil test(int i)
cout << test(4, 5); // memanggil test(int i, int j)
getchar();
return 0;
}
int test(int i)
{
return i;
}
int test(int i, int j)
{
return i*j;
}
___________________________________________________
OUTPUT :
10 20
___________________________________________________
Langganan:
Postingan (Atom)