/*
Contoh program Polimorfisme
*/
#include <iostream>
#include <conio.h>
#include <string.h>
using namespace std;
class Manusia
{
protected:
string nama;
string alamat;
public:
Manusia(string nama, string alamat):nama(nama), alamat(alamat){}
void judul()
{
cout<<"\n Kelas Manusia!"<<endl;
}
virtual void tampil_data() //menggunakan fungsi virtual
{
cout<<"\n =========================";
cout<<"\n\t MANUSIA"<<endl;
cout<<" =========================";
cout<<"\n Nama : "<<nama<<endl;
cout<<" Alamat : "<<alamat<<endl;
cout<<" ========================="<<endl;
}
};
class Mahasiswa : public Manusia
{
private:
string nim;
string jurusan;
public:
Mahasiswa(string nim, string nama, string jurusan, string alamat):Manusia(nama,
alamat),nim(nim),jurusan(jurusan){}
void judul()
{
cout<<"\n Kelas Mahasiswa!"<<endl;
}
void tampil_data()
{
cout<<"\n =========================";
cout<<"\n\t MAHASISWA"<<endl;
cout<<" =========================";
cout<<"\n NIM : "<<nim<<endl;
cout<<" Nama : "<<nama<<endl;
cout<<" Jurusan : "<<jurusan<<endl;
cout<<" Alamat : "<<alamat<<endl;
cout<<" ========================="<<endl;
}
};
class Pegawai : public Manusia
{
private:
string nip;
string jabatan;
public:
Pegawai(string nip, string nama, string alamat, string jabatan):Manusia(nama,
alamat),nip(nip),jabatan(jabatan){}
void judul()
{
cout<<"\n Kelas Pegawai!"<<endl;
}
void tampil_data()
{
cout<<"\n\n =========================";
cout<<"\n\t PEGAWAI"<<endl;
cout<<" =========================";
cout<<"\n NIP : "<<nip<<endl;
cout<<" Nama : "<<nama<<endl;
cout<<" Alamat : "<<alamat<<endl;
cout<<" Jabatan : "<<jabatan<<endl;
cout<<" ========================="<<endl;
}
};
int main()
{
Mahasiswa mhs1("201081016", "Patricia", "Teknik Informatika", "Jakarta");
Pegawai peg1("4321.8765.1234", "Budi", "Surabaya","Direktur");
Manusia *man; //definisi pointer ke objek berkelas manusia
man = &mhs1; //menunjuk ke objek berkelas mahasiswa
man->judul();
man->tampil_data();
man = &peg1; //menunjuk ke objek pegawai
man->judul();
man->tampil_data();
getch();
}
No comments:
Post a Comment