Pemrograman Object dengan C++


Berikut ini adalah salah satu contoh pemrograman objeck dalam bahasa C / C++ :

#include "conio.h"
#include "stdio.h"
#include "string.h"

class ClassSiswa{

public :

typedef struct{
int kode;
float nilai;
int SKS;
}mkul;

typedef struct{
char NPM[8];
char nama[15];
char alamat[20];
mkul matakuliah[5];
}Siswa;

int n;
Siswa S;

Siswa BuatDataSiswa(char *NPM,char *nama,char *alamat)
{
strcpy(S.NPM,NPM);
strcpy(S.nama,nama);
strcpy(S.alamat,alamat);
return S;
};

void CetakData()
{
printf("\tNP Mahasiswa : %s\n",S.NPM);
printf("\tNama Mahasiswa : %s\n",S.nama);
printf("\tAlamat Mahasiswa : %s\n",S.alamat);
return;
};

Siswa BuatMatKul(char *NPM,int kode,float nilai,int SKS)
{
if(strcmp(S.NPM,NPM)==0)
{
S.matakuliah[n].kode = kode;
S.matakuliah[n].nilai = nilai;
S.matakuliah[n].SKS = SKS;
n++;
}
};

void CetakMkul()
{
for(int i=0;i<n;i++)
{
printf("\tData Mata Kuliah ke-%d\n",i+1);
printf("\tKode Mata Kuliah : %d\n",S.matakuliah[i].kode);
printf("\tNilai Mata Kuliah : %.2f\n",S.matakuliah[i].nilai);
printf("\tSKS Mata Kuliah : %d SKS\n\n",S.matakuliah[i].SKS);
}
return;
};

};

int main()
{
ClassSiswa S1;
S1.n=0;
printf("* Data Mahasiswa : \n");
S1.BuatDataSiswa("0608042","Laurensius","Kuningan");
S1.CetakData();
printf("\n* Data Nilai : \n");
S1.BuatMatKul("0608042",9033,86.6,3);
S1.BuatMatKul("0608042",9036,100,2);
S1.BuatMatKul("0608042",9090,100,4);
S1.CetakMkul();
getch();
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s