Sharing the Heritage (Quiz Game)

Diego Dagum - MSFT

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
  1. #include <iostream>
  2.  
  3. class Foo {
  4. public:
  5.     virtual void DoStuff()=0;
  6. };
  7.  
  8. class Bar : public Foo {
  9. public:
  10.     virtual void DoStuff(int a)=0;
  11. };
  12.  
  13. class Baz : public Bar {
  14. public:
  15.     void DoStuff(int a) override
  16.     {
  17.         std::cout << “Baz::DoStuff(int)”;
  18.     }
  19.  
  20.     void DoStuff() override
  21.     {
  22.         std::cout << “Baz::DoStuff()”;
  23.     }
  24. };
  25.  
  26. int main() {
  27.     Baz baz;
  28.     Bar *pBar = &baz;
  29.  
  30.     pBar->DoStuff();
  31. }

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
  1. #include <iostream>
  2.  
  3. class Foo {
  4. public:
  5.     virtual void DoStuff(char a)=0;
  6. };
  7.  
  8. class Bar : public Foo {
  9. public:
  10.     virtual void DoStuff(int a)=0;
  11. };
  12.  
  13. class Baz : public Bar {
  14. public:
  15.     void DoStuff(int a) override
  16.     {
  17.         std::cout << “Baz::DoStuff(int)”;
  18.     }
  19.  
  20.     void DoStuff(char a) override
  21.     {
  22.         std::cout << “Baz::DoStuff(char)”;
  23.     }
  24. };
  25.  
  26. int main() {
  27.     Baz baz;
  28.     Bar *pBar = &baz;
  29.  
  30.     pBar->DoStuff(‘a’);
  31. }

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

Discussion is closed.

Feedback usabilla icon