{"id":185663,"date":"2020-10-19T19:49:58","date_gmt":"2020-10-19T11:49:58","guid":{"rendered":"http:\/\/4563.org\/?p=185663"},"modified":"2020-10-19T19:49:58","modified_gmt":"2020-10-19T11:49:58","slug":"c-%e5%b0%8f%e7%99%bd%e4%b8%8d%e6%87%82%e5%b0%b1%e9%97%ae%ef%bc%8c-polymorphism-%e7%88%b6%e7%b1%bb%e8%b0%83%e7%94%a8%e5%ad%90%e7%b1%bb%e7%9a%84-function-%e9%99%90%e5%88%b6%e4%b8%8d%e5%85%81%e8%ae%b8","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=185663","title":{"rendered":"C++ \u5c0f\u767d\u4e0d\u61c2\u5c31\u95ee\uff0c polymorphism \u7236\u7c7b\u8c03\u7528\u5b50\u7c7b\u7684 function, \u9650\u5236\u4e0d\u5141\u8bb8\u4fee\u6539\u4efb\u4f55 class \u7684\u5185\u5bb9&#8230;"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  C++ \u5c0f\u767d\u4e0d\u61c2\u5c31\u95ee\uff0c polymorphism \u7236\u7c7b\u8c03\u7528\u5b50\u7c7b\u7684 function, \u9650\u5236\u4e0d\u5141\u8bb8\u4fee\u6539\u4efb\u4f55 class \u7684\u5185\u5bb9&#8230;               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : AkideLiu <\/span>  <span><i><\/i> 7<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u9898\u76ee\u5982\u4e0b\uff1a<\/p>\n<p>The code in the main() function is incomplete. Write code as described in each of the comments marked (1), (2) and (3) above. Your code for (2) and (3) should take advantage of polymorphism to keep the code simple and short.<\/p>\n<p>\u6211\u7684\u89e3\u6cd5\u5982\u4e0b\uff1a\uff08\u867d\u7136\u89e3\u51fa\u6765\u4e86\uff0c\u611f\u89c9\u6709\u70b9\u8822\uff0c\u4e4b\u524d\u5e76\u6ca1\u6709\u7528\u5230\u8fc7 static_cast \uff09<\/p>\n<p>\u91cd\u70b9\u662f\u6211\u89c9\u5f97\u6211\u7684\u89e3\u6cd5\u8ddf polymorphism \u591a\u6001\u597d\u50cf\u6ca1\u5565\u5173\u7cfb\uff0c\u662f\u6211\u7684\u601d\u8def\u6709\u95ee\u9898\u5417\uff1f<\/p>\n<pre><code>#include &lt;string&gt; #include &lt;iostream&gt;  using namespace std;  \/\/ class definition for Quadraped class Quadruped{ protected:     int height; \/\/ height of the quadruped in hands public:     Quadruped(int height); \/\/ constructor     int get_height();  \/\/ get the height };  Quadruped::Quadruped(int hands){   \/\/ implementation of constructor     height=hands; }  int Quadruped::get_height(){  \/\/ implementation of get_height     return height; }  \/\/ ------------------------------------------------------------   \/\/ abstract class for Equines class Equine:public Quadruped{ public:     Equine(int hands); \/\/ constructor     virtual void ride() =0;  \/\/ not implemented here };  \/\/ constructor Equine::Equine(int hands):Quadruped(hands){     return; \/\/ nothing more to do }  \/\/ ------------------------------------------------------------   class Horse:public Equine{ public:     Horse(int hands); \/\/ constructor     virtual void ride();  \/\/ will define };  Horse::Horse(int hands):Equine(hands){     return; \/\/ nothing more to do }  void Horse::ride(){     cout &lt;&lt; \"You ride off into the sunset\" &lt;&lt; endl; }   \/\/ ------------------------------------------------------------  class Zebra:public Equine{ private:     int stripes; public:     Zebra(int hands); \/\/ constructor     virtual void ride();  \/\/ will define     void setStripes(int strs);     int getStripes(); };  Zebra::Zebra(int hands):Equine(hands){     stripes=0;     return; }   void Zebra::ride(){     cout &lt;&lt; \"The Zebra bites you and does not let go.\" &lt;&lt; endl; }  void Zebra::setStripes(int strs) {     stripes=strs; }  int Zebra::getStripes() {     return(stripes); }  \/\/ ------------------------------------------------------------  int main(){     \/\/collection of assets     int num=4;     Quadruped* my_things[num];      \/\/ complete the code below      \/\/ (1) add two horses and two zebras to my_things, all of different heights.      for (int i = 0; i &lt; num; i++) {         if (i &lt; 2) {             my_things[i] = new Horse(100 + i * 10);         } else {             my_things[i] = new Zebra(100 + i * 10);         }     }      \/\/ (2) call ride on each of the items in my_things      Equine * temp[num] ;      for (int i = 0; i &lt; num; ++i) {          temp[i] = static_cast&lt;Equine *&gt;(my_things[i]);          temp[i]-&gt;ride();      }        \/\/ (3) add code to set the number of stripes on any zebras to 13.     for (int i = 0; i &lt; num; ++i) {          if (typeid(*temp[i]).name() == typeid(Zebra).name()) {              Zebra * temp_zebra = static_cast&lt;Zebra*&gt;(my_things[i]);              temp_zebra-&gt;setStripes(13);              cout &lt;&lt; temp_zebra-&gt;getStripes() &lt;&lt; endl;                      }     }       \/\/ clean up      for (int i = 0; i &lt; num ; ++i) {         delete temp[i];     }   } <\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff08 memcheck \u6ca1\u6709\u5185\u5b58\u6cc4\u6f0f\uff09\uff1a<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2020\/11\/20201105_5fa41df67275f.png\" alt=\"C++ \u5c0f\u767d\u4e0d\u61c2\u5c31\u95ee\uff0c polymorphism \u7236\u7c7b\u8c03\u7528\u5b50\u7c7b\u7684 function, \u9650\u5236\u4e0d\u5141\u8bb8\u4fee\u6539\u4efb\u4f55 class \u7684\u5185\u5bb9...\" \/><\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>12<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3998633\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : across <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6362 dynamic_cast\uff0ctypeid \u4e0d\u7528\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3998634\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : AkideLiu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @across dynamic_cast \u4f1a\u62a5\u9519                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3998635\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nightwitch <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             static_cast&lt;&gt;\u53ef\u4ee5\u7528\u4e8e\u5411\u4e0b\u8f6c\u6362\uff0c\u4f46\u662f\u6ca1\u6709\u8fd0\u884c\u671f\u68c0\u67e5\uff0c\u8f6c\u6362\u7684\u6b63\u786e\u6027\u9700\u8981\u7531\u7a0b\u5e8f\u5458\u81ea\u884c\u4fdd\u8bc1\u3002dynamic_cast \u53ea\u80fd\u8fd0\u7528\u5728\u6709\u865a\u51fd\u6570\u7684\u7c7b\u4e2d\uff0c\u56e0\u4e3a\u53ea\u6709\u5728\u865a\u6807\u91cc\u4f1a\u8bb0\u5f55 RTTI \u7684\u4e00\u4e9b\u4fe1\u606f\u3002<\/p>\n<p>\u7531\u4e8e\u4f60\u4e4b\u524d\u5728(2)\u5df2\u7ecf\u628a\u57fa\u7c7b\u8f6c\u5230\u4e86\u5e26\u6709\u865a\u51fd\u6570\u7684 Equine*, \u6240\u4ee5\u4f60\u7684(3)\u53ef\u4ee5\u4f7f\u7528 dynamic_cast \u5411\u4e0b\u8f6c\u6362\uff0c\u800c\u65e0\u9700\u4f7f\u7528 typeid<br \/>https:\/\/paste.ubuntu.com\/p\/VvKt87PSZd\/<\/p>\n<p>\u6b64\u5916\uff0c\u8fd9\u9053\u9898\u6709\u70b9\u5c0f\u95ee\u9898\uff0cEquine \u5e94\u8be5\u52a0\u4e0a\u865a\u6790\u6784\u51fd\u6570\uff0c\u4e0d\u7136\u5b50\u7c7b\u91cc\u7684\u8d44\u6e90\u4f1a\u51fa\u73b0\u5185\u5b58\u6cc4\u6f0f\uff0c\u4e0d\u8fc7\u4f60\u8fd9\u91cc\u7684\u5b50\u7c7b\u90fd\u6ca1\u6709\u7533\u8bf7\u52a8\u6001\u5185\u5b58\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3998636\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : user8341 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             auto zebra = dynamic_cast&lt;Zebra*&gt;(temp[i]);                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3998637\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nightwitch <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nightwitch Fix \u7b2c\u4e00\u53e5\uff1aRTTI -&gt; offset_to_top,\u7528\u4e8e dynamic_cast                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3998638\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : 786375312123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u80fd\u4e0d\u80fd\u6309\u7167 StackOverflow \u4e0a\u7684\u683c\u5f0f\uff0c\u7528\u4e00\u4e2a minimum \u7684\u4f8b\u5b50\u5199\u51fa\u6765\uff0c\u5199\u4e86\u4e00\u5927\u5806\u4e1c\u897f\u4e0d\u77e5\u9053\u4f60\u60f3\u95ee\u4ec0\u4e48                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3998639\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xuanbg <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e3b\u4f60\u7684\u4ee3\u7801\u8fd9\u4e48\u7c97\u66b4\u771f\u7684\u597d\u4e48\uff1f\u4f60\u628a Equine \u5b9a\u4e49\u6210\u62bd\u8c61\u7c7b\uff0c\u7136\u540e Horse\uff0cZebra \u4f5c\u4e3a Equine \u7684\u5b9e\u73b0\uff0c\u4e0d\u5c31\u5565\u4e8b\u90fd\u6ca1\u4e86\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3998640\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xuanbg <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @xuanbg \u554a\u554a\uff0c\u4ee3\u7801\u770b\u6f0f\u4e86\uff0c\u4e0d\u597d\u610f\u601d\u3002\u6211\u7684\u95ee\u9898\u662f\u4e3a\u4ec0\u4e48\u4e0d\u6307\u5b9a my_things \u7684\u7c7b\u578b\u4e3a Equine \uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3998641\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : AkideLiu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @786375312123 <br \/>\u8fd9\u662f\u4e2a 4 \u4e2a class \u5408\u6210\u5728\u4e00\u8d77\uff0c\u6c34\u5e73\u6709\u9650\u771f\u7684\u4e0d\u77e5\u9053\u5982\u4f55\u7f29\u77ed\u4e86<\/p>\n<p>@xuanbg \u54c8\u54c8\u54c8\u786c\u6027\u8981\u6c42\u4e0d\u5141\u8bb8\u4fee\u6539 class \u5185\u5bb9<\/p>\n<p>@user8341 <br \/>@nightwitch <br \/>\u8c22\u8c22\u4e24\u4f4d\u63d0\u5230\u7684 dynamic_cast, \u6211\u770b\u770b\u5982\u4f55\u4fee\u6b63\u4e00\u4e0b\u3002\u8bf7\u95ee\u51fa\u4e86 dynamic_cast \u8fd8\u6709\u66f4\u4f18\u89e3\u5417                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3998642\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : AkideLiu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nightwitch \u8bf7\u95ee\u4f7f\u7528 cast downgrade class \u7c7b\u578b\u548c\u591a\u6001\u8fd8\u6709\u8054\u7cfb\u5417\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3998643\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : 52coder <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6c42\u6559\u4e0b\uff0c\u8fd9\u662f\u5728\u54ea\u5237\u7684\u9898?leetcode?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3998644\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : AkideLiu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @52coder \u4e0d\u662f\u516c\u5f00\u5e73\u53f0                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>C++ \u5c0f\u767d\u4e0d\u61c2\u5c31\u95ee\uff0c polym&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/185663"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=185663"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/185663\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=185663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=185663"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=185663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}