Are only virtual methods overridden [duplicate] - c++

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.

Related

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

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();
}

Virtual inheritance ambiguous function [duplicate]

This question already has answers here:
Virtual Inheritance: Error: no unique final overrider
(2 answers)
Multiple (diamond) inheritance compiles without "virtual", but doesn't with
(2 answers)
Closed 6 years ago.
I have this part of code
#include <iostream>
using namespace std;
class A {
public:
int i;
A(){i = 0; }
virtual void f() { cout << i; }
};
class B1 : virtual public A {
public:
B1() { f(); }
void f() { cout << i+10; }
};
class B2 : virtual public A {
public:
B2() { f(); }
void f() { cout << i+1; }
};
class C : public B1, public B2 {
public:
C() {}
};
void main(){
C c;
//c.A::f();
}
First, I understand the main idea behind using virtual inheritance (diamond) in order to create only one A object in memory.
In this example, I get compilation error in C class:
override of virtual function "A::f" is ambiguous
If I remove the virtual inheritance. The code compiles, there is no error in class C as before.
If I remove the comment from the last code line it still compiles. I understand that in this case the f() function that will be executed is the one from the first class that C inherits from.
Now, if I replace c.A::f() with c.f() I get a compilation error at this specific line.
Can someone please explain this behaviour and the differences between these cases?
The problem is with C not with your expression call. As f is virtual and redefined twice in B1 and B2 then the C class is malformed because a call to fwould be ambiguous (which override to choose?). Add an override of f in Cand everything will be ok.
Within the class C you must choose which variant of f to usr. That is, using B1::f; or using B2::f.

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.

How in C++ multiple inheritance choose of which base class method will be inherited? [duplicate]

This question already has answers here:
Multiple inheritance + virtual function mess
(6 answers)
Closed 7 years ago.
Consider following simple example:
#include <iostream>
using namespace std;
class A {
public:
virtual void foo() {
cout<<"A"<<endl;
}
};
class B: public virtual A {
public:
virtual void foo(){
cout<<"B"<<endl;
}
};
class C : public virtual A {
public:
virtual void foo(){
cout<<"C"<<endl;
}
};
class D : public B, public C {
public:
void print(){
foo();
}
};
int main () {
D d;
d.print();
return 0;
}
This code will not work, because call to foo is ambigous. But question is:
How can I define which methods from which classes should be inherited? And if I have the such opportunity?
Disambiguate the call to the function. Take the following example:
class A { virtual void foo(); };
class B { virtual void foo(); };
class C : public A ,public B { void foo(); };
To call either foo from A, B or even from the child class: C do
C *obj = new C;
obj->foo();
obj->A::foo();
obj->B::foo();
Source/inspiration: http://www.cprogramming.com/tutorial/multiple_inheritance.html

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()
{
}