Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

c++ polymorphism

#include <iostream>
 
class Animal {
public:
  void action() {
    std::cout << "The animal does something.
";
  }
};
 
class Fish: public Animal {
public:
  void action() {
    std::cout << "Fish swim.
";
  }
};
 
class Bird: public Animal {
public:
  void action() {
    std::cout << "Birds fly.
";
  }
};
 
int main() {
  Animal newAnimal;
  Fish newFish;
  Bird newBird;
 
  newAnimal.action();
  newFish.action();
  newBird.action();
 
  return 0;
}
Source by www.codecademy.com #
 
PREVIOUS NEXT
Tagged: #polymorphism
ADD COMMENT
Topic
Name
1+2 =