{"id":357,"date":"2020-11-03T11:24:08","date_gmt":"2020-11-03T15:24:08","guid":{"rendered":"http:\/\/www.prglab.com\/blog\/?page_id=357"},"modified":"2020-11-03T11:29:47","modified_gmt":"2020-11-03T15:29:47","slug":"relations-between-classes","status":"publish","type":"page","link":"https:\/\/www.prglab.com\/blog\/cplusplus-tutorial\/object-oriented\/relations-between-classes","title":{"rendered":"\u7c7b\u4e4b\u95f4\u7684\u5173\u7cfb"},"content":{"rendered":"\n<h2>4.3 \u7c7b\u4e4b\u95f4\u7684\u5173\u7cfb(Relationships between classes)<\/h2>\n\n\n\n<h3>\u53cb\u5143\u51fd\u6570(Friend functions)<\/h3>\n\n\n\n<p>\u5728\u524d\u9762\u7684\u7ae0\u8282\u4e2d\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u4e86\u5bf9class\u7684\u4e0d\u540c\u6210\u5458\u5b58\u57283\u4e2a\u5c42\u6b21\u7684\u5185\u90e8\u4fdd\u62a4\uff1apublic\uff0c protected \u548c private\u3002\u5728\u6210\u5458\u4e3a protected \u548c private\u7684\u60c5\u51b5\u4e0b\uff0c\u5b83\u4eec\u4e0d\u80fd\u591f\u88ab\u4ece\u6240\u5728\u7684class\u4ee5\u5916\u7684\u90e8\u5206\u5f15\u7528\u3002\u7136\u800c\uff0c\u8fd9\u4e2a\u89c4\u5219\u53ef\u4ee5\u901a\u8fc7\u5728\u4e00\u4e2aclass\u4e2d\u4f7f\u7528\u5173\u952e\u5b57friend\u6765\u7ed5\u8fc7\uff0c\u8fd9\u6837\u6211\u4eec\u53ef\u4ee5\u5141\u8bb8\u4e00\u4e2a\u5916\u90e8\u51fd\u6570\u83b7\u5f97\u8bbf\u95eeclass\u7684protected \u548c private \u6210\u5458\u7684\u80fd\u529b\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u5b9e\u73b0\u5141\u8bb8\u4e00\u4e2a\u5916\u90e8\u51fd\u6570\u8bbf\u95eeclass\u7684private \u548c protected \u6210\u5458\uff0c\u6211\u4eec\u5fc5\u987b\u5728class\u5185\u90e8\u7528\u5173\u952e\u5b57friend\u6765\u58f0\u660e\u8be5\u5916\u90e8\u51fd\u6570\u7684\u539f\u578b\uff0c\u4ee5\u6307\u5b9a\u5141\u8bb8\u8be5\u51fd\u6570\u5171\u4eabclass\u7684\u6210\u5458\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a friend \u51fd\u6570 duplicate\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ friend functions #include &lt;iostream.h&gt; class CRectangle { int width, height; public: void set_values (int, int); int area (void) {return (width * height);} friend CRectangle duplicate (CRectangle); }; void CRectangle::set_values (int a, int b) { width = a; height = b; } CRectangle duplicate (CRectangle rectparam) { CRectangle rectres; rectres.width = rectparam.width*2; rectres.height = rectparam.height*2; return (rectres); } int main () { CRectangle rect, rectb; rect.set_values (2,3); rectb = duplicate (rect); cout &lt;&lt; rectb.area(); }<\/td><td>24<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u51fd\u6570duplicate\u662fCRectangle\u7684friend\uff0c\u56e0\u6b64\u5728\u8be5\u51fd\u6570\u4e4b\u5185\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbf\u95eeCRectangle \u7c7b\u578b\u7684\u5404\u4e2aobject\u7684\u6210\u5458 width \u548c height\u3002\u6ce8\u610f\uff0c\u5728 duplicate()\u7684\u58f0\u660e\u4e2d\uff0c\u53ca\u5176\u5728\u540e\u9762main()\u91cc\u88ab\u8c03\u7528\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5e76\u6ca1\u6709\u628aduplicate \u5f53\u4f5cclass CRectangle\u7684\u6210\u5458\uff0c\u5b83\u4e0d\u662f\u3002<\/p>\n\n\n\n<p>friend \u51fd\u6570\u53ef\u4ee5\u88ab\u7528\u6765\u5b9e\u73b0\u4e24\u4e2a\u4e0d\u540cclass\u4e4b\u95f4\u7684\u64cd\u4f5c\u3002\u5e7f\u4e49\u6765\u8bf4\uff0c\u4f7f\u7528friend \u51fd\u6570\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e4b\u5916\u7684\u65b9\u6cd5\uff0c\u56e0\u6b64\uff0c\u5982\u679c\u53ef\u80fd\uff0c\u5e94\u5c3d\u91cf\u4f7f\u7528class\u7684\u6210\u5458\u51fd\u6570\u6765\u5b8c\u6210\u8fd9\u4e9b\u64cd\u4f5c\u3002\u6bd4\u5982\u5728\u4ee5\u4e0a\u7684\u4f8b\u5b50\u4e2d\uff0c\u5c06\u51fd\u6570duplicate() \u96c6\u6210\u5728class CRectangle \u53ef\u4ee5\u4f7f\u7a0b\u5e8f\u66f4\u77ed\u3002<\/p>\n\n\n\n<h3>\u53cb\u5143\u7c7b (Friend classes)<\/h3>\n\n\n\n<p>\u5c31\u50cf\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2afriend \u51fd\u6570\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2aclass\u662f\u53e6\u4e00\u4e2a\u7684friend\uff0c\u4ee5\u4fbf\u5141\u8bb8\u7b2c\u4e8c\u4e2aclass\u8bbf\u95ee\u7b2c\u4e00\u4e2aclass\u7684 protected \u548c private \u6210\u5458\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ friend class #include &lt;iostream.h&gt; class CSquare; class CRectangle { int width, height; public: int area (void) {return (width * height);} void convert (CSquare a); }; Class CSquare { private: int side; public: void set_side (int a){side=a;} friend class CRectangle; }; void CRectangle::convert (CSquare a) { width = a.side; height = a.side; } int main () { CSquare sqr; CRectangle rect; sqr.set_side(4); rect.convert(sqr); cout &lt;&lt; rect.area(); return 0; }<\/td><td>16<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u58f0\u660e\u4e86CRectangle \u662fCSquare \u7684friend\uff0c\u56e0\u6b64CRectangle\u53ef\u4ee5\u8bbf\u95eeCSquare \u7684protected \u548c private \u6210\u5458\uff0c\u66f4\u5177\u4f53\u5730\u8bf4\uff0c\u53ef\u4ee5\u8bbf\u95eeCSquare::side\uff0c\u5b83\u5b9a\u4e49\u4e86\u6b63\u65b9\u5f62\u7684\u8fb9\u957f\u3002<\/p>\n\n\n\n<p>\u5728\u4e0a\u9762\u7a0b\u5e8f\u7684\u7b2c\u4e00\u4e2a\u8bed\u53e5\u91cc\u4f60\u53ef\u80fd\u4e5f\u770b\u5230\u4e86\u4e00\u4e9b\u65b0\u7684\u4e1c\u897f\uff0c\u5c31\u662fclass CSquare\u7a7a\u539f\u578b\u3002\u8fd9\u662f\u5fc5\u9700\u7684\uff0c\u56e0\u4e3a\u5728CRectangle \u7684\u58f0\u660e\u4e2d\u6211\u4eec\u5f15\u7528\u4e86CSquare (\u4f5c\u4e3aconvert()\u7684\u53c2\u6570)\u3002CSquare \u7684\u5b9a\u4e49\u5728CRectangle\u7684\u540e\u9762\uff0c\u56e0\u6b64\u5982\u679c\u6211\u4eec\u6ca1\u6709\u5728\u8fd9\u4e2aclass\u4e4b\u524d\u5305\u542b\u4e00\u4e2aCSquare \u7684\u58f0\u660e\uff0c\u5b83\u5728CRectangle\u4e2d\u5c31\u662f\u4e0d\u53ef\u89c1\u7684\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u8981\u8003\u8651\u5230\uff0c\u5982\u679c\u6ca1\u6709\u7279\u522b\u6307\u660e\uff0c\u53cb\u5143\u5173\u7cfb\uff08friendships\uff09\u5e76\u4e0d\u662f\u76f8\u4e92\u7684\u3002\u5728\u6211\u4eec\u7684CSquare \u4f8b\u5b50\u4e2d\uff0cCRectangle \u662f\u4e00\u4e2afriend\u7c7b\uff0c\u4f46\u56e0\u4e3aCRectangle \u5e76\u6ca1\u6709\u5bf9CSquare\u4f5c\u76f8\u5e94\u7684\u58f0\u660e\uff0c\u56e0\u6b64CRectangle \u53ef\u4ee5\u8bbf\u95eeCSquare \u7684 protected \u548cprivate \u6210\u5458\uff0c\u4f46\u53cd\u8fc7\u6765\u5e76\u4e0d\u884c\uff0c\u9664\u975e\u6211\u4eec\u5c06 CSquare \u4e5f\u5b9a\u4e49\u4e3aCRectangle\u7684 friend\u3002<\/p>\n\n\n\n<h3>\u7c7b\u4e4b\u95f4\u7684\u7ee7\u627f(Inheritance between classes)<\/h3>\n\n\n\n<p>\u7c7b\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u5f81\u662f\u7ee7\u627f\uff0c\u8fd9\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u4e00\u4e2a\u7c7b\u751f\u6210\u53e6\u4e00\u4e2a\u7c7b\u7684\u5bf9\u8c61\uff0c\u4ee5\u4fbf\u4f7f\u540e\u8005\u62e5\u6709\u524d\u8005\u7684\u67d0\u4e9b\u6210\u5458\uff0c\u518d\u52a0\u4e0a\u5b83\u81ea\u5df1\u7684\u4e00\u4e9b\u6210\u5458\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6211\u4eec\u8981\u58f0\u660e\u4e00\u7cfb\u5217\u7c7b\u578b\u7684\u591a\u8fb9\u5f62\uff0c\u6bd4\u5982\u957f\u65b9\u5f62CRectangle\u6216\u4e09\u89d2\u5f62CTriangle\u3002\u5b83\u4eec\u6709\u4e00\u4e9b\u5171\u540c\u7684\u7279\u5f81\uff0c\u6bd4\u5982\u90fd\u53ef\u4ee5\u53ea\u7528\u4e24\u6761\u8fb9\u6765\u63cf\u8ff0\uff1a\u9ad8\uff08height\uff09\u548c\u5e95\uff08base\uff09\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u7279\u70b9\u53ef\u4ee5\u7528\u4e00\u4e2a\u7c7bCPolygon \u6765\u8868\u793a\uff0c\u57fa\u4e8e\u8fd9\u4e2a\u7c7b\u6211\u4eec\u53ef\u4ee5\u5f15\u7533\u51fa\u4e0a\u9762\u63d0\u5230\u7684\u4e24\u4e2a\u7c7bCRectangle \u548c CTriangle \u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"http:\/\/www.prglab.com\/cms\/media\/images\/cplusplus\/img4-3-1.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p>\u7c7bCPolygon \u5305\u542b\u6240\u6709\u591a\u8fb9\u5f62\u5171\u6709\u7684\u6210\u5458\u3002\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u91cc\u5c31\u662f\uff1a width \u548c height\u3002\u800cCRectangle \u548c CTriangle \u5c06\u4e3a\u5b83\u7684\u5b50\u7c7b(derived classes)\u3002<\/p>\n\n\n\n<p>\u7531\u5176\u5b83\u7c7b\u5f15\u7533\u800c\u6765\u7684\u5b50\u7c7b\u7ee7\u627f\u57fa\u7c7b\u7684\u6240\u6709\u53ef\u89c6\u6210\u5458\uff0c\u610f\u601d\u662f\u8bf4\uff0c\u5982\u679c\u4e00\u4e2a\u57fa\u7c7b\u5305\u542b\u6210\u5458A \uff0c\u800c\u6211\u4eec\u5c06\u5b83\u5f15\u7533\u4e3a\u53e6\u4e00\u4e2a\u5305\u542b\u6210\u5458B\u7684\u7c7b\uff0c\u5219\u8fd9\u4e2a\u5b50\u7c7b\u5c06\u540c\u65f6\u5305\u542b A \u548c B\u3002<\/p>\n\n\n\n<p>\u8981\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u7684\u5b50\u7c7b\uff0c\u6211\u4eec\u5fc5\u987b\u5728\u5b50\u7c7b\u7684\u58f0\u660e\u4e2d\u4f7f\u7528\u5192\u53f7(colon)\u64cd\u4f5c\u7b26: \uff0c\u5982\u4e0b\u6240\u793a\uff1a<code>class derived_class_name: public base_class_name;<\/code><\/p>\n\n\n\n<p>\u8fd9\u91ccderived_class_name \u4e3a\u5b50\u7c7b(derived class)\u540d\u79f0\uff0cbase_class_name \u4e3a\u57fa\u7c7b(base class)\u540d\u79f0\u3002public \u4e5f\u53ef\u4ee5\u6839\u636e\u9700\u8981\u6362\u4e3aprotected \u6216 private\uff0c\u63cf\u8ff0\u4e86\u88ab\u7ee7\u627f\u7684\u6210\u5458\u7684\u8bbf\u95ee\u6743\u9650\uff0c\u6211\u4eec\u5728\u4ee5\u4e0b\u4f8b\u5b50\u540e\u4f1a\u5f88\u5feb\u770b\u5230\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ derived classes #include &lt;iostream.h&gt; Class CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class CRectangle: public CPolygon { public: int area (void){ return (width * height); } }; class CTriangle: public CPolygon { public: int area (void){ return (width * height \/ 2); } }; int main () { CRectangle rect; CTriangle trgl; rect.set_values (4,5); trgl.set_values (4,5); cout &lt;&lt; rect.area() &lt;&lt; endl; cout &lt;&lt; trgl.area() &lt;&lt; endl; return 0; }<\/td><td>20<br>10<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5982\u4e0a\u6240\u793a\uff0c\u7c7b CRectangle \u548c CTriangle \u7684\u6bcf\u4e00\u4e2a\u5bf9\u8c61\u90fd\u5305\u542bCPolygon\u7684\u6210\u5458\uff0c\u5373\uff1a width, height \u548c set_values()\u3002<\/p>\n\n\n\n<p>\u6807\u8bc6\u7b26protected \u4e0e private\u7c7b\u4f3c\uff0c\u5b83\u4eec\u7684\u552f\u4e00\u533a\u522b\u5728\u7ee7\u627f\u65f6\u624d\u8868\u73b0\u51fa\u6765\u3002\u5f53\u5b9a\u4e49\u4e00\u4e2a\u5b50\u7c7b\u7684\u65f6\u5019\uff0c\u57fa\u7c7b\u7684protected \u6210\u5458\u53ef\u4ee5\u88ab\u5b50\u7c7b\u7684\u5176\u5b83\u6210\u5458\u6240\u4f7f\u7528\uff0c\u7136\u800cprivate \u6210\u5458\u5c31\u4e0d\u53ef\u4ee5\u3002\u56e0\u4e3a\u6211\u4eec\u5e0c\u671bCPolygon\u7684\u6210\u5458width \u548c height\u80fd\u591f\u88ab\u5b50\u7c7bCRectangle \u548c CTriangle \u7684\u6210\u5458\u6240\u8bbf\u95ee\uff0c\u800c\u4e0d\u53ea\u662f\u88abCPolygon\u81ea\u8eab\u7684\u6210\u5458\u64cd\u4f5c\uff0c\u6211\u4eec\u4f7f\u7528\u4e86protected \u8bbf\u95ee\u6743\u9650\uff0c\u800c\u4e0d\u662f private\u3002<\/p>\n\n\n\n<p>\u4e0b\u8868\u6309\u7167\u8c01\u80fd\u8bbf\u95ee\u603b\u7ed3\u4e86\u4e0d\u540c\u8bbf\u95ee\u6743\u9650\u7c7b\u578b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>\u53ef\u4ee5\u8bbf\u95ee<\/th><th>public<\/th><th>protected<\/th><th>private<\/th><\/tr><tr><td>\u672cclass\u7684\u6210\u5458<\/td><td>yes<\/td><td>yes<\/td><td>yes<\/td><\/tr><tr><td>\u5b50\u7c7b\u7684\u6210\u5458<\/td><td>yes<\/td><td>yes<\/td><td>no<\/td><\/tr><tr><td>\u975e\u6210\u5458<\/td><td>yes<\/td><td>no<\/td><td>no<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8fd9\u91cc&#8221;\u975e\u6210\u5458&#8221;\u6307\u4ececlass\u4ee5\u5916\u7684\u4efb\u4f55\u5730\u65b9\u5f15\u7528\uff0c\u4f8b\u5982\u4ecemain()\u4e2d\uff0c\u4ece\u5176\u5b83\u7684class\u4e2d\u6216\u4ece\u5168\u57df(global)\u6216\u672c\u5730(local)\u7684\u4efb\u4f55\u51fd\u6570\u4e2d\u3002<\/p>\n\n\n\n<p>\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\uff0cCRectangle \u548cCTriangle \u7ee7\u627f\u7684\u6210\u5458\u4e0e\u57fa\u7c7bCPolygon\u62e5\u6709\u540c\u6837\u7684\u8bbf\u95ee\u9650\u5236\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">   CPolygon::width           \/\/ protected access\n   CRectangle::width         \/\/ protected access\n   CPolygon::set_values()    \/\/ public access\n   CRectangle::set_values()  \/\/ public access\n   <\/pre>\n\n\n\n<p>\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u5728\u7ee7\u627f\u7684\u65f6\u5019\u4f7f\u7528\u7684\u662fpublic\uff0c\u8bb0\u5f97\u6211\u4eec\u7528\u7684\u662f\uff1a<code>class CRectangle: public CPolygon;<\/code><\/p>\n\n\n\n<p>\u8fd9\u91cc\u5173\u952e\u5b57 public \u8868\u793a\u65b0\u7684\u7c7b(CRectangle)\u4ece\u57fa\u7c7b(CPolygon)\u6240\u7ee7\u627f\u7684\u6210\u5458\u5fc5\u987b\u83b7\u5f97\u6700\u4f4e\u7a0b\u5ea6\u4fdd\u62a4\u3002\u8fd9\u79cd\u88ab\u7ee7\u627f\u6210\u5458\u7684\u8bbf\u95ee\u9650\u5236\u7684\u6700\u4f4e\u7a0b\u5ea6\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528 protected \u6216 private\u800c\u4e0d\u662fpublic\u6765\u6539\u53d8\u3002\u4f8b\u5982\uff0cdaughter \u662fmother \u7684\u4e00\u4e2a\u5b50\u7c7b\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u5b9a\u4e49\uff1a<code>class daughter: protected mother;<\/code><\/p>\n\n\n\n<p>\u8fd9\u5c06\u4f7f\u5f97protected \u6210\u4e3adaughter \u4ecemother\u5904\u7ee7\u627f\u7684\u6210\u5458\u7684\u6700\u4f4e\u8bbf\u95ee\u9650\u5236\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u539f\u6765mother \u4e2d\u7684\u6240\u6709public \u6210\u5458\u5230daughter \u4e2d\u5c06\u4f1a\u6210\u4e3aprotected \u6210\u5458\uff0c\u8fd9\u662f\u5b83\u4eec\u80fd\u591f\u88ab\u7ee7\u627f\u7684\u6700\u4f4e\u8bbf\u95ee\u9650\u5236\u3002\u5f53\u7136\u8fd9\u5e76\u4e0d\u662f\u9650\u5236daughter \u4e0d\u80fd\u6709\u5b83\u81ea\u5df1\u7684public \u6210\u5458\u3002\u6700\u4f4e\u8bbf\u95ee\u6743\u9650\u9650\u5236\u53ea\u662f\u5efa\u7acb\u5728\u4ecemother\u4e2d \u7ee7\u627f\u7684\u6210\u5458\u4e0a\u7684\u3002<\/p>\n\n\n\n<p>\u6700\u5e38\u7528\u7684\u7ee7\u627f\u9650\u5236\u9664\u4e86public \u5916\u5c31\u662fprivate \uff0c\u5b83\u88ab\u7528\u6765\u5c06\u57fa\u7c7b\u5b8c\u5168\u5c01\u88c5\u8d77\u6765\uff0c\u56e0\u4e3a\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u9664\u4e86\u5b50\u7c7b\u81ea\u8eab\u5916\uff0c\u5176\u5b83\u4efb\u4f55\u7a0b\u5e8f\u90fd\u4e0d\u80fd\u8bbf\u95ee\u90a3\u4e9b\u4ece\u57fa\u7c7b\u7ee7\u627f\u800c\u6765\u7684\u6210\u5458\u3002\u4e0d\u8fc7\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u7ee7\u627f\u90fd\u662f\u4f7f\u7528public\u7684\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6ca1\u6709\u660e\u786e\u5199\u51fa\u8bbf\u95ee\u9650\u5236\uff0c\u6240\u6709\u7531\u5173\u952e\u5b57class \u751f\u6210\u7684\u7c7b\u88ab\u9ed8\u8ba4\u4e3aprivate \uff0c\u800c\u6240\u6709\u7531\u5173\u952e\u5b57struct \u751f\u6210\u7684\u7c7b\u88ab\u9ed8\u8ba4\u4e3apublic\u3002<\/p>\n\n\n\n<h3>\u4ec0\u4e48\u662f\u4ece\u57fa\u7c7b\u4e2d\u7ee7\u627f\u7684? (What is inherited from the base class?)<\/h3>\n\n\n\n<p>\u7406\u8bba\u4e0a\u8bf4\uff0c\u5b50\u7c7b(drived class)\u7ee7\u627f\u4e86\u57fa\u7c7b(base class)\u7684\u6240\u6709\u6210\u5458\uff0c\u9664\u4e86\uff1a<\/p>\n\n\n\n<ul><li>\u6784\u9020\u51fd\u6570Constructor \u548c\u6790\u6784\u51fd\u6570destructor<\/li><li>operator=() \u6210\u5458<\/li><li>friends<\/li><\/ul>\n\n\n\n<p>\u867d\u7136\u57fa\u7c7b\u7684\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570\u6ca1\u6709\u88ab\u7ee7\u627f\uff0c\u4f46\u662f\u5f53\u4e00\u4e2a\u5b50\u7c7b\u7684object\u88ab\u751f\u6210\u6216\u9500\u6bc1\u7684\u65f6\u5019\uff0c\u5176\u57fa\u7c7b\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570 (\u5373\uff0c\u6ca1\u6709\u4efb\u4f55\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570)\u548c\u6790\u6784\u51fd\u6570\u603b\u662f\u88ab\u81ea\u52a8\u8c03\u7528\u7684\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u57fa\u7c7b\u6ca1\u6709\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u6216\u4f60\u5e0c\u671b\u5f53\u5b50\u7c7b\u751f\u6210\u65b0\u7684object\u65f6\uff0c\u57fa\u7c7b\u7684\u67d0\u4e2a\u91cd\u8f7d\u7684\u6784\u9020\u51fd\u6570\u88ab\u8c03\u7528\uff0c\u4f60\u9700\u8981\u5728\u5b50\u7c7b\u7684\u6bcf\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u7684\u5b9a\u4e49\u4e2d\u6307\u5b9a\u5b83\uff1a<code>derived_class_name (parameters) : base_class_name (parameters) {}<\/code><\/p>\n\n\n\n<p>\u4f8b\u5982 \uff08\u6ce8\u610f\u7a0b\u5e8f\u4e2d\u9ed1\u4f53\u7684\u90e8\u5206\uff09\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ constructors and derivated classes #include &lt;iostream.h&gt; class mother { public: mother () { cout &lt;&lt; &#8220;mother: no parameters\\n&#8221;; } mother (int a) { cout &lt;&lt; &#8220;mother: int parameter\\n&#8221;; } }; class daughter : public mother { public: daughter (int a) { cout &lt;&lt; &#8220;daughter: int parameter\\n\\n&#8221;; } }; class son : public mother { public: son (int a) : mother (a) { cout &lt;&lt; &#8220;son: int parameter\\n\\n&#8221;; } }; int main () { daughter cynthia (1); son daniel(1); return 0; }<\/td><td>mother: no parameters<br>daughter: int parameter<br><br>mother: int parameter<br>son: int parameter<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u89c2\u5bdf\u5f53\u4e00\u4e2a\u65b0\u7684daughter object\u751f\u6210\u7684\u65f6\u5019mother\u7684\u54ea\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u88ab\u8c03\u7528\u4e86\uff0c\u800c\u5f53\u65b0\u7684son object\u751f\u6210\u7684\u65f6\u5019\uff0c\u53c8\u662f\u54ea\u4e00\u4e2a\u88ab\u8c03\u7528\u4e86\u3002\u4e0d\u540c\u7684\u6784\u9020\u51fd\u6570\u88ab\u8c03\u7528\u662f\u56e0\u4e3adaughter \u548c son\u7684\u6784\u9020\u51fd\u6570\u7684\u5b9a\u4e49\u4e0d\u540c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">   daughter (int a)          \/\/ \u6ca1\u6709\u7279\u522b\u5236\u5b9a\uff1a\u8c03\u7528\u9ed8\u8ba4constructor\n   son (int a) : mother (a)  \/\/ \u6307\u5b9a\u4e86constructor: \u8c03\u7528\u88ab\u6307\u5b9a\u7684\u6784\u9020\u51fd\u6570\n   <\/pre>\n\n\n\n<h3>\u591a\u91cd\u7ee7\u627f(Multiple inheritance)<\/h3>\n\n\n\n<p>\u5728C++ \u4e2d\uff0c\u4e00\u4e2aclass\u53ef\u4ee5\u4ece\u591a\u4e2aclass\u4e2d\u7ee7\u627f\u5c5e\u6027\u6216\u51fd\u6570\uff0c\u53ea\u9700\u8981\u5728\u5b50\u7c7b\u7684\u58f0\u660e\u4e2d\u7528\u9017\u53f7\u5c06\u4e0d\u540c\u57fa\u7c7b\u5206\u5f00\u5c31\u53ef\u4ee5\u4e86\u3002\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u6709\u4e00\u4e2a\u7279\u6b8a\u7684class COutput \u53ef\u4ee5\u5b9e\u73b0\u5411\u5c4f\u5e55\u6253\u5370\u7684\u529f\u80fd\uff0c\u6211\u4eec\u540c\u65f6\u5e0c\u671b\u6211\u4eec\u7684\u7c7bCRectangle \u548c CTriangle \u5728CPolygon \u4e4b\u5916\u8fd8\u7ee7\u627f\u4e00\u4e9b\u5176\u5b83\u7684\u6210\u5458\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>class CRectangle: public CPolygon, public COutput {<br>class CTriangle: public CPolygon, public COutput {<\/code><\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>\/\/ multiple inheritance #include &lt;iostream.h&gt; class CPolygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b;} }; class COutput { public: void output (int i); }; void COutput::output (int i) { cout &lt;&lt; i &lt;&lt; endl; } class CRectangle: public CPolygon, public COutput { public: int area (void) { return (width * height); } }; class CTriangle: public CPolygon, public COutput { public: int area (void) { return (width * height \/ 2); } }; int main () { CRectangle rect; CTriangle trgl; rect.set_values (4,5); trgl.set_values (4,5); rect.output (rect.area()); trgl.output (trgl.area()); return 0; }<\/td><td>20<br>10<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>4.3 \u7c7b\u4e4b\u95f4\u7684\u5173\u7cfb(Relationships between classes) \u53cb\u5143\u51fd\u6570(Friend functions) \u5728\u524d\u9762\u7684\u7ae0\u8282\u4e2d\u6211\u4eec\u5df2\u7ecf\u770b\u5230\u4e86\u5bf9class\u7684\u4e0d\u540c\u6210\u5458\u5b58\u57283\u4e2a\u5c42\u6b21\u7684\u5185\u90e8\u4fdd\u62a4\uff1apublic\uff0c protected \u548c private\u3002\u5728\u6210\u5458\u4e3a protected \u548c private\u7684\u60c5\u51b5\u4e0b\uff0c\u5b83\u4eec\u4e0d\u80fd\u591f\u88ab\u4ece\u6240\u5728\u7684class\u4ee5\u5916\u7684\u90e8\u5206\u5f15\u7528\u3002\u7136\u800c\uff0c\u8fd9\u4e2a\u89c4\u5219\u53ef\u4ee5\u901a\u8fc7\u5728\u4e00\u4e2aclass\u4e2d\u4f7f\u7528\u5173\u952e\u5b57friend\u6765\u7ed5\u8fc7\uff0c\u8fd9\u6837\u6211\u4eec\u53ef\u4ee5\u5141\u8bb8\u4e00\u4e2a\u5916\u90e8\u51fd\u6570\u83b7\u5f97\u8bbf\u95eeclass\u7684protected \u548c private \u6210\u5458\u7684\u80fd\u529b\u3002 \u4e3a\u4e86\u5b9e\u73b0\u5141\u8bb8\u4e00\u4e2a\u5916\u90e8\u51fd\u6570\u8bbf\u95eeclass\u7684private \u548c protected \u6210\u5458\uff0c\u6211\u4eec\u5fc5\u987b\u5728class\u5185\u90e8\u7528\u5173\u952e\u5b57friend\u6765\u58f0\u660e\u8be5\u5916\u90e8\u51fd\u6570\u7684\u539f\u578b\uff0c\u4ee5\u6307\u5b9a\u5141\u8bb8\u8be5\u51fd\u6570\u5171\u4eabclass\u7684\u6210\u5458\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\u6211\u4eec\u58f0\u660e\u4e86\u4e00\u4e2a friend \u51fd\u6570 duplicate\uff1a \/\/ friend functions #include &lt;iostream.h&gt; class CRectangle { int width, height; public: void set_values (int, int); int area (void) {return (width * height);} friend CRectangle duplicate (CRectangle); }; void CRectangle::set_values (int a, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":351,"menu_order":2,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/357"}],"collection":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/comments?post=357"}],"version-history":[{"count":1,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/357\/revisions"}],"predecessor-version":[{"id":358,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/357\/revisions\/358"}],"up":[{"embeddable":true,"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/pages\/351"}],"wp:attachment":[{"href":"https:\/\/www.prglab.com\/blog\/wp-json\/wp\/v2\/media?parent=357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}