[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
multiple inheritance

Multiple inheritance

Multiple inheritance is a term from object oriented programming. In every modern object oriented programming language, a class can inherit from another class. Multiple inheritance simply means that a class can inherit from more than one class. An example of a programming language where this is possible is C++. In Java, it is not possible, but in most situations one can use interfaces instead to get the same thing in a different way. More specifically, in Java a class can inherit from only one class, but can ''implement'' multiple interfaces.

Example in C++:
class A
{
public:
  int GetA() { return a; }
  void SetA(int n) { a = n; }
private:
  int a;
};
class B
{
public:
  int GetB() { return b; }
  void SetB(int n) { b = n; }
private:
  int b;
};
class C: public A, public B
{
public:
  int GetSum() { return GetA()+GetB(); }
};
int main()
{
  C c;
  c.SetA(5);
  c.SetB(7);
  cout << c.GetSum() << endl; // is 12
}


last edited (December 24, 2006) by bilderbikkel, Number of views: 2508, Current Rev: 4 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.