Re: Multi-level inheritance
Quote:
Originally Posted by
luftwaffe
And that ends up in a compilation error because multiple level inheritance is not supported by C++.
Hum... What?
Your problem has nothing to do with that. Your code is rife with typo class errors. Just correct them and everything will be fine.
amongst others:
Code:
class Class2If : public Class1 {
public:
Class2If() {}
virtual ~Class2If() {}
virtual int PubFun() = 0;
protected:
virtual int ProFun() = 0;
};
class Class2 : public Class2If {
public:
Class2If();
virtual ~Class2If();
virtual int PubFun() = 0;
protected:
virtual int ProFun() = 0;
};
Class2::Class2() {
Value = 1;
}
Class2::~Class2() {
Value = 0;
}
Class2::PubFun() {
Value = 2;
}
Class2::ProFun() {
Value = 3;
}
Re: Multi-level inheritance
Quote:
Originally Posted by
luftwaffe
And that ends up in a compilation error because multiple level inheritance is not supported by C++.
Where did you get that idea?
Multi-level inheritance as well as multiple inheritance is possible in C++.
Quote:
Originally Posted by
luftwaffe
My question is that what is the best way to make sure that both Value and WantToCall() are available in Class2?
Don't declare PubFun and ProFun in Class2If or in Class2. They are already declared as virtual functions in Class1If, so you can use them in derived classes (since they are not private).
Re: Multi-level inheritance
Hmm, the trouble is that if I run the same code then both Value and WantToCall() appears undefined - no worries about anything else. Yes, there might be typos, I just put the stuff in; the question is rather theorethical, having:
Class1If -> Class1 -> Class2If -> Class2; Value and WantToCall() are reported as undefined.
Re: Multi-level inheritance
How exactly did you get it compiled with all those errors in the first place?
I cleaned your code and it worked, but since it was too cluttered to show any particular concept, here, this should make it clear:
Code:
#include <iostream>
class Base {
protected:
int value;
public:
Base() { }
virtual ~Base() { }
void setValue(int v) {
value = v;
}
virtual int getValue() = 0;
};
class A : public Base {
public:
A() { }
virtual ~A() { }
virtual int getValue() {
return value;
}
};
class B : public A {
public:
B() { }
virtual ~B() { }
};
class C : public B {
public:
C() { }
virtual ~C() { }
};
class D : public C {
public:
D() { }
virtual ~D() { }
};
class E : public D {
public:
E() { }
virtual ~E() { }
};
int main() {
E obj;
obj.setValue(69);
std::cout << obj.getValue() << "\n";
return 0;
}