Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) Derived class

 

A derived class is a class that has inherited member variables and member functions 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
{
  //All animals have a name (consider making this a const member)
  std::string m_name;

  // * Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6.
  //   Item 50: 'Make base class destructors public and virtual, or protected and nonvirtual'.
  virtual ~Animal() {} //All animals have a destructor
};

//A cat is a-kind-of animal
struct Cat : public Animal
{
  void Meow() const { std::cout << "Meow" << std::endl; }
};

//A dog is a-kind-of animal
struct Dog : public Animal
{
  void Bark() const { std::cout << "Bark" << std::endl; }
  bool m_has_bone;
};

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml