Tsallaka zuwa abun ciki
Justice Tech School

Inheritance

A derived class inherits from a base. virtual functions allow overriding.

Example
#include <iostream>

struct Animal { virtual std::string sound() const { return "..."; } virtual ~Animal() {} };
struct Dog : Animal { std::string sound() const override { return "Woof"; } };

int main() {
  Animal *a = new Dog();
  std::cout << a->sound() << "\n";
  delete a;
  return 0;
}

Result

Woof

Exercise

Derived from.

class Dog :  Animal

Quiz Test

What does virtual enable?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.