J
Jochen Zeischka
Hello,
I just tried something with multiple inheritance and I have a problem with
the construction of my object.
There's a base class Base, containing an integer. The base class has 2
derived classes: Derived1 and Derived2. Then, multiple inheritance is used
to derive MultipleDer from Derived1 and Derived2. All classes have 1
constructor: ClassName::ClassName(int i = 0);
The argument i is always passed to a class's base class, but when the code
is run, the default value for class Base is used. Does anybody know why?
Here's the code:
#include<iostream>
using namespace std;
class Base {
int i;
public:
Base(int ii=12345) {i=ii;}
void set(int ii) {i=ii;}
int get() const {return i;}
};
class Derived1: public virtual Base {
public:
Derived1(int i=0): Base(i) {;}
};
class Derived2: public virtual Base {
public:
Derived2(int i=0): Base(i) {;}
};
class MultipleDer: public Derived1, public Derived2 {
public:
MultipleDer(int i=0): Derived1(i), Derived2(i) {;}
void set1(int i) {Derived1::set(i);}
void set2(int i) {Derived2::set(i);}
int get1() const {return Derived1::get();}
int get2() const {return Derived2::get();}
};
int main() {
MultipleDer x(100);
cout << x.get() << endl << x.get1() << endl << x.get2() << endl << endl;
x.set(0);
cout << x.get() << endl << x.get1() << endl << x.get2() << endl << endl;
x.set1(1);
cout << x.get() << endl << x.get1() << endl << x.get2() << endl << endl;
x.set2(2);
cout << x.get() << endl << x.get1() << endl << x.get2() << endl << endl;
return 0;
}
Thanks for any help!
Jochen
I just tried something with multiple inheritance and I have a problem with
the construction of my object.
There's a base class Base, containing an integer. The base class has 2
derived classes: Derived1 and Derived2. Then, multiple inheritance is used
to derive MultipleDer from Derived1 and Derived2. All classes have 1
constructor: ClassName::ClassName(int i = 0);
The argument i is always passed to a class's base class, but when the code
is run, the default value for class Base is used. Does anybody know why?
Here's the code:
#include<iostream>
using namespace std;
class Base {
int i;
public:
Base(int ii=12345) {i=ii;}
void set(int ii) {i=ii;}
int get() const {return i;}
};
class Derived1: public virtual Base {
public:
Derived1(int i=0): Base(i) {;}
};
class Derived2: public virtual Base {
public:
Derived2(int i=0): Base(i) {;}
};
class MultipleDer: public Derived1, public Derived2 {
public:
MultipleDer(int i=0): Derived1(i), Derived2(i) {;}
void set1(int i) {Derived1::set(i);}
void set2(int i) {Derived2::set(i);}
int get1() const {return Derived1::get();}
int get2() const {return Derived2::get();}
};
int main() {
MultipleDer x(100);
cout << x.get() << endl << x.get1() << endl << x.get2() << endl << endl;
x.set(0);
cout << x.get() << endl << x.get1() << endl << x.get2() << endl << endl;
x.set1(1);
cout << x.get() << endl << x.get1() << endl << x.get2() << endl << endl;
x.set2(2);
cout << x.get() << endl << x.get1() << endl << x.get2() << endl << endl;
return 0;
}
Thanks for any help!
Jochen