Penentu akses private biasa digunakan pada kelas untukmemproteksi anggota - anggota tertentu pada kelas , agar tidak dapat di akses di luar kelas secara langsung. Public menyatakan bahwa anggota kelas tersebut dapat diakses di luar kelas ( bersifat public).
Berikut contoh script aplikasi sederhana dengan menggunakan class :
--------------------------------------------------------------------------------------------------------------
#include
#include
#include
#include
class Gaji
{
private :
float u;
float j;
float h;
public :
char nik[10];
char n[30];
float g (float x, float y, float z){
return (x*y-z);
}
void gaji_pegawai()
{
cout<<"MASUKKAN DATA YANG SESUAI \n";
cout<<"\n";
cout<<"NIK (Max. 10 char) : "; gets(nik);
cout<<"\n";
cout<<"Nama (max . 30 char): "; gets(n);
cout<<"\n";
cout<<"Jumlah Jam Kerja : ";
cin>>j;
cout<<"\n";
cout<<"Upah per Jam : Rp";
cin>>u;
cout<<"\n";
cout<<"Hutang : Rp";
cin>>h;
cout<<"\n";
cout<<"\t===================================================\n";
cout<<"\t NIK \t\t"<<"Nama \t\t"<<"Gaji Bersih \t\t""\n";
cout<<"\t---------------------------------------------------\n";
cout<<"\t "<
getch(); }
};
void main()
{
Gaji Pegawai;
int c;
clrscr();
do{
clrscr ();
cout<< "************************************************************************\n";
cout<< "* CONTOH PENGGUNAAN CLASS *\n";
cout<< "* *\n";
cout<< "************************************************************************\n";
cout<<"\n";
cout<<"\n";
cout<<"***********************MENU PILIHAN ANDA***************************** \n";
cout<<" 1. Menghitung Gaji Anda\n";
cout<<" 2. Keluar\n";
cout <<"\n";
cout <<"\n";
cout<<" PILIHAN ANDA : \n";
cin>>c;
if ((c<1) || (c>2)){
cout<< "PILIHAN ANDA SALAH... COBA LAGI :";
getch();
cout<< "\n";
}
clrscr();
switch (c)
{
case 1 :Pegawai.gaji_pegawai() ;
break;
case 2 :exit (0);
default : break;
}
}
while (c!=0);}
-----------------------------------------------------------------------------------------------------------------
Semoga bermanfaat :-)
No comments:
Post a Comment