I was reading recent discussion threads and found something interesting that someone introduced as a question, but instead of start developing the whole story from here, I’ll just challenge you with two different quiz games.
Quiz 1
- #include <iostream>
- class Foo {
- public:
- virtual void DoStuff()=0;
- };
- class Bar : public Foo {
- public:
- virtual void DoStuff(int a)=0;
- };
- class Baz : public Bar {
- public:
- void DoStuff(int a) override
- {
- std::cout << “Baz::DoStuff(int)”;
- }
- void DoStuff() override
- {
- std::cout << “Baz::DoStuff()”;
- }
- };
- int main() {
- Baz baz;
- Bar *pBar = &baz;
- pBar->DoStuff();
- }
Without using Visual Studio or your building tool of preference, just your knowledge, if we tried to run this code, what is expected to see in the console? Why?
Now let’s make a little variation and substitute the method getting no arguments for one getting char, as follows:
Quiz 2
- #include <iostream>
- class Foo {
- public:
- virtual void DoStuff(char a)=0;
- };
- class Bar : public Foo {
- public:
- virtual void DoStuff(int a)=0;
- };
- class Baz : public Bar {
- public:
- void DoStuff(int a) override
- {
- std::cout << “Baz::DoStuff(int)”;
- }
- void DoStuff(char a) override
- {
- std::cout << “Baz::DoStuff(char)”;
- }
- };
- int main() {
- Baz baz;
- Bar *pBar = &baz;
- pBar->DoStuff(‘a’);
- }
If we ran this new version, provided that it compiles with no errors, what is expected to be seen in the output console? Why?
Solutions are explained in this sequel.
0 comments