Kamis, 29 Oktober 2009
Encapsulation
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;
}
Jumat, 16 Oktober 2009
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
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
___________________________________________________
Rabu, 16 September 2009
Kelebihan Dasar C++ dibanding C
test data[10];
(membuat objek bernama data dari Class test)
Sehingga mempercepat pembuatan program Anda.
Bayangkan saja apabila kode program seperti di atas diterapkan pada Bahasa C, maka Anda harus menuliskan 10 fungsi untuk dengan nama yang berbeda untuk dipanggil. Hal ini lah yang menjadi inti mengapa C++ sebagai Object-Oriented Programming Language menjadi superset dari Bahasa C.
C++
C++ adalah bahasa pemrograman yang berbentuk bebas, multi-paradigma, dan merupakan general-purpose programming language sehingga dapat digunakan untuk berbagai target aplikasi, seperti systems software, application software, device drivers, embedded software, high-performance server dan client applications, dan entertainment software. C++ merupakan middle-level language karena meliputi kombinasi antara fitur-fitur high-level language dan low-level language. C++ dikembangkan oleh Bjarne Stroustrup pada tahun 1979 di Bell Labs sebagai peningkatan dari bahasa pemrograman C. Pada awalnya C++ dinamai "C with Classes" karena memang pada dasarnya C++ merupakan bahasa C yang ditambah dengan fitur membuat Class, lalu akhirnya pada tahun 1983, namanya baru diubah menjadi C++.
C++ dipengaruhi oleh beberapa bahasa pemrograman seperti C, Simula, Ada 83, ALGOL 68, CLU, dan ML. C++ juga mempengaruhi beberapa bahasa pemrograman seperti Java, PHP, C#, dan Perl.
C++ memulai penambahan fitur dari bahasa C dengan Class, lalu dilanjutkan dengan virtual functions, operator overloading, multiple inheritance, templates, dan exception handling.
Kompatibilitas C++ dengan C hampir dapat dikatakan kompatibel, dalam arti hampir semua kode program yang ditulis dalam bahasa C dapat dijalankan dengan compiler C++ karena memang C++ merupakan superset dari bahasa C tetapi ada beberapa fitur dalam bahasa C yang tidak kompatibel dengan C++, seperti konversi void* ke tipe pointer lain, dan yang sudah pasti adalah C++ memiliki keyword lebih banyak dari pada C, sehingga ada kemungkinan identifier yang mungkin ada dalam kode bahasa C merupakan keyword pada C++, sehingga akan error jika di-compile.
Beberapa produk compiler C++ adalah GNU(C++), Borland C++, Turbo C++, Dev C++, Microsoft Visual Studio, dan lainnya.