BAGIAN 7.2.2 Mengakses Anggota
.................................
// Struktur.cpp
#include
#include
int main()
{
// definisi struktur
struct Pegawai
{
int nip;
char nama[35];
bool pria;
};
// deklarasi variabel struktur
Pegawai dataPeg;
// Mengisi anggota
dataPeg.nip = 12345;
strcpy(dataPeg.nama, "Asti Damayanti");
dataPeg.pria = false;
// menampilkan isi struktur
cout << "NIP: " << dataPeg.nip << endl;
cout << "Nama: " << dataPeg.nama << endl;
cout << "Jenis kelamin: "
<< (dataPeg.pria ? "Pria" : "Wanita")
<< endl;
return 0;
}
----------------------------------------------------------------
BAGIAN 7.2.3 Struktur dan Pointer
.................................
// DasarSenarai.cpp
#include
#include
int main()
{
// definisi struktur
struct Simpul
{
int nip;
char nama[35];
bool pria;
Simpul* berikut;
};
// deklarasi variabel struktur
Simpul* kepala = NULL;
Simpul* tmp = NULL;
// Mengisi simpul pertama
kepala = new Simpul;
(*kepala).nip = 12345;
strcpy((*kepala).nama, "Asti Damayanti");
(*kepala).pria = false;
(*kepala).berikut = NULL;
// Mengisi simpul kedua
tmp = new Simpul;
(*tmp).nip = 12346;
strcpy((*tmp).nama, "Edi Danhuri");
(*tmp).pria = true;
(*tmp).berikut = kepala;
kepala = tmp;
// Mengisi simpul ketiga
tmp = new Simpul;
(*tmp).nip = 12347;
strcpy((*tmp).nama, "Sita Dewi");
(*tmp).pria = false;
(*tmp).berikut = kepala;
kepala = tmp;
// Menampilkan isi senarai
tmp = kepala;
while (tmp)
{
cout << endl
<< "Data Pegawai: " << endl;
cout << "NIP: " << (*tmp).nip << endl;
cout << "Nama: " << (*tmp).nama << endl;
cout << "Jenis kelamin: "
<< ((*tmp).pria ? "Pria" : "Wanita")
<< endl;
tmp = (*tmp).berikut;
}
return 0;
}
----------------------------------------------------------------
SUBBAB 7.3 Union
.................................
// Union.cpp
#include
int main()
{
union BilBulat
{
unsigned int bInt;
unsigned char cKar[2];
};
BilBulat suatuBilangan;
suatuBilangan.bInt = 0x5678;
cout << "bInt = "
<< hex << suatuBilangan.bInt
<< endl;
cout << "cKar[0] = "
<< hex << int(suatuBilangan.cKar[0])
<< endl;
cout << "cKar[1] = "
<< hex << int(suatuBilangan.cKar[1])
<< endl;
return 0;
}
----------------------------------------------------------------
SUBBAB 7.4 Bit-field
.................................
// InfoBit.cpp
#include
int main()
{
struct InfoBit
{
unsigned int bit0: 1;
unsigned int bit1: 1;
unsigned int bit2: 1;
unsigned int bit3: 1;
unsigned int bit4: 1;
unsigned int bit5: 1;
unsigned int bit6: 1;
unsigned int bit7: 1;
};
union UByte
{
unsigned char byte;
InfoBit bit;
};
UByte bilangan;
int nilai;
cout << "Masukkan sebuah byte (0-255): ";
cin >> nilai;
bilangan.byte = nilai;
cout << "76543210 <-- data-blogger-escaped-bit="" data-blogger-escaped-br="" data-blogger-escaped-endl=""> cout << bilangan.bit.bit7
<< bilangan.bit.bit6
<< bilangan.bit.bit5
<< bilangan.bit.bit4
<< bilangan.bit.bit3
<< bilangan.bit.bit2
<< bilangan.bit.bit1
<< bilangan.bit.bit0
<< endl;
return 0;
}
----------------------------------------------------------------
SUBBAB 7.5 Enum
.................................
// Enumerasi.cpp
#include
int main()
{
enum Warna { Merah, Kuning, Hijau };
Warna keadaanLampu;
// Memberi nilai
keadaanLampu = Merah;
// Menampilkan isi keadaanLampu
switch (keadaanLampu)
{
case Merah:
cout << "Warna merah" << endl;
break;
case Kuning:
cout << "Warna kuning" << endl;
break;
case Hijau:
cout << "Warna hijau" << endl;
break;
}
return 0;
}
----------------------------------------------------------------
SUBBAB 7.6 Typedef
.................................
// Typedef.cpp
#include
int main()
{
typedef unsigned char Byte;
Byte kode;
kode = 65;
cout << kode
<< endl;
return 0;
}
----------------------------------------------------------------
0 comments:
Post a Comment