Codectionary / Developer documentation / C++

C++ virtual and override

C++ reference for c++ virtual and override.

Syntax

class Animal { public: virtual void speak() const; };
class Dog : public Animal { public: void speak() const override; };

Examples

C++ virtual and override example

A focused example you can adapt while practising.

class Animal { public: virtual void speak() const; };
class Dog : public Animal { public: void speak() const override; };

Best practices

  • Start with the smallest working example, then adapt it to your project.
  • Use the language’s standard library and idioms before introducing extra dependencies.

At a glance

Purpose
Systems and general-purpose software
File extension
.cpp · .hpp
Runs in
Compiled native applications
Usually used with
Compiler and standard library

Specifications & further reading

Related C++ documentation