Created
April 29, 2023 09:13
-
-
Save arisupriatna14/14e4ca792d27ecddd0a4bfca38228b00 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
class Hewan { | |
protected: | |
string indra; | |
string alatGerak; | |
public: | |
Hewan(string indra, string alatGerak) { | |
this->indra = indra; | |
this->alatGerak = alatGerak; | |
cout << "// Konstruktor kelas Hewan dijalankan" << endl; | |
} | |
~Hewan() { | |
cout << "// Destruktor kelas Hewan dijalankan" << endl; | |
} | |
void info(string namaHewan, string habitat) { | |
cout << "Info salah satu jenis hewan" << endl; | |
cout << "Nama: " << namaHewan << endl; | |
cout << "Habitat: " << habitat << endl; | |
cout << "Ciri Khusus: " << endl; | |
} | |
}; | |
class Burung : public Hewan { | |
private: | |
string sayap; | |
bool terbang; | |
public: | |
Burung(string indra, string alatGerak, string sayap, bool terbang) : Hewan(indra, alatGerak) { | |
this->sayap = sayap; | |
this->terbang = terbang; | |
cout << "// Konstruktor kelas Burung dijalankan" << endl; | |
} | |
~Burung() { | |
cout << "// Destruktor kelas Burung dijalankan" << endl; | |
} | |
void info(string namaHewan, string habitat) { | |
Hewan::info(namaHewan, habitat); | |
cout << sayap << endl; | |
if (terbang) { | |
cout << "Terbang" << endl; | |
} else { | |
cout << "Tidak terbang" << endl; | |
} | |
} | |
}; | |
class Gajah : public Hewan { | |
private: | |
string belalai; | |
bool gading; | |
public: | |
Gajah(string indra, string alatGerak, string belalai, bool gading) : Hewan(indra, alatGerak) { | |
this->belalai = belalai; | |
this->gading = gading; | |
cout << "// Konstruktor kelas Gajah dijalankan" << endl; | |
} | |
~Gajah() { | |
cout << "// Destruktor kelas Gajah dijalankan" << endl; | |
} | |
void info(string namaHewan, string habitat) { | |
Hewan::info(namaHewan, habitat); | |
cout << belalai << endl; | |
if (gading) { | |
cout << "Gading" << endl; | |
} else { | |
cout << "Tidak punya gading" << endl; | |
} | |
} | |
}; | |
int main() { | |
Gajah gajah("Penciuman", "Kaki", "Belalai", true); | |
Burung burung("Penglihatan", "Sayap", "Sayap", true); | |
cout << endl; | |
cout << "// awal fungsi main()" << endl; | |
cout << endl; | |
cout << "Inheritance pada C++" << endl; | |
cout << "--------------------" << endl; | |
cout << endl; | |
gajah.info("Gajah", "Padang rumput"); | |
cout << endl; | |
burung.info("Burung", "Hutan"); | |
cout << endl; | |
cout << "// akhir fungsi main()" << endl; | |
cout << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Link Repl untuk menjalankan code di atas:
https://replit.com/@arisupriatna/RightFlatKeychanger#main.cpp