Tsallaka zuwa abun ciki
Justice Tech School

Multiple Inheritance and Interfaces

A class can inherit from several bases. Pure abstract classes act as interfaces. Virtual inheritance solves the diamond problem.

Example
#include <iostream>
using namespace std;
struct Flyer { virtual void fly() const { cout << "flying\n"; } virtual ~Flyer() = default; };
struct Swimmer { virtual void swim() const { cout << "swimming\n"; } virtual ~Swimmer() = default; };
struct Duck : Flyer, Swimmer {};
int main() { Duck d; d.fly(); d.swim(); }

Result

flying
swimming

Exercise

Inherit from two classes.

struct Duck : A B {}

Quiz Test

Can a C++ class have several base classes?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.