A C++ class can inherit from another class.
To create a class that inherits from another class in C++, put a colon after the class name and specify the access level of the class (public, protected, or private) and the class from which it derives.
class Dog : public Mammal
This statement creates a derived class called Dog that inherits from the base class Mammal.
#include <iostream> class Mammal //from w w w.j a v a 2 s. c o m { public: // constructors Mammal(); ~Mammal(); // accessors int getAge() const; void setAge(int); int getWeight() const; void setWeight(); // other member functions void speak(); void sleep(); protected: int age; int weight; }; class Dog : public Mammal { public: // constructors Dog(); ~Dog(); // accessors int getBreed() const; void setBreed(int); protected: int itsBreed; }; int main() { return 0; }
Protected data members and functions are visible to derived classes, but are otherwise private.