Which method will this inherited class call (virtual function)? c++ - c++

Say I have a base class
class Base
{
public:
void A() {do stuff};
void B() {A(); do stuff};
};
and a derived class
class Derived : public Base
{
public:
void A() {do things}
};
Derived derived1;
derived1.B();
will B call A of the base class or the A of the derived class?
I suspect B will call the A of the base class, and that in order for it to use the new redefined A, I have to make it virtual:
class Base
{
public:
virtual void A() {do stuff};
void B() {A(); do stuff};
};
Is that correct? Is that what virtual functions are used for?

Yes, correct on both counts...

try this
class Base
{
public:
virtual void A() {do stuff};
void B() { do stuff};
};
class Derived : public Base
{
public:
void A() {do things}
void B() {do things}
};
and
Derived d;
Base *p = &d;
p->A();
p->B();
function B will not be overrided

Related

Calling derived constructor from base static method

I would like to know if there would be a way to instantiate a derived class from a static method defined on its base class. For now I have wrapped method in a templated struct. Ideally I would like to do something like this:
class Base {
public:
Base(){}
virtual ~Base(){}
static void do_something() {
Base b(); // should call Derived() instead
// use our object
}
}
class Derived: public Base {
public:
Derived(): Base() {}
~Derived(){}
}
// The idea would be to use the derived static method like that
int main(){
Derived::do_something();
}

convert an object created with base class ctor to a derived class

I have the following classes:
class Base {
public:
virtual ~Base(){}
Base() {}
virtual void foo() = 0;
};
class Derived : public Base {
public:
virtual ~Derived(){}
Derived() : Base() {}
void foo() { printf("derived : foo\n"); }
};
class IInterface {
public:
virtual ~IInterface() {}
virtual void bar() = 0;
};
class C : public Derived, public IInterface {
public:
virtual ~C(){}
C() : Derived(){}
void bar() { printf("C : bar\n"); }
};
now I have a bunch of Derived* objects and I want to apply different interfaces on them :
Derived* d = new Derived();
C* c = dynamic_cast<C*>(d);
c->bar();
c->foo();
dynamic_cast returns nullptr and with c-style cast i get seg fault.
is there anyway to achieve this?
note that my objects are already created with Derived ctor.
i just want to treat them differently using Interfaces
The only way to achive this is to create a new object and move the data over from the old object.
Try encapsulating the behaviour that needs to change at runtime. Instead of inheriting from the IInterface, you have a member variable that is an IInterface pointer. Then instead of overriding bar in the child class, you pass the call to bar through to whatever is being pointed at. This allows modular behavior that looks just like polymorphism, but is more flexible:
class IInterface {
public:
virtual ~IInterface() {}
virtual void bar() = 0;
};
class Derived : public Base {
public:
IInterface* m_bar;
virtual ~Derived(){}
Derived() : Base(){}
void bar() {return m_bar->bar(); }
void foo() { printf("derived : foo\n"); }
};
You then derive and create IInterface objects and can associate any of them with Derived objects.

Calling derived class method in base class

We can call "Base class methods" in derived class. but Can we call derived class method in base class function?
#include<iostream.h>
class base {
public:
void print()
{
cout<<"base class";
}
};
class derived : public base
{
public:
void print()
{
cout<<"derived class";
}
};
How to call this derived class print() function in Base class?
You cannot call a normal method defined in derived class from base class. What you can do is add a virtual (possibly pure) method in base class, provide implementation in derived class. You can call this method from base.
class Base{
public:
virtual void foo(){cout<<"Hello";};
void bar() { foo();}
};
class Derived: public Base{
public:
void foo() override{ cout<<"Hi";}
};
int main() {
Base* b1 = new Derived();
b1->bar(); //will call Derived::foo()
Base* b2=new Base();
b2->bar(); // will call Base::foo()
}
You can call derived class's method from base class method on a derived class object, if it's a virtual method:
class base {
public:
void doSomething() {
print();
}
virtual void print() {
cout<<"base class";
}
};
class derived : public base {
public:
virtual void print() {
cout<<"derived class";
}
};
int main() {
derived d;
base* pb = &d;
pb->doSomething();
}
It is known as Template method pattern.
If you know for certain that all instantiations of base will be as the base class of derived (as might be the case in a use of the Curiously Recurring Template Pattern), you can simply static_cast this to derived*:
#include <iostream>
class base {
public:
void call_derived_print();
void print()
{
std::cout<<"base class";
}
};
class derived : public base
{
public:
void print()
{
std::cout<<"derived class";
}
};
void base::call_derived_print() {
//undefined behaviour unless the most-derived type of `*this`
//is `derived` or is a subtype of `derived`.
static_cast<derived*>(this)->print();
}

Calling a subclass from mainclass

I have a base class Board_S and 10 other classes that inherit the base Board_S class.
I have an object for Board_S which calls the Board_S class and its function which should call one more function from a specific sub class.
I am getting the error : Class does not name a type;
How to do I call the subclass function from this main Class.
Please Help
The only way to do this correctly is to declare a virtual function in your base class and implement it in your derived class.
class A
{
public:
A() {}
virtual ~A() {}
virtual void func() {}
void CallFunc() { func(); }
};
class B : public A
{
public:
B() {}
virtual ~B() {}
virtual void func() {} // overrides it
};
int main()
{
A a;
A* pB = new B;
a.CallFunc(); // will call the base class version of func()
pB->CallFunc(); // will call the derived class version of func()
delete pB;
}
If you don't need the function overridden in a derived class (and the function isn't declared as a pure virtual function), you do not override it.

Virtual inheritance and same name members

I have the the following classes:
class Base
{
public:
Base() { x = 3; }
int x;
virtual void foo() {};
};
class Med1 : public virtual Base
{
public:
int x;
Med1() { x = 4; }
virtual void foo() {};
};
class Med2 : public virtual Base
{
public:
virtual void goo() {};
virtual void foo() {};
};
class Der : public Med1, public Med2
{
public:
Der() {}
virtual void foo() {};
virtual void goo() {};
};
And the following code:
Base* d = new Der;
d->foo();
cout << d->x;
Output:
3
Why is that? Med1 constructor is called after Base constructor. I'm guessing it's setting Med1::x, and not Base::x, but why is Der::x the same as Base::x and not Med1::x. Why is there no ambiguity?
d is a pointer to Base, so d->x refers unambiguously to Base::x. There would only be an ambiguity if it were a pointer to Der.
Since it is pointer to Base x will be of Base.
And the order of constructor is super class and then derived class. So constructor of Base class is called first and then of Der.
The variable x is not virtual, so the compiler has to scratch its head and say - hang on you Base. Therefore it goes to Base->x