### Examples of Single and Multiple Inheritance

Suppose you have defined a shape class to describe and operate on geometric shapes. Now suppose you want to define a circle class. Because you have existing code that operates on the shape class, you can use inheritance to create the circle class. You can redefine operations in the derived circle class that were originally defined in the shape base class. When you manipulate an object of the circle class, these redefined function implementations are used.

For example:

    class shape
    {
          char* name;
          int xpoint, ypoint;
    public:
          virtual void rotate(int);
          virtual void draw();
          void display() const;
    };

    class circle: public shape      // derive class circle from
    class shape
    {
          int xorigin, yorigin;
          int radius;
    public:
          void rotate(int);
          void draw();
          void display() const;
    };

In the above example, class circle inherits the data members name , xpoint and ypoint, as well as the member functions display() , rotate(), and draw() from class shape. Because the member functions rotate() and draw() are declared in class shape with the keyword virtual, you can provide an alternative implementation for them in class circle.

You can also provide an alternative implementation for the nonvirtual member function display() in class circle. When you manipulate an argument of type circle using a pointer to shape, and call a virtual member function, the member function defined in the derived class overrides the base-class member function. A similar call to a nonvirtual member function will call the member function defined in the base class. In addition to inheriting the members of class shape, class circle has declared its own data members, xorigin, yorigin, and radius.

The key difference between virtual and nonvirtual member functions is that , when you treat the circle class as if it were a shape, the implementations of the virtual functions rotate() and draw() defined in class circle are used, rather than those originally defined in class shape. Because display() is a nonvirtual member function, the original implementation of display() defined in class shape is used.

Multiple inheritance allows you to create a derived class that inherits properties from more than one base class.

For example, in addition to the shape class, described above, you could also have a symbol class. Because a circle is both a shape and a symbol, you can use multiple inheritance to reflect this relationship. If the circle class is derived from both the shape and symbol classes, the circle class inherits properties from both classes.

    class symbol
    {
          char* language;
          char letter;
          int number;
    public:
          virtual void write();
          virtual void meaning();
    };
    class shape
    {
          char* name;
          int xpoint, ypoint;
    public:
          virtual void rotate(int);
          virtual void draw();
          void display() const;
    };
    class circle: public symbol, public shape
    {
          int xorigin, yorigin;
          int radius;
    public:
          void rotate(int);
          void draw ();
          void write();
          void meaning();
          void display() const;
    };

In the above example, class circle inherits the members name, xpoint, ypoint, display(), rotate(), and draw() from class shape and also inherits the members language, letter, number , write(), and meaning() from class symbol.