{"id":3533,"date":"2011-01-25T10:35:00","date_gmt":"2011-01-25T10:35:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/vcblog\/2011\/01\/25\/sharing-the-heritage-quiz-game\/"},"modified":"2019-02-18T18:45:17","modified_gmt":"2019-02-18T18:45:17","slug":"sharing-the-heritage-quiz-game","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/cppblog\/sharing-the-heritage-quiz-game\/","title":{"rendered":"Sharing the Heritage (Quiz Game)"},"content":{"rendered":"<p>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&rsquo;ll just challenge you&nbsp;with two different quiz games.<\/p>\n<div class=\"wlWriterEditableSmartContent\" id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:d3336cdb-beaf-45a8-a9fd-ca56b63b0133\" style=\"padding-bottom: 0px;margin: 0px;padding-left: 0px;padding-right: 0px;float: none;padding-top: 0px\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Quiz 1<\/div>\n<div style=\"background: #ddd;overflow: auto\">\n<ol style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\">\n<li><span style=\"color:#0000ff\">#include<\/span> <span style=\"color:#a31515\">&lt;iostream&gt;<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li><span style=\"color:#0000ff\">class<\/span> Foo {<\/li>\n<li style=\"background: #f3f3f3\"><span style=\"color:#0000ff\">public<\/span>:<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#0000ff\">virtual<\/span> <span style=\"color:#0000ff\">void<\/span> DoStuff()=0;<\/li>\n<li style=\"background: #f3f3f3\">};<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\"><span style=\"color:#0000ff\">class<\/span> Bar : <span style=\"color:#0000ff\">public<\/span> Foo {<\/li>\n<li><span style=\"color:#0000ff\">public<\/span>:<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#0000ff\">virtual<\/span> <span style=\"color:#0000ff\">void<\/span> DoStuff(<span style=\"color:#0000ff\">int<\/span> a)=0;<\/li>\n<li>};<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li><span style=\"color:#0000ff\">class<\/span> Baz : <span style=\"color:#0000ff\">public<\/span> Bar {<\/li>\n<li style=\"background: #f3f3f3\"><span style=\"color:#0000ff\">public<\/span>:<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#0000ff\">void<\/span> DoStuff(<span style=\"color:#0000ff\">int<\/span> a) <span style=\"color:#0000ff\">override<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;{<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout &lt;&lt; <span style=\"color:#a31515\">&#8220;Baz::DoStuff(int)&#8221;<\/span>;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;}<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#0000ff\">void<\/span> DoStuff() <span style=\"color:#0000ff\">override<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;{<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout &lt;&lt; <span style=\"color:#a31515\">&#8220;Baz::DoStuff()&#8221;<\/span>;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;}<\/li>\n<li style=\"background: #f3f3f3\">};<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\"><span style=\"color:#0000ff\">int<\/span> main() {<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;Baz baz;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;Bar *pBar = &amp;baz;<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;pBar-&gt;DoStuff();<\/li>\n<li>}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p>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?<\/p>\n<p>Now let&rsquo;s make a little variation and substitute the method getting no arguments for one getting char, as follows:<\/p>\n<div class=\"wlWriterEditableSmartContent\" id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:690373d8-5357-4a27-bc37-f7d5fd303919\" style=\"padding-bottom: 0px;margin: 0px;padding-left: 0px;padding-right: 0px;float: none;padding-top: 0px\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Quiz 2<\/div>\n<div style=\"background: #ddd;overflow: auto\">\n<ol style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\">\n<li><span style=\"color:#0000ff\">#include<\/span> <span style=\"color:#a31515\">&lt;iostream&gt;<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li><span style=\"color:#0000ff\">class<\/span> Foo {<\/li>\n<li style=\"background: #f3f3f3\"><span style=\"color:#0000ff\">public<\/span>:<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#0000ff\">virtual<\/span> <span style=\"color:#0000ff\">void<\/span> DoStuff(<span style=\"color:#0000ff\">char<\/span> a)=0;<\/li>\n<li style=\"background: #f3f3f3\">};<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\"><span style=\"color:#0000ff\">class<\/span> Bar : <span style=\"color:#0000ff\">public<\/span> Foo {<\/li>\n<li><span style=\"color:#0000ff\">public<\/span>:<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#0000ff\">virtual<\/span> <span style=\"color:#0000ff\">void<\/span> DoStuff(<span style=\"color:#0000ff\">int<\/span> a)=0;<\/li>\n<li>};<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li><span style=\"color:#0000ff\">class<\/span> Baz : <span style=\"color:#0000ff\">public<\/span> Bar {<\/li>\n<li style=\"background: #f3f3f3\"><span style=\"color:#0000ff\">public<\/span>:<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#0000ff\">void<\/span> DoStuff(<span style=\"color:#0000ff\">int<\/span> a) <span style=\"color:#0000ff\">override<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;{<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout &lt;&lt; <span style=\"color:#a31515\">&#8220;Baz::DoStuff(int)&#8221;<\/span>;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;}<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;<span style=\"color:#0000ff\">void<\/span> DoStuff(<span style=\"color:#0000ff\">char<\/span> a) <span style=\"color:#0000ff\">override<\/span><\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;{<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;std::cout &lt;&lt; <span style=\"color:#a31515\">&#8220;Baz::DoStuff(char)&#8221;<\/span>;<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;}<\/li>\n<li style=\"background: #f3f3f3\">};<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\"><span style=\"color:#0000ff\">int<\/span> main() {<\/li>\n<li>&nbsp;&nbsp;&nbsp;&nbsp;Baz baz;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;Bar *pBar = &amp;baz;<\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\">&nbsp;&nbsp;&nbsp;&nbsp;pBar-&gt;DoStuff(<span style=\"color:#a31515\">&#8216;a&#8217;<\/span>);<\/li>\n<li>}<\/li>\n<\/ol>\n<\/div>\n<\/div>\n<\/div>\n<p>If we ran this new version, provided that it compiles with no errors, what is expected to be seen in the output console? Why?<\/p>\n<p>&nbsp;<\/p>\n<p>Solutions are explained in this <a href=\"http:\/\/blogs.msdn.com\/b\/vcblog\/archive\/2011\/01\/28\/10121387.aspx\">sequel<\/a>.<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&rsquo;ll just challenge you&nbsp;with two different quiz games. Quiz 1 #include &lt;iostream&gt; &nbsp; class Foo { public: &nbsp;&nbsp;&nbsp;&nbsp;virtual void DoStuff()=0; }; &nbsp; class Bar : public Foo { public: [&hellip;]<\/p>\n","protected":false},"author":293,"featured_media":35994,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[100],"class_list":["post-3533","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cplusplus","tag-c-language"],"acf":[],"blog_post_summary":"<p>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&rsquo;ll just challenge you&nbsp;with two different quiz games. Quiz 1 #include &lt;iostream&gt; &nbsp; class Foo { public: &nbsp;&nbsp;&nbsp;&nbsp;virtual void DoStuff()=0; }; &nbsp; class Bar : public Foo { public: [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/3533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/users\/293"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/comments?post=3533"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/posts\/3533\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media\/35994"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/media?parent=3533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/categories?post=3533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/cppblog\/wp-json\/wp\/v2\/tags?post=3533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}