C++/Class/Inheritance Multiple
Содержание
Call parent constructors in multiple inheritance
#include <iostream>
using namespace std;
class One
{
public:
One(void) { cout << "Constructor for One\n"; };
~One(void) { cout << "Destructor for One\n"; };
};
class Two
{
public:
Two(void) { cout << "Constructor for Two\n"; };
~Two(void) { cout << "Destructor for Two\n"; };
};
class Three
{
public:
Three(void) { cout << "Constructor for Three\n"; };
~Three(void) { cout << "Destructor for Three\n"; };
};
class Derived: public One, public Two, public Three
{
public:
Derived(void) : One(), Two(), Three()
{ cout << "Derived constructor called\n"; };
~Derived(void)
{ cout << "Derived destructor called\n"; };
};
int main(void)
{
Derived my_class;
}
Directly inherit two base classes.
#include <iostream>
using namespace std;
class BaseClass1 {
int a;
public:
BaseClass1(int x) {
a = x;
}
int geta() {
return a;
}
};
class BaseClass2 {
int b;
public:
BaseClass2(int x)
{
b = x;
}
int getb() {
return b;
}
};
class DerivedClass : public BaseClass1, public BaseClass2 {
int c;
public:
DerivedClass(int x, int y, int z) : BaseClass1(z), BaseClass2(y){
c = x;
}
void show() {
cout << geta() << " " << getb() << " ";
cout << c << "\n";
}
};
int main()
{
DerivedClass object(1, 2, 3);
object.show();
return 0;
}
extending two parent classes
#include <iostream>
#include <string.h>
using namespace std;
class Cover
{
public:
Cover(char *title) { strcpy(Cover::title, title); };
protected:
char title[256];
};
class Page
{
public:
Page(int lines = 55) { Page::lines = lines; };
protected:
int lines;
char *text;
};
class Book: public Cover, public Page
{
public:
Book(char *author, char *title, float cost): Cover(title), Page(60)
{
strcpy(Book::author, author);
strcpy(Book::title, title);
Book::cost = cost;
};
void show_book(void)
{
cout << title << endl;
cout << author << "\t" << cost;
};
private:
char author[256];
float cost;
};
int main(void)
{
Book text("A", "B", 49.95);
text.show_book();
}
Inherit two base classes.
#include <iostream>
using namespace std;
class BaseClass1 {
public:
BaseClass1() {
cout << "Constructing BaseClass1\n";
}
~BaseClass1() {
cout << "Destructing BaseClass1\n";
}
};
class BaseClass2 {
int b;
public:
BaseClass2() {
cout << "Constructing BaseClass2\n";
}
~BaseClass2() {
cout << "Destructing BaseClass2\n";
}
};
// Inherit two base classes.
class DerivedClass : public BaseClass1, public BaseClass2 {
public:
DerivedClass() {
cout << "Constructing DerivedClass\n";
}
~DerivedClass() {
cout << "Destructing DerivedClass\n";
}
};
int main()
{
DerivedClass object;
return 0;
}
Inherit two classes: constructing and destructing sequence
#include <iostream>
using namespace std;
class BaseClass1 {
public:
BaseClass1() {
cout << "Constructing BaseClass1\n";
}
~BaseClass1() {
cout << "Destructing BaseClass1\n";
}
};
class BaseClass2 {
public:
BaseClass2() {
cout << "Constructing BaseClass2\n";
}
~BaseClass2() {
cout << "Destructing BaseClass2\n";
}
};
class DerivedClass : public BaseClass2aseClass1, public BaseClass2 {
public:
DerivedClass() {
cout << "Constructing DerivedClass\n";
}
~DerivedClass() {
cout << "Destructing DerivedClass\n";
}
};
int main()
{
DerivedClass object;
return 0;
}