La herencia múltiple es una característica de C++ donde una clase puede heredar de más de una clase. Los constructores de clases heredadas se llaman en el mismo orden en que se heredan. Por ejemplo, en el siguiente programa, el constructor de B se llama antes que el constructor de A.
Una clase puede derivarse de más de una clase base.
CPP
#include<iostream> using namespace std; class A { public: A() { cout << "A's constructor called" << endl; } }; class B { public: B() { cout << "B's constructor called" << endl; } }; class C: public B, public A // Note the order { public: C() { cout << "C's constructor called" << endl; } }; int main() { C c; return 0; }
CPP
#include<iostream> using namespace std; class Person { // Data members of person public: Person(int x) { cout << "Person::Person(int ) called" << endl; } }; class Faculty : public Person { // data members of Faculty public: Faculty(int x):Person(x) { cout<<"Faculty::Faculty(int ) called"<< endl; } }; class Student : public Person { // data members of Student 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); }
CPP
#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); }
CPP
#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); }
CPP
#include<iostream> using namespace std; class A { int x; public: void setX(int i) {x = i;} void print() { cout << x; } }; class B: public A { public: B() { setX(10); } }; class C: public A { public: C() { setX(20); } }; class D: public B, public C { }; int main() { D d; d.print(); return 0; }
CPP
#include<iostream> using namespace std; class A { int x; public: A(int i) { x = i; } void print() { cout << x; } }; class B: virtual public A { public: B():A(10) { } }; class C: virtual public A { public: C():A(10) { } }; class D: public B, public C { }; int main() { D d; d.print(); return 0; }
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA