|
-
November 23rd, 2010, 08:25 AM
#1
Multi-level inheritance
Hi There,
I have a bit of problems with implementing multiple levels of interitance and am looking for advice how best to do it.
I start with an interface class looks like:
[code]
class Class1If {
public:
Class1If() {}
virtual ~Class1If() {}
virtual int PubFun() = 0;
protected:
virtual int ProFun() = 0;
virtual int WantToCall() = 0;
};
[code]
Then implementation:
Code:
class Class1 : public Class1If {
public:
Class1();
~Class();
virtual int PubFun();
protected:
virtual int ProFun();
virtual int WantToCall();
int Value;
};
Class1::Class1 {
Value = 1;
}
Class1::~Class1 {
Value = 0;
}
Class1::PubFun() {
Value = 2;
}
Class1::ProFun() {
Value = 3;
}
Class1::WantToCall() {
Value = 100;
}
That is fine until I would like to go one level beyond and still access, for example value, so what I try:
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;
}
And that ends up in a compilation error because multiple level inheritance is not supported by C++.
My question is that what is the best way to make sure that both Value and WantToCall() are available in Class2?
Thanks
-
November 23rd, 2010, 09:01 AM
#2
Re: Multi-level inheritance
 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;
}
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
November 23rd, 2010, 09:04 AM
#3
Re: Multi-level inheritance
 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++.
 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).
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
November 23rd, 2010, 10:30 AM
#4
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.
-
November 24th, 2010, 01:53 PM
#5
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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|