๐ŸŽ“ A1Learner
๐Ÿ”

C++ Polymorphism

Polymorphism is the ability of a base class pointer to call the method that belongs to the actual object. One call can run different methods for different objects.

The problem without virtual

The sound method in Animal is not virtual. The pointer calls the Animal version, so the base line prints.

#include <iostream>

class Animal {
public:
  void sound() {
    std::cout << "Some sound" << std::endl;
  }
};

class Cat : public Animal {
public:
  void sound() {
    std::cout << "Meow" << std::endl;
  }
};

int main() {
  Cat cat;
  Animal* a = &cat;
  a->sound();
  return 0;
}

The fix with virtual and override

The sound method in Animal is virtual now. The Cat version carries override, so the Cat line prints.

#include <iostream>

class Animal {
public:
  virtual void sound() {
    std::cout << "Some sound" << std::endl;
  }
};

class Cat : public Animal {
public:
  void sound() override {
    std::cout << "Meow" << std::endl;
  }
};

int main() {
  Cat cat;
  Animal* a = &cat;
  a->sound();
  return 0;
}

Two objects, two sounds

Two base pointers point at two different objects. Each call runs the method of its own object.

#include <iostream>

class Animal {
public:
  virtual void sound() {
    std::cout << "Some sound" << std::endl;
  }
};

class Cat : public Animal {
public:
  void sound() override {
    std::cout << "Meow" << std::endl;
  }
};

class Dog : public Animal {
public:
  void sound() override {
    std::cout << "Woof" << std::endl;
  }
};

int main() {
  Cat cat;
  Dog dog;
  Animal* first = &cat;
  Animal* second = &dog;
  first->sound();
  second->sound();
  return 0;
}

Example

#include <iostream>

class Animal {
public:
  virtual void sound() {
    std::cout << "Some sound" << std::endl;
  }
};

class Duck : public Animal {
public:
  void sound() override {
    std::cout << "Quack" << std::endl;
  }
};

class Cow : public Animal {
public:
  void sound() override {
    std::cout << "Moo" << std::endl;
  }
};

int main() {
  Duck duck;
  Cow cow;
  Animal* first = &duck;
  Animal* second = &cow;
  first->sound();
  second->sound();
  return 0;
}

Key points

  • A base class pointer can point at an object of a derived class.
  • A method called through a base pointer runs the base version when the base method is not virtual.
  • The virtual keyword marks a base method for choice by the actual object.
  • The override keyword marks a derived method that replaces a virtual base method.
  • A call through a base pointer runs the method of the actual object when the base method is virtual.

In this example, two base pointers point at a Duck and a Cow in turn. The first call prints Quack. The second call prints Moo.

Note

Polymorphism in this lesson works through base class pointers.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท Programiz C++ Compiler

Exercise

A base pointer points at a Cat object and the base sound method is virtual. Which line prints when sound is called?