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.
Next Steps:
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Buy Computer Science Books
- Buy C++ Books
- Apply for Information Technology Internship
- Apply for C++ Internship
- Buy Programming Books