Multiple Inheritance - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Multiple Inheritance

Description:

Sometimes it is useful for derived classes to inherit from two base classes. ... Inheriting the same member name from different base classes. ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 23
Provided by: serv385
Category:

less

Transcript and Presenter's Notes

Title: Multiple Inheritance


1
Multiple Inheritance
  • Sometimes it is useful for derived classes to
    inherit from two base classes.
  • This feature not available in Smalltalk or Java.

2
Potential Problems with Multiple Inheritance.
  • Inheriting the same member name from different
    base classes.
  • Inheriting members of a base class through more
    than one inheritance path.

3
The problem
  • Many functions have the same behaviour but
    operate on different types.
  • Hence, basically the same function must be
    rewritten several times to accommodate the
    different types.

4
Templates
  • C permits the use of templates.
  • Same code used with various types.
  • Enables parametric polymorphism

5
Types of template
  • Two types
  • class template
  • function template

6
Function Templates
  • Function definition preceded by
  • template ltclass TYPEgt
  • where TYPE is an arbitrary type.

7
Example
  • template ltclass TYPEgt
  • void swap (TYPE x, TYPE y)

8
Example
  • int j double k
  • swap (j, k)
  • causes a compile-time error.
  • This is called a unification error.

9
Example
  • templateltclass T1, class T2gt
  • void copy(T1 a, T2 b, int n)
  • for (int i 0 i lt n i)
  • ai bi

10
Special cases
  • template ltclass Tgt
  • void swap (T x, T y)
  • T temp
  • temp x x y y temp

11
Example
  • int i, j
  • char str1100, char str2100, ch
  • swap (i, j)
  • swap (str150, str233)
  • swap (i, ch)
  • swap (str1, str2)

12
Example
  • void swap (char s1, char s2)
  • appropriate code

13
Overloaded function-selection algorithm.
  • 1. Exact match with trivial conversions on
    nontemplate functions.
  • 2. Exact match using function templates.
  • 3. Ordinary argument resolution on nontemplate
    functions.

14
Class templates
  • Allows an arbitrary type to be used in a class
    definition.
  • Saves us from rewriting class declarations where
    the only variation is the type declarations.

15
Class templates
  • The class declaration prefaced by
  • template ltclass identifiergt
  • identifier represents some arbitrary type which
    can be used throughout the declaration of the
    class

16
Defining classes
  • When defining a class that uses a class template,
    the angle brackets indicating the type must be
    included.

17
Member functions
  • Member functions defined within a class are
    inline.
  • If defined externally the full angle bracket
    declaration must be used.

18
Example
  • TYPE top_of() const return stop
  • would be rewritten as
  • template ltclass TYPEgt TYPE stackltTYPEgttop_o
    f() const
  • return stop

19
friends
  • Template functions can contain friends.
  • A friend function that does not use a template
    specification is a friend to all instantiations.
  • A friend function that incorporates template
    arguments is specifically a friend of its
    instantiated class.

20
Static members
  • Static members are not universal but specific to
    each instantiation.

21
Example
  • Template class ltTgt
  • class foo
  • public static int count
  • foo ltintgt a
  • foo ltdoublegt b

22
Example
  • The static variables
  • fooltintgtcount
  • fooltdoublegtcount
  • are distinct.
Write a Comment
User Comments (0)
About PowerShow.com