Created
April 16, 2023 18:27
-
-
Save arisupriatna14/925ccd8274b6f892aeee1521fef0f95e 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 <memory> | |
#include <vector> | |
class Binatang { | |
public: | |
std::string nama; | |
Binatang(std::string n) : nama(n) {} | |
virtual void suara() = 0; | |
}; | |
class Kucing : public Binatang { | |
public: | |
Kucing(std::string n) : Binatang(n) {} | |
void suara() override { | |
std::cout << "\"meow meow\"" << std::endl; | |
} | |
}; | |
class Bebek : public Binatang { | |
public: | |
Bebek(std::string n) : Binatang(n) {} | |
void suara() override { | |
std::cout << "\"kukuruyuk\"" << std::endl; | |
} | |
}; | |
int main() { | |
std::vector<std::unique_ptr<Binatang>> binatangs; | |
binatangs.emplace_back(std::make_unique<Kucing>("Kucing")); | |
binatangs.emplace_back(std::make_unique<Bebek>("Bebek")); | |
std::cout << "POLIMORFISME 2" << std::endl; | |
std::cout << "==============" << std::endl; | |
for (const auto& binatang : binatangs) { | |
std::cout << "Informasi Binatang" << std::endl; | |
std::cout << "Nama: " << binatang->nama << std::endl; | |
std::cout << "Suara: "; | |
binatang->suara(); | |
std::cout << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment