C++ Multiple Inheritance
Multiple Inheritance
A class can also be derived from more than one base class, using a comma-separated list:
For example, a smartphone is both a camera and a phone. So a Smartphone class can inherit from both a Camera class and a Phone class:
Example
// Base class
class Camera {
public:
void takePhoto() {
cout << "Taking a photo\n";
}
};
// Another base class
class Phone {
public:
void call() {
cout << "Calling...\n";
}
};
// Derived class
class Smartphone : public Camera, public Phone {
};
int main() {
Smartphone myPhone;
myPhone.takePhoto(); // From Camera
myPhone.call(); // From Phone
return 0;
}
Try it Yourself »
The Smartphone class has no methods of its own, but it can use the methods of both base classes.