Files
ZUT-notatki/uczelnia/2_semestr/programowanie/laby/lab11/zad3.cpp
2026-05-26 15:01:50 +02:00

35 lines
822 B
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <iostream>
#include <memory>
#include <vector>
class Czlowiek {
public:
virtual void mowa() const { std::cout << "..." << std::endl; }
virtual ~Czlowiek() = default;
};
class Polak : public Czlowiek {
public:
void mowa() const override { std::cout << "Czesc!" << std::endl; }
};
class Anglik : public Czlowiek {
public:
void mowa() const override { std::cout << "Hello!" << std::endl; }
};
int main() {
std::vector<std::unique_ptr<Czlowiek>> ludzie;
ludzie.push_back(std::make_unique<Polak>());
ludzie.push_back(std::make_unique<Anglik>());
ludzie.push_back(std::make_unique<Czlowiek>());
for (const auto &c : ludzie)
c->mowa();
// Cześć!
// Hello!
// ...
// Przy metodzie niewirtualnej pojawi się błąd kompilacji nadpisujemy meto
// dę, która nie jest wirtualna
}