Skip to content

Instantly share code, notes, and snippets.

@arisupriatna14
Created April 16, 2023 18:27
Show Gist options
  • Save arisupriatna14/925ccd8274b6f892aeee1521fef0f95e to your computer and use it in GitHub Desktop.
Save arisupriatna14/925ccd8274b6f892aeee1521fef0f95e to your computer and use it in GitHub Desktop.
#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