struct buku
{
char judul[20];
char pengarang[20];
int jumlah;
} ;
class Buku
{
public :
char judul[20];
char pengarang[20];
int jumlah;
} ;
#include
#include
struct buku {
char judul[20];
char pengarang[20];
int jumlah;
} ;
int main()
{
buku novel ; // pendefinisian var struktur
strcpy(novel.judul, "Ramayana");
strcpy(novel.pengarang, "Narayan");
novel.jumlah = 12;
cout << novel.judul << endl; cout << novel.pengarang << endl; cout << novel.jumlah << endl; return 0; } #include
#include
class Buku {
public :
char judul[20];
char pengarang[20];
int jumlah;
} ;
int main() {
Buku novel ; // pendefinisian var struktur
strcpy(novel.judul, "Ramayana");
strcpy(novel.pengarang, "Narayan");
novel.jumlah = 12;
cout << novel.judul << endl; cout << novel.pengarang << endl; cout << novel.jumlah << endl; return 0; } #include
#include
class Buku{
private :
char judul[20];
char pengarang[20];
int jumlah;
public :
void inisialisasi(char *Judul, char *Pengarang, int Jumlah)
{
strcpy(judul, Judul);
strcpy(pengarang, Pengarang);
jumlah = Jumlah;
}
void info()
{
cout << "Judul : " << judul << endl; cout << "Pengarang : " << pengarang << endl; cout << "Jumlah : " << jumlah << endl; } } ; // end class int main() { Buku novel ; // pendefinisian var struktur (object) novel.inisialisasi("Ramayana", "Narayan", 12); novel.info(); return 0; }
Class
Penggunaan private digunakan pada kelas untuk memproteksi anggota-anggota tertentu pada kelas, agar tidak dapat diakses di luar kelas secara langsungContoh perhitungan luas segi empat
class area {
private:
float a, b, c;
public:
float x, y ;
area (float x, float y) //konstruktor
{a = x;
b = y;
cout << "harga a : " << a << endl; cout << "harga b : " << b << endl << endl; } ; // fungsi utk cetak hrg parameter ~area() //destruktor { cout << "Stop " << endl; }; void hasil () { c = a * b; cout << "Hasil : " << c << endl; } }; int main() { area luas(2, 4.5); luas.hasil(); return 0; }

No comments:
Post a Comment