C++Builder Learn C++ Inheritance :: Hybrid Inheritance

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Learn C++ Inheritance :: Hybrid Inheritance
By Yilmaz Yoru May 27, 2021

Let’s remember that, Для просмотра ссылки Войди или Зарегистрируйся (OOP) is a way to integrate with objects which can contain data in the form (attributes or properties of objects), and code blocks in the form of procedures (methods, functions of objects). These attributes and methods are variables and functions that belong to the class, they are generally referred to as class members. In C++, classes have members (attributes, methods) and we should protect each member inside this class.

The Inheritance is one of the most important concept in object-oriented C++ programming as in other features of Classes. Inheritance allows us to define a class in terms of another class, and it makes easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time. Inheritance implements the relationship between classes. For example, a rectangle is a kind of shape and ellipse is a kind of shape etc.

Do you want to learn what is Hybrid Inheritance ? How we can use Inheritance in Hybrid from in an example ?

Hybrid Inheritance, it also is called Multipath Inheritance, is implemented by combining more than one type of inheritance. For example Hybrid Inheritance might be composed with derived classes with multiple base classes and these base classes have one common base class.

Here is a Hybrid Inheritance example below,
C++:
class base_class1
{
   // base properties and methods
}
 
class base_class2
{
   // base properties and methods
}
 
class classname1: public base_class1
{
   // properties and methods
}
 
class classname2: public base_class1 public base_class2
{
   // properties and methods
}
Let’s explain this with an example below;
C++:
#include <iostream>
 
class Animal // Base Class 1
{
  public:
 int weight;
};
 
class Wing // Base Class 2
{
  public:
 int wingwidth;
};
 
class Mammal: public Animal  // Derived Class 1
{
 
};
 
class Bird: public Animal, public Wing  // Derived Class 2
{
 
};
 
int main()
{
 Mammal elephant;  // mammal object
 Bird eagle; // bird object
 
 
 elephant.weight = 5000; // in kgf
 
 eagle.weight = 3;   // in kgf
 eagle.wingwidth = 280;  // in cm
 
 std::cout << " Elephant Weight:" << elephant.weight << " kgf\n\n";
 
 std::cout << " Eagle Weight:" << eagle.weight << " kgf\n";
 std::cout << " Eagle Wing Width:" << eagle.wingwidth << " cm\n";
 
 getchar();
 return 0;
}
In this example elephant object has only weight property but eagle has both weight and wingwidth properties. Please see how we declared both Mammal and Bird classes.