How to resolve function name collision in c++ muti-inheritance? [duplicate] - c++

This question already has answers here:
C++ multiple inheritance function call ambiguity
(3 answers)
Closed 3 years ago.
I am inheriting from two classes. Both classes contain the same function name, but one being implemented and one being pure virtual. Is there any way that I can just use the one already implement?
#include <iostream>
using namespace std;
class BaseA {
public:
void DoSomething() {
value += 1;
std::cout<<"DoSomething():"<< value<<endl;
}
int value;
};
class BaseB {
public:
virtual void DoSomething() =0;
};
class Derived : public BaseA, public BaseB {
public:
Derived() { value = 0; }
// Compiler complains DoSomething() is not implemented.
// Can we just use BaseA.DoSomething()?
};
int main() {
Derived* obj = new Derived();
obj->DoSomething();
}

You need to define a function because DoSomething is a pure-virtual function in BaseB class. But you can call the DoSomething from BaseA class in your implementation:
void DoSomething(){
BaseA::DoSomething();
}

Related

Private function in the derived class is getting called with base class pointer [duplicate]

This question already has answers here:
Derived class Private method is getting called
(2 answers)
Public virtual function derived private in C++
(3 answers)
Closed 1 year ago.
My Code looks like this -
#include <iostream>
using namespace std;
class Base {
public:
virtual void print() {};
};
class Derived: public Base {
private:
int secret;
void print() {
cout<<"the secret is "<<this->secret<<endl;
}
public:
explicit Derived(int a): secret(a) { }
};
int main() {
Base* b1;
Derived d1(1022);
d1.print(); // Wont Work - and should not work
b1 = &d1;
b1->print(); // Works and prints the secret
return 0;
}
I don't understand how can a derived class private function be called using a base class pointer? I understand that after declaring the base class function as virtual we essentially use late binding and check the contents of the pointer before associating it with a function call but does it ignore the access specifiers in the derived classes if the base class implementation of the same function is virtual and public?

Are only virtual methods overridden [duplicate]

This question already has answers here:
Why do we need virtual functions in C++?
(27 answers)
Closed 7 years ago.
I am trying to understand virtual and pure-virtual functions in C++. I came to know that in the derived class the virtual methods are overridden with the implementation given in the derived class.
Here is the code that i used to test this :
#include <iostream>
using namespace std;
class Base
{
protected:
int a;
public :
virtual void modify ()
{
a=200;
}
void print ()
{
cout<<a<<"\n";
}
};
class Derived : public Base
{
int b;
public :
void modify()
{
a=100;
b=10;
}
void print ()
{
cout<<a<<"\t"<<b<<"\n";
}
};
int main ()
{
Base b;
b.modify ();
b.print ();
Derived d;
d.modify ();
d.print ();
return 0;
}
Output :
200
100 10
This means that the print () is also overridden along with the modify ().
My Question :
Then why do we need virtual methods at all...?
Consider this case with your code sample:
Base* b = new Derived();
b->modify();
b->print();
Even though b points to an instance of Derived, the invocation of virtual method b->modify would correctly call Derived::modify. But the invocation of b->print, which is not declared virtual, would print 200\n without the leading \t char as you have in Derived::print.

Why does an overridden virtual function get called in the base class during construction? [duplicate]

This question already has answers here:
C++ virtual function from constructor [duplicate]
(7 answers)
Closed 7 years ago.
There is a c++ program:
# include <iostream>
using namespace std;
class base
{
public:
base()
{
cout<<"base"<<endl;
f();
}
virtual void f() {
cout<<"base f"<<endl;
}
};
class derive: public base
{
public:
derive()
{
cout<<"derive"<<endl;
f();
}
void f() {
cout<<"derive f"<<endl;
}
};
int main()
{
derive d;
return 1;
}
and it outputs:
base
base f
derive
derive f
I am wondering why base f appears?
I quess in base the constrctor expands to:
cout<<"base"<<endl;
this.f();
But this should point to derive so why base f is print out?
During construction, in the baseclass constructor, the actual type of the object is base, even though it lateron continues to become a derived. The same happens during destruction, btw, and you can also verify the type using typeid.

Why derived class overloaded function hides base class function? [duplicate]

This question already has answers here:
Why does an overridden function in the derived class hide other overloads of the base class?
(4 answers)
Closed 8 years ago.
Consider the following code:
class Base
{
public:
void Fun()
{
}
};
class Derived : public Base
{
public:
void Fun(int x)
{
}
};
int main()
{
Derived d;
d.Fun(5);
d.Fun(); // Compilation error
}
On compilation, the compiler complains that there is no version of Fun() that takes zero arguments. Why is this so? I can understand compiler hiding the base class version if the function signatures match in Base and derived classes, but why is the compiler not able to resolve both functions if they take different parameters/different no. of parameters?
you have to create function body in derived class
void Fun()
{
}
Compile time first it will check function body in derived class.
You can use base class function if you are not overloading the function in derived class..
In the below example I changed the function name of derived class...
class Base
{
public:
void Fun()
{
}
};
class Derived : public Base
{
public:
void Fun1(int x)
{
}
};
int main()
{
Derived d;
d.Fun1(5);
d.Fun(); // No Compilation error
}
If you want to overload the function in derived class then you will have to override the base class function. like this
class Base
{
public:
void Fun()
{
printf("Base Class\n");
}
};
class Derived : public Base
{
public:
void Fun()
{
Base::Fun();
}
void Fun(int x)
{
printf("Derived Class\n");
}
};
int main()
{
Derived d;
d.Fun(5);
d.Fun(); // No Compilation error
}
compilation error, as there is no any 'Fun()' in derived class. You are calling Fun() by using derived class object. In CPP this is called as 'method hiding'. To solve this create function Fun() in derived class
void Fun()
{
}

Prevent inheritor class from overriding virtual function of base class [duplicate]

This question already has answers here:
Is there a way to prevent a method from being overridden in subclasses?
(14 answers)
Closed 7 years ago.
The situation is like this.
class Interface
{
public:
virtual void foo() = 0;
}
class MyClass : Interface
{
public:
virtual void bar() = 0;
private:
void foo()
{
//Some private work and checks.
bar();
};
}
I want that my user will create a class which inherit from MyClass, and they will have to implement there bar().
But how can I enfoce they wouldn't override foo()? because it's important to me to use my foo().
In C++11 you can mark the method as final to prevent it from being overriden:
class MyClass : Interface
{
public:
virtual void bar() = 0;
private:
void foo() final
{
//Some private work and checks.
bar();
};
}
As per other answer, you can use final keyword in C++11 (such facility is similar to Java's final keyword).
For C++03 code, you can use CRTP mechanism (provided if you can change the definition of Interface)
template<typename Derived>
class Interface
{
public:
void foo() // not 'virtual'
{
static_cast<Derived*>(this)->foo();
}
}
class MyClass : public Interface<MyClass>
{
public:
virtual void bar() = 0;
private:
void foo()
{
//Some private work and checks.
bar();
};
}
So now you have remove the virtualness of the foo() and the binding will happen at compile time. Remember that CRTP comes with its own limitation, so whether to use it or not is up to you.