Sabtu, 02 Juni 2012

Polimorpisme dengan Objek Manusia

Berikut adalah source code mengenai Polimorpisme Turunan dari 1 class dasar

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

class Man
{
protected:
   string name;
   short age;
public:
   Man(string name, short age):name(name),age(age){
   }
   virtual void showAttributes(){
   }
};
class Artist: public Man{
private:
   string alias;
public:
   Artist(string name, short age,string alias):Man(name,age),
alias(alias){
   }
  void showAttributes(){
     cout << "ARTIST " << name <<  endl;
  }
};

class Lecturer: public Man {
private:
   string course;
public:
   Lecturer(string name, short age,string course):Man(name,age),course(course){
   }
  void showAttributes(){
     cout << "LECTURER " << name << endl;
  }
};
int main(){
   Artist me("Tukul",59,"Cink Cank Conk");
   Lecturer u("Emergency",17,"C++");
   Man* it = &me;
   it->showAttributes();
   it = &u;
   it->showAttributes();
   getch();
}

Tidak ada komentar:

Posting Komentar