Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- class Drawable {
- public:
- virtual void draw() = 0;
- };
- void Drawable::draw() {
- std::cout << "draw superClass!!" << std::endl;
- }
- class Ship : public Drawable {
- public:
- void virtual draw();
- };
- void Ship::draw() {
- std::cout << "draw ship!!" << std::endl;
- }
- class Enemy : public Drawable {
- public:
- void virtual draw();
- };
- void Enemy::draw() {
- std::cout << "enemy ship!!" << std::endl;
- }
- int main() {
- std::vector<Drawable*> drawables;
- drawables.push_back(new Ship());
- drawables.push_back(new Enemy());
- for (std::vector<Drawable*>::iterator itr = drawables.begin(), end = drawables.end();
- itr != end; ++itr) {
- // use -> syntax for calling when we have a pointer to an object
- (*itr)->draw(); // calls every object in drawables that has draw()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement