This C++ program illustrates multiple inheritance. The concept of multiple inheritance is used when derivation is to be done from two or more classes. The features available in the classes from which derived class is to be constructed are available in the derived class.
Here is the source code of the C++ program which illustrates multiple inheritance. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to illustrate multiple inheritance
*/
#include <iostream>
using namespace std;
class A {
int i;
public:
A() : i(1) {
cout << "A's constructor\n";
}
void printI(){
cout << "i = " << i << "\n";
}
};
class B {
char c;
public:
B() : c('a') {
cout << "B's constructor\n";
}
void printC(){
cout << "c = " << c << "\n";
}
};
class C : public A, public B {
public:
C() {
cout << "C's constructor\n";
}
};
int main () {
C c;
c.printI();
c.printC();
}
$ gcc test.cpp $ a.out A's constructor B's constructor C's constructor i = 1 c = a
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Practice Programming MCQs
- Apply for Computer Science Internship
- Apply for C++ Internship
- Check C++ Books
- Check Computer Science Books