#include "stdafx.h"
#include <iostream>
using namespace std;
class Base
{ // Define a base class
public:
virtual void Func1() = 0;
virtual void Func2();
virtual void Func3();
void Func4();
};
class A : public Base
{ // Class A derives from Base
public:
void Func2();
virtual void Func4();
};
class B : public A
{// Class B derives from A
public:
virtual void Func1();
void Func2();
};
class C : public B
{ // Class C derives from B
public:
virtual void Func1();
virtual void Func4();
};
// Base Class Methods
void Base::Func2(){ cout << "Hello from Base::Func2()" << endl;}
void Base::Func3(){cout << "Hello from Base::Func3()" << endl;
Func1(); // DON’T MISS THIS CALL IN YOUR ANSWER
}
void Base::Func4(){ cout << "Hello from Base::Func4()" << endl;}
// Class A Methods
void A::Func2() { cout << "Hello from A::Func2()" << endl; }
void A::Func4() { cout << "Hello from A::Func4()" << endl; }
// Class B Methods
void B::Func1() { cout << "Hello from B::Func1()" << endl; }
void B::Func2() { cout << "Hello from B::Func2()" << endl; }
// Class C Methods
void C::Func1() { cout << "Hello from C::Func1()" << endl; }
void C::Func4() { cout << "Hello from C::Func4()" << endl; }
void TestFuncRef(Base& x)
{
x.Func1();
x.Func2();
x.Func3();
x.Func4();
}
void TestFuncVal(B x)
{
x.Func1();
x.Func2();
x.Func3();
x.Func4();
}
int main()
{
B b;
C c;
TestFuncRef(c);
TestFuncRef(b);
TestFuncVal(c);
}
So right now I have the above code that focuses on inheritance and polymorphism. I need some help with understanding how parts of the outputs are achieved.
The outputs are:
Hello from C::Func1()
Hello from B::Func2()
Hello from Base::Func3()
Hello from C::Func1()
Hello from Base::Func4()
Hello from B::Func1()
Hello from B::Func2()
Hello from Base::Func3()
Hello from B::Func1()
Hello from Base::Func4()
Hello from B::Func1()
Hello from B::Func2()
Hello from Base::Func3()
Hello from B::Func1()
Hello from A::Func4()
I don't understand how there are always some call between the base Func3 and Base func4 calls. Such as the following
Hello from Base::Func3()
Hello from C::Func1()
Hello from Base::Func4()
and
Hello from Base::Func3()
Hello from B::Func1()
Hello from Base::Func4()
Also could someone please explain to me how the TestFuncRef and TestFuncVal functions work? I don't get the outputs from those calls either. Thank you so much.