C++ Using through an interface a class that inherits from several others - c++

I am learning C++ and I am confused about the multiple inheritance and the interface classes.
I want to have a class that inherits from several others. Also, I want to use that derived class through an interface. So I suppose, the derived class should extend the base classes, and the derived interface should extend the base interfaces. I would have done that in other languages, but I think C++ does not work like that.
This is the code I thought should work:
#include <iostream>
using std::cout;
using std::endl;
class
Base1Itf
{
public:
virtual void blue() = 0;
};
class
Base1Abs
:
public Base1Itf
{
public:
void blue()
{
cout << "blue" << endl;
}
};
class
DerivedItf
:
public Base1Itf
{
public:
virtual void red() = 0;
};
class
Derived
:
public Base1Abs,
public DerivedItf
{
public:
void red()
{
cout << "red" << endl;
}
};
int main()
{
DerivedItf* d = new Derived();
d->red();
d->blue();
delete d;
return 0;
}
This is the compiler error I get:
src/test.cpp: In function ‘int main()’:
src/test.cpp:49:30: error: invalid new-expression of abstract class type ‘Derived’
DerivedItf* d = new Derived();
^
src/test.cpp:35:2: note: because the following virtual functions are pure within ‘Derived’:
Derived
^~~~~~~
src/test.cpp:10:16: note: virtual void Base1Itf::blue()
virtual void blue() = 0;
^~~~
In the example only a base class is implemented, but there will be many more.
What I am doing wrong? Thank you.
EDIT
If I remove the Base1Abs inheritance of Base1Itf for avoiding the Diamond Problem, the compiler shows the same error.

This is the well known diamond problem in C++. This is how you solve it:
#include <iostream>
using std::cout;
using std::endl;
class Base1Itf {
public:
virtual void blue() = 0;
virtual ~Base1Itf() { }
};
class Base1Abs : virtual public Base1Itf {
public:
void blue() override {
cout << "blue" << endl;
}
virtual ~Base1Abs() { }
};
class DerivedItf : virtual public Base1Itf {
public:
virtual void red() = 0;
virtual ~DerivedItf() { }
};
class Derived : public Base1Abs, public DerivedItf {
public:
void red() override {
cout << "red" << endl;
}
virtual ~Derived() { }
};
int main() {
DerivedItf* d = new Derived();
d->red();
d->blue();
delete d;
return 0;
}
It is also recommended to use virtual destructors in inheritance.
You see, what happens here is that classes Base1Abs and DerivedItf both inherit a copy of blue. Now, when you inherit another class Derived from these 2, that class inherits 2 copies of blue and then the compiler starts wondering which copy is to be called. So, you inherit the 2 classes virtually resulting in only one copy of blue getting inherited

Related

Overriding non-virtual function from abstract grandparent class

I am learning and playing around with inheritance and abstract classes. I've run into a predicament that I would appreciate some clarifications on.
I am trying to override a non-virtual function from an abstract grandparent class. I am getting an error saying that 'member function declared with 'override' does not override a base class member.
I can call the original non-virtual function from child class instances in main(), but not override them in child classes?
Here's the code.
UPDATE:
Error went away when I marked the function as virtual, I would still appreciate an explanation as to why that virtual is necessary?
#include <iostream>
using namespace std;
class A
{
public:
virtual string GetClassName() = 0;
void foo(); // <--- this is the problem function
{
cout << "foo" << endl;
}
};
class B : public A
{
public:
string GetClassName() override
{
return "B";
}
};
class C1 : public B
{
public:
string GetClassName() override
{
return "C";
}
};
class C2 : public B
{
public:
void foo() override // ERROR:member function declared with override does not override a base class member.
{
cout << "foo c1" << endl;
}
};
// testing interface
void printName(A* ptr)
{
cout << ptr->GetClassName() << endl;
}
int main()
{
B* b = new B();
C1* c1 = new C1();
C2* c2 = new C2();
printName(b); // prints B
printName(c1); // prints C
printName(c2); // prints B
b->foo(); // prints foo, inherited directly from abstract class
c1->foo(); // prints foo, inherited directly from abstract class
c2->foo(); // ??
}
You cannot override a non-virtual function. It is as simple as that.
Methods in child classes can hide methods of parent classes when they have the same name, for example:
struct A {
void foo(){}
};
struct B : A {
void foo() {}
};
But thats not overriding. To override the method must be virtual. Thats one of the conditions that the override specifier helps to check:
struct A {
virtual void foo(){}
};
struct B : A {
void foo() override {} // <- error if A::foo is not virtual
};
PS: I have seen poor tutorials, that use the first example and call that overriding. Thats just wrong.

How do I choose the base class when overriding a function in c++?

I have two base classes and a class that inherits both base classes.
Both base classes have a virtual function with the same signature, and I want to provide different implementations in the derived class to each virtual function.
class A{
virtual void f() = 0;
}
class B{
virtual void f() = 0;
}
class Derived:public A, public B{
void A::f() override{ // Error
...
}
void B::f() override{ // Error
...
}
}
What is the correct way to do this? (I cannot rename the virtual function. Actually the two base classes are generated from the same template class.)
template <typename T>
class AShim : public A {
void f() override {
static_cast<T*>(this)->A_f();
}
};
template <typename T>
class BShim : public B {
void f() override {
static_cast<T*>(this)->B_f();
}
};
class Derived: public AShim<Derived>, public BShim<Derived> {
void A_f();
void B_f();
};
class A {
public:
virtual void f() = 0;
};
class B {
public:
virtual void f() = 0;
};
class Derived :public A, public B {
public:
void A::f() {
cout << "Inside A's version"<<endl;
}
void B::f() {
cout << "Inside B's version"<<endl;
}
};
int main()
{
Derived derived;
cout << "calling A" << endl;
A *a;
a = &derived;
a->f();
cout << "calling B" << endl;
B *b;
b = &derived;
b->f();
}
Works fine for me. No need to explicitly mention override keyword as pure virtual functions will be overridden by virtue of its default properties.Use base class's scope while defining the functions as you have already done. Use public access specifier to enable derived classes to override the pure virtual function. That's all.

How can specify the base class from whom I derive in multiple inheritance?

I have a homework for my programming course that asks me to designs an abstract from whom derive two classes, and from those two derived classes derives another class.
This brings the Deadly Diamond of Death problem. Which can be solved by virtual inheritance, though I need instance the objects of my first two classes to a pointer of my abstract class. And this can't be done with any kind of virtual inheritance. So, if there is a way to specify the base class from which the multiple inheritance class will derive, it would be astronomically useful.
Example:
#include <iostream>
using namespace std;
class Base {
public:
virtual void foo(){};
virtual void bar(){};
};
class Der1 : public Base {
public:
virtual void foo();
};
void Der1::foo()
{
cout << "Der1::foo()" << endl;
}
class Der2 : public Base {
public:
virtual void bar();
};
void Der2::bar()
{
cout << "Der2::bar()" << endl;
}
class Join : virtual Der1, virtual Der2 {
private:
int atribb;
};
int main()
{
Base* obj = new Join();
((Der1 *)(obj))->foo();
}
Compiler error:
nueve.cpp: In function ‘int main()’:
nueve.cpp:43:30: error: ‘Base’ is an ambiguous base of ‘Join’
Base* obj = new Join();
Use virtual inheritance.
class Der1 : public virtual Base {
public:
virtual void foo();
};
class Der2 : public virtual Base {
public:
virtual void bar();
};
If virtual is declared while deriving from base class, as shown in below example, the most derived class object contains only one base class sub object, which helps in resolving the ambiguity in your case.
After doing some code changes, your final executable code:
Code changes in below sample:
Added vitual key word, while deriving the child classes.
Removed "virtual" keyword while deriving most derived class.
Modified main to invoke the member function properly.
#include <iostream>
using namespace std;
class Base {
public:
virtual void foo(){};
virtual void bar(){};
};
class Der1 : public virtual Base {
public:
virtual void foo();
};
void Der1::foo()
{
cout << "Der1::foo()" << endl;
}
class Der2 : public virtual Base {
public:
virtual void bar();
};
void Der2::bar()
{
cout << "Der2::bar()" << endl;
}
class Join : public Der1, public Der2 {
private:
int atribb;
};
int main()
{
Base* obj = new Join();
obj->foo();
}
It's not clear what you're asking for, although you seem to be saying that virtual inheritance from Base is not suitable. So, assuming that's correct, you can replace Base* obj = new Join(); with Base* obj = (Der1*)new Join(); and you'll get a pointer to the Base object that's the base of Der1. Similarly, you can replace it with Base* obj = (Der2*)new Join(); and you'll get a pointer to the Base object thats the base of Der2.

Inheritance from two abstract classes

I have a problem that I have not yet tested/compiled and wondering if it is possible and if it is bad design?
My Problem:
I want to have an abstract base class A and a abstract derived class B.
I realize if this is possible I will have a number of pure virtual member functions in each class and also I will not be able to initialize these objects, but that is a given for abstract classes.
In my design I will have another derived class C which I would then initialize - class C would be derived from class B.
I would have something that looked like this
class C
^
|
abstract class B
^
|
abstract base class A
My Question:
Is this possible first of all? I would suspect so, but not declaring the pure virtual functions in A in class B may be troublesome?
e.x.
class A {
public:
virtual void test()=0;
};
class B: public A {
public:
virtual void test()=0;
virtual void anotherTest()=0;
};
Is the above okay?
Is this bad c++ design? In future I will have derived classes from A so it would be nice to have this design.
Nothing wrong with it, and it will certainly work. Example follows
stefanos-imac:dftb borini$ more test.cpp
#include <iostream>
class A {
public:
A(void) { std::cout << "A" << std::endl; }
virtual void method1() = 0;
};
class B : public A {
public:
B(void) : A() { std::cout << "B" << std::endl; }
virtual void method2() = 0;
};
class C : public B {
public:
C(void) : B() { std::cout << "C" << std::endl; }
virtual void method1() { std::cout << "method1" << std::endl; }
virtual void method2() {std::cout << "method2" << std::endl; }
};
int main() {
C c;
c.method1();
c.method2();
}
stefanos-imac:dftb borini$ ./a.out
A
B
C
method1
method2
Thank you for reminding me that I can still type basic C++.

is it possible to have a virtual class declaration in a class?

I'm setting a up an interface for various components of a framework in a personal project, and i've suddenly thought of something that i figured might be useful with an interface. My question is whether this is possible or not:
class a
{
public:
virtual class test = 0;
};
class b : public a
{
public:
class test
{
public:
int imember;
};
};
class c : public a
{
public:
class test
{
public:
char cmember; // just a different version of the class. within this class
};
};
sort of declaring a virtual class or pure virtual class, that is required to be defined within the derived object, so that you might be able to do something like this:
int main()
{
a * meh = new b();
a * teh = new c();
/* these would be two different objects, but have the same name, and still be able
to be referred to by an interface pointer in the same way.*/
meh::test object1;
teh::test object2;
delete meh;
delete teh;
return 0;
}
msvc++ throws me a bunch of syntax errors, so is there a way to do this, and i'm just not writing it right?
No, it isn't valid. In any case, C++ has no concept of virtual classes as such. You can probably achieve what you want by holding a pointer to a certain class with only pure virtual methods (although that isn't a requirement):
class ITest { /* full of pure virtual methods... maybe. */};
class a
{
public:
virtual ITest* someFunctionName()=0 ;
private:
ITest* test_;
};
Then you can decide to inherit from a, giving each implementation concrete implementations of ITest, or some other approach, such as deciding which implementation to use based on some constructor parameter, for example.
The keyword "virtual" merely means "table to dispatch function call.
What you proposed is not part of the language.
But you can approach it in another way, by chaining the object creation to proper virtual calls:
#include <iostream>
using namespace std;
class a
{
public:
class test
{
public:
virtual ~test() {} ///< required to have polimorphic behavior
virtual void hello() const =0;
};
virtual test* create_test()=0;
};
class b: public a
{
public:
class test_b: public a::test
{
virtual void hello() const
{ cout << "this is test_b at " << this << endl; }
};
virtual test* create_test()
{ return new test_b; }
};
class c: public a
{
public:
class test_c: public a::test
{
virtual void hello() const
{ cout << "this is test_c at " << this << endl; }
};
virtual test* create_test()
{ return new test_c; }
};
int main()
{
a* pa1 = new b;
a* pa2 = new c;
a::test* p1 = pa1->create_test();
a::test* p2 = pa2->create_test();
p1->hello();
p2->hello();
delete p2; delete p1;
delete pa2; delete pa1;
}