Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

(C++) Derived class

 

A class that has inherited member variables and methods from a base class.

 

The code below shows how an Animal is used as a base class and Cat and Dog are derived classes (of Animal).

 

 

#include <iostream>

#include <string>

 

struct Animal //Animal is a (non-abstract) base class

{

std::string mName; //All animals have a name (note: should better be const)

virtual ~Animal() {} //All animals have a destructor

//All base classes must have a virtual destructor

};

 

struct Cat : public Animal //A cat is a-kind-of animal

{

void Meow() const { std::cout << "Meow" << std::endl; }

};

 

struct Dog : public Animal //A dog is a-kind-of animal

{

void Bark() const { std::cout << "Bark" << std::endl; }

bool mHasBone;

};

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.