Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179
Deprecated: Creation of dynamic property PPDStack::$accum is deprecated in /var/www/cppe.ru/www/includes/parser/Preprocessor_DOM.php on line 803
Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179
Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/vendor/mediawiki/at-ease/src/Functions.php on line 48
Материал из C\C++ эксперт
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Using virtual base classes
#include <iostream>
using std::cout;
using std::endl;
class Base
{
public:
virtual void print() const = 0; // pure virtual
};
class Derived1 : virtual public Base
{
public:
void print() const
{
cout << "Derived1\n";
}
};
class Derived2 : virtual public Base
{
public:
void print() const
{
cout << "Derived2\n";
}
};
class Multiple : public Derived1, public Derived2
{
public:
void print() const
{
Derived2::print();
}
};
int main()
{
Multiple both;
Derived1 one;
Derived2 two;
Base *array[ 3 ];
array[ 0 ] = &both;
array[ 1 ] = &one;
array[ 2 ] = &two;
for ( int i = 0; i < 3; i++ )
array[ i ]->print();
return 0;
}
Derived2
Derived1
Derived2