Multiple Inheritance in C++
来源:互联网 发布:淘宝 非典 编辑:程序博客网 时间:2024/05/29 15:22
#include<iostream>using namespace std;class Person {public: Person(int x) { cout << "Person::Person(int ) called" << endl; } Person() { cout << "Person::Person() called" << endl; }}; class Faculty : virtual public Person {public: Faculty(int x):Person(x) { cout<<"Faculty::Faculty(int ) called"<< endl; }}; class Student : virtual public Person {public: Student(int x):Person(x) { cout<<"Student::Student(int ) called"<< endl; }}; class TA : public Faculty, public Student {public: TA(int x):Student(x), Faculty(x) { cout<<"TA::TA(int ) called"<< endl; }}; int main() { TA ta1(30);}
Output:
Person::Person() calledFaculty::Faculty(int ) calledStudent::Student(int ) calledTA::TA(int ) called
In the above program, constructor of ‘Person’ is called once. One important thing to note in the above output is, the default constructor of ‘Person’ is called. When we use ‘virtual’ keyword, the default constructor of grandparent class is called by default even if the parent classes explicitly call parameterized constructor.
How to call the parameterized constructor of the ‘Person’ class? The constructor has to be called in ‘TA’ class. For example, see the following program.
#include<iostream>using namespace std;class Person {public: Person(int x) { cout << "Person::Person(int ) called" << endl; } Person() { cout << "Person::Person() called" << endl; }}; class Faculty : virtual public Person {public: Faculty(int x):Person(x) { cout<<"Faculty::Faculty(int ) called"<< endl; }}; class Student : virtual public Person {public: Student(int x):Person(x) { cout<<"Student::Student(int ) called"<< endl; }}; class TA : public Faculty, public Student {public: TA(int x):Student(x), Faculty(x), Person(x) { cout<<"TA::TA(int ) called"<< endl; }}; int main() { TA ta1(30);}
Output:
Person::Person(int ) calledFaculty::Faculty(int ) calledStudent::Student(int ) calledTA::TA(int ) called
In general, it is not allowed to call the grandparent’s constructor directly, it has to be called through parent class. It is allowed only when ‘virtual’ keyword is used.
Ref http://www.geeksforgeeks.org/multiple-inheritance-in-c/
- Multiple Inheritance in C++
- Multiple Inheritance in Java and Composition vs Inheritance
- Multiple inheritance
- non-virtual thunk for Virtual Function in multiple inheritance
- Default Methods in Java 8 and Multiple Inheritance
- non-virtual thunk for Virtual Function in multiple inheritance
- Why Multiple Inheritance is Not Supported in Java
- C++(13):多继承(multiple inheritance)
- Multiple Inheritance(11.4)
- About Multiple Inheritance
- [翻译] Effective C++, 3rd Edition, Item 40: 谨慎使用 multiple inheritance(多继承)
- Classical Inheritance in JavaScript
- Inheritance in C++
- Classical Inheritance in JavaScript
- Inheritance in Java
- Inheritance in JavaScript 继承
- Classical Inheritance in JavaScript
- multiple inheritance 的问题 diamond inheritance,及解决办法 virtual inherit
- 剑指offer-从尾到头打印链表
- Access denied for user 'Administrator'@'172.16.12.34' (using password: YES)
- 修改System.UI并编译
- 数据库三大范式
- Manacher算法总结
- Multiple Inheritance in C++
- MySQL锁类型以及子查询锁表问题、解锁
- stax和Do4j解析xml文件,并输出txt,zip,gz格式文件
- 怎么将U盘的FAT32转换成ntfs
- This application is modifying the autolayout engine from a background thread, which can lead
- 40多个关于人脸检测/识别的API、库和软件
- 安卓animation动画
- HTML meta viewport属性说明(mark)
- LLVM学习笔记(9)