Contoh Program Inheritance

Inheritance adalah proses dimana kelas baru yang disebut kelas turunan dibuat dari kelas-kelas yang ada disebut kelas dasar. Kelas-kelas turunan memiliki semua fitur dari kelas dasar dan programmer bisa memilih untuk menambahkan fitur baru yang khusus untukkelas turunan yang baru dibuat.
contoh codingannya :



#include <iostream>
#include <conio.h>
#include <string.h>
#include <stdio.h>

class
{
private :
char nama[25];
char alamat[30];

public :
man (){}
void setNama(char nama[25])
{strcpy (this -> nama,nama);}
void setAlamat(char alamat[30])
{strcpy (this -> alamat,alamat);}

char* getNama()
{return this -> nama;}
char* getAlamat()
{return this -> alamat;}
};

class sub : public man
{
private :
char nip[12];
char jbtn[15];
char depat[15];

public :
sub (){}

void setNIP(char nip[12])
{strcpy (this -> nip,nip);}
void setJabatan(char jbtn[15])
{strcpy (this -> jbtn,jbtn);}
void setDepartemen(char depat[15])
{strcpy (this -> depat,depat);}

char* getNIP()
{return (this ->nip);}
char* getJabatan()
return (this ->jbtn);}
char* getDepartemen()
{return (this ->depat);}
};

int main ()
{
man x;
sub y;

x.setNama("Emergency");
cout<<x.getNama()<<endl;
x.setAlamat("Jakarta");
cout<<x.getAlamat()<<endl<<endl;

y.setNama("Emergency");
y.setAlamat("Jakarta");
y.setNIP("1991-991-99");
y.setJabatan("Kepala Divisi");
y.setDepartemen("Pendidikan");

cout<<y.getNama()<<endl;
cout<<y.getAlamat()<<endl;
cout<<y.getNIP()<<endl;
cout<<y.getJabatan()<<endl;
cout<<y.getDepartemen()<<endl;
getch();


Hasil OutPutnya..

Post a Comment

0 Comments