Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- /* Podejście polimorficzne, mądre i dobre: */
- class Human
- {
- private:
- std::string name;
- public:
- Human() {};
- Human(std::string name) : name(name) {}
- virtual void sayHi() { std::cout << "Hello from Human!" << std::endl; }
- virtual void sayIdentity() = 0;
- /* ^^^^^^^ Jedyna zmiana */
- };
- class Man : public Human
- {
- public:
- Man() {};
- Man(std::string name) : Human(name) {}
- void sayIdentity() { std::cout << "I'm a Man" << std::endl; }
- virtual void sayHi() { std::cout << "Hello from Man!" << std::endl; }
- };
- class Woman : public Human
- {
- public:
- Woman() {};
- Woman(std::string name) : Human(name) {}
- void sayIdentity() { std::cout << "I'm a Woman" << std::endl; }
- virtual void sayHi() { std::cout << "Hello from Woman!" << std::endl; }
- };
- int main()
- {
- Human* h_array[10];
- for (int i = 0; i < 10; ++i)
- {
- if (i%2==0)
- h_array[i] = new Man();
- else
- h_array[i] = new Woman();
- h_array[i]->sayHi();
- delete h_array[i];
- }
- std::cout << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement