I hope this is a simple question.
Can I inherit both an abstract class and it's implementation? That is, can the following be made to work?
class A {
virtual void func1() = 0;
}
class B {
void func1() { /* implementation here */ }
}
class C : public A, public B {
}
I've tried a few variations, and am getting compile errors complaining about unimplemented methods in class C. I can save a lot of repeated code if I can make this work, however. Is it possible?
I solved this by creating a "composite class" called D which inherits from A & B, but contains the implementation code previously contained in B. This makes my inheritance model less clean, but it solves the problem without requiring code duplication. And, as I noted in the comments below, it makes my naming conventions pretty gross.
class A {
virtual void func1() = 0;
}
class B {
// Other stuff
}
class D : public A, public B {
void func1() { /* implementation here */ }
}
class C : public D {
}
class B is not an implementation of class A in your code. Class B should be inherited from class A and func1 should be virtual. Only in that case class B will be implementation of class A. And then there is no need to inherit from both A and B.
class A {
virtual void func1() = 0;
}
class B : public A {
virtual void func1() { /* implementation is here */ }
}
class C : public B {
}
Otherwise you will get unimplemented pure virtual function func1.
Make B inherit from A. If that is not possible, using virtual inheritance might also work (I am not entirely sure about this).
If you want to reuse code from B class in C class, try to do something like this:
class C : public A, public B {
void func1(){ B::func1(); }
}
As Kirill pointed out: Your premise is wrong.
Class B in your example does not inherit class A (it needs to be declared to do that first).
Thus, B.func1() is something entirely different to A.func1() for the compiler. In class C it is expecting you to provide an implementation of A.func1()
Somebody above posted something along the lines of:
class C : public A, public B
{
// implement A::func1()
virtual void func1()
{
// delegate to inherited func1() in class B
B::func1();
}
}
Related
Suppose I have the following classes:
class Base {
virtual void func() { cout << "func base" << endl; }
};
class A : virtual public Base {
public:
virtual void func() { cout << "func A" << endl; }
};
class B : virtual public Base {
public:
virtual void func() { cout << "func B" << endl; }
};
class C : public A, public B {
C(bool is_a);
public:
virtual void func() { // Call appropriate parent's func here }
};
My requirement is that the appropriate parent class' function func() be called when I call C's func(). This is what I mean by appropriate:
Base* ptr = new C(true /*is_a*/);
ptr->func(); // This should call A's func internally
How to achieve this? Is it even possible?
[edit]
This might be more of a design question. It is known that class C will have only one true parent (either A or B). And depending on which is the parent, I want that function to be called. Any alternative design suggestions are welcome.
I am deriving C from A and B because there is some common functionality which both A and B share and which can't be a part of base class.
You need to call A::func() explicitly.
class C : public A, public B {
public:
virtual void func() { A::func(); }
};
Update (to yours):
What do you really want to achieve? Is there an actual problem that you want to solve?
If you know that "class C will have only one true parent", why do you need to derive from A and B in the first place? Even though Aconcagua's answer works, it looks more like a workaround for a problem that isn't properly stated from your side. In case you are dealing with different implementations for your class, wouldn't you want to employ some kind of pattern (like the bridge or strategy pattern) instead?
This might be more of a design question. It is known that class C will have only one true parent (either A or B). And depending on which is the parent, I want that function to be called. Any alternative design suggestions are welcome.
class Base { };
class A : public Base { };
class B : public Base { };
class C
{
// common interface for all types of C
};
class C_A : public A, public C { };
class C_B : public B, public C { };
Now you can use C_A, wherever a C needs to behave as A and C_B analogously...
In above example, all virtual inheritances are removed, they are not needed as is. Depending on the use case, it might or might not be appropriate to let class C itself inherit from Base. If so, let all of A, B and C inherit virtually from Base. C_A and C_B, though, do not need to inherit virtually from their parents (there still will just be one Base instance inherited due to the virtual inheritance of the base classes).
This question already has an answer here:
abstract base classes, multiple inheritence, and common pure virtual methods
(1 answer)
Closed 6 years ago.
I have this problem, that I dont know, how to solve correctly.
class A : public B, public C
{
virtual void GetSomethingElse()
{
}
}
class B
{
public void GetSomething()
{
GetSomethingElse();
}
virtual void GetSomethingElse() = 0;
}
class C
{
virtual void GetSomethingElse() = 0;
}
Basically, what I have are two abstract classes B, C. Usually, I just need to inherit one of them, but in some particular cases, I need both of them.
How to correctly solve this problem with GetSomethingElse() method, that needs to be called from B. If I inherit only B, it will be implementted in child. If only from C, all is still OK.
However, if I inherit B and C, I have some "weird" design, that resemlbes diamond inheritance problem, but it is quite not the same.
As you already mentioned it is quite different from diamond problem.
Your design is correct and not weired. Having GetSomeThingElse as pure virtual is to enforce the child to define it. Defining it if it is because of C or B is not a design issue.
You would have a design issue if it were pure virtual in one and virtual in the other (B or C) as one gives you the choice and the other by obligation.
class A : public B, public C
{
virtual void GetSomethingElse()
{
}
}
class B:public class D
{
public void GetSomething()
{
GetSomethingElse();
}
virtual void GetSomethingElse()
{
}
}
class C:public class D
{
virtual void GetSomethingElse()
{
}
}
//here's the only pure virtual base class
class D
{
virtual void GetSomethingElse() = 0;
}
As they are abstract, you need to overwrite the method in class A.
Each object will know if it is a B or a C, so it will call the right function.
if, however, your class C has no function GetSomething(), then your inheritance structure is the wrong way around - remember the logic of inheritance is that every object of a derived class is an object of the parent class too? So if an A is not a B because is doesn't have such a call, something is wrong.
Another point is to derive virtual, so the two identical functions are considered the same: class A : virtual public B, virtual public C
Did you consider making a class that is the parent of B and C? If they don't have a common parent, why do they have a function each with the same name?
Suppose i have three classes A, B and C. class B inherits from class A and the inheritance is private whereas class C inherits from B and the inheritance is public. Now class A has a protected function which class C wants to access. So, what must be done in class B to make that protected function available to class C.
Here is the link to the code : http://pastebin.com/9E2sLZzj
The "using" keyword makes a member of an inherited class visible, and resolvable, in the scope of its subclass. So, to make the privately-inherited member available to B's subclasses:
class A {
protected:
void foo() {}
};
class B : private A {
protected:
using A::foo;
};
class C : public B {
void bar()
{
foo();
}
};
Okay i got the solution
This code fragment worked after inserting it into Class B.
int get(){
return A::get();
}
Not sure what it does though
I have three classes named A, B, and C. B inherits from A and C inherits from B. (A -> B -> C).
I also have an abstract base class named IBinary. I'd like to make all of the classes implement the IBinary interface. When I make class A inherit from IBinary, the output of my code is C::readb. When class A does not inherit from IBinary, the output is B:readb.
What is the proper way to make my three classes subscribe to the same interface? If I only have the top class (A) inherit from the interface class, I'll need to refactor my code so that I don't have resolution problems like the one above.
If I explicitly have all of the classes inherit from the interface class then I'll have a more complicated class hierarchy and become closer to having a diamond of death.
#include <iostream>
class IBinary {
public:
virtual void readb( std::istream& in ) = 0;
};
// Basic A -- change whether this inherits from IBinary
class A : public IBinary {
public:
A() {};
void readb( std::istream& in ) {}
};
// Specialized A
class B : public A {
public:
B() {};
void load() {
this->readb(std::cin); // <-- which readb is called?
}
void readb( std::istream& in ) {
std::cout << "B::readb" << std::endl;
}
};
// Specialized B
class C : public B {
public:
C() {};
void readb( std::istream& in ) {
std::cout << "C::readb" << std::endl;
}
void foo() {
B::load();
}
};
int main() {
C c;
c.foo();
}
The virtual in the definition of IBinary::readb makes all the difference.
When you inherit from IBinary, all the readbs in the hierarchy that override the one from IBinary are implicitly virtual, too. So virtual dispacth kicks in, just as it's supposed to.
When you don't, then the call is resolved statically. Because the call is inside B, it is B::readb that gets called.
Just have A inherit from IBinary which will make all the children be usable as the abstract interface IBinary.
The reason you are seeing this behavior is, in short, because A::readb is not declared virtual.
Because IBinary::readb is virtual, when A inherits from it, A::readb becomes virtual by default.
Your code would behave more consistently if you added virtual to every declaration of readb, rather than just the first. For this reason, a lot of code style guides for C++ make it a requirement that all virtual methods be declared virtual in all derived classes, even if they are not the ancestor base class.
I have this setup:
class A
{
public:
virtual void Function();
}
class B : private A
{
}
class C : public B
{
public:
// I want to expose A::Function() here
}
I tried to do this by adding:
class C : public B
{
public:
virtual void Function();
}
and
C::Function()
{
A::Function();
}
but I get and "inaccessible base" error.
Is it possible to do something like this?
In B you can change the accessibility of A::Function to protected:
class B : private A
{
protected:
using A::Function;
};
In C::Function (and elsewhere in C) you will then have to refer to the function as B::Function, not A::Function. You could also public: using B::Function; in C instead of implementing a C::Function that just calls B::Function.
You can't do this. The fact that B inherits from A is an implementation detail and you are not allowed to access it from C- just like you can't access B's private functions or member variables.
This would be completely legal if B inherited protected or public from A.
If you can change it to class B : protected A it should work.
Not really. The scenario you're describing would indicate that your class hierarchy needs to be reworked.