1.\u7c7b\u5916\u8fd0\u7b97\u7b26\u91cd\u8f7d<\/strong><\/div>\nclass Point {\r\nprivate:\r\n int x,y;\r\npublic:\r\n \/\/ \u7cfb\u7edfC++\u6e90\u7801\uff0c\u5927\u91cf\u4f7f\u7528\u6b64\u65b9\u5f0f :x(x), y(y)\r\n Point(int x, int y) :x(x), y(y) {}\r\n \/\/ set get \u51fd\u6570\r\n void setX(int x) {\r\n this->x = x;\r\n }\r\n void setY(int y) {\r\n this->y = y;\r\n }\r\n int getX() {\r\n return this->x;\r\n }\r\n int getY() {\r\n return this->y;\r\n }\r\n};\r\n\/*\u7c7b\u5916\u8fd0\u7b97\u7b26\u91cd\u8f7d\r\n * \u5728\u771f\u5b9e\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u57fa\u672c\u4e0a\u90fd\u662f\u5199\u5728\u7c7b\u7684\u91cc\u9762\u7684\uff0c\u5916\u90e8\u662f\u4e0d\u80fd\u83b7\u53d6\u5185\u90e8\u7684\u79c1\u6709\u6210\u5458\u7684\r\n * *\/\r\nPoint operator + (Point point1,Point point2){\r\n int x = point1.getX() + point2.getX();\r\n int y = point1.getY() + point2.getY();\r\n \r\n Point res(x, y);\r\n return res;\r\n}\r\nint main(){\r\n Point pointA(10,20);\r\n Point pointB(10,20);\r\n Point pointC=pointA+pointB;\r\n cout << pointC.getX() << \" , \" << pointC.getY() << endl;\r\n}<\/pre>\n\u65e5\u5fd7\u8f93\u51fa\uff1a\r\n20 , 40<\/pre>\n\u4e24\u4e2a\u5bf9\u8c61\u505a+\u6cd5\u8fd0\u7b97\u5c31\u662f\u6267\u884c\u4e86\u8fd0\u7b97\u7b26\u91cd\u8f7d\u51fd\u6570<\/p>\n
2.\u7c7b\u5185\u90e8\u8fd0\u7b97\u7b26\u53f7\u91cd\u8f7d<\/strong><\/div>\nclass Point {\r\nprivate:\r\n int x,y;\r\npublic:\r\n Point(){}\r\n \/\/ \u7cfb\u7edfC++\u6e90\u7801\uff0c\u5927\u91cf\u4f7f\u7528\u6b64\u65b9\u5f0f :x(x), y(y)\r\n Point(int x, int y) :x(x), y(y) {}\r\n \/\/ set get \u51fd\u6570\r\n void setX(int x) {\r\n this->x = x;\r\n }\r\n void setY(int y) {\r\n this->y = y;\r\n }\r\n int getX() {\r\n return this->x;\r\n }\r\n int getY() {\r\n return this->y;\r\n }\r\n \/*\r\n * \u5e38\u91cf\u5f15\u7528\uff1a\u4e0d\u5141\u8bb8\u4fee\u6539\uff0c\u53ea\u8bfb\u6a21\u5f0f\r\n * & \u6027\u80fd\u7684\u63d0\u9ad8\uff0c\u5982\u679c\u6ca1\u6709& \u8fd0\u884c+ \u6784\u5efa\u65b0\u7684\u526f\u672c\uff0c\u4f1a\u6d6a\u8d39\u6027\u80fd\r\n * \u5982\u679c\u589e\u52a0\u4e86& \u5f15\u7528\u662f\u7ed9\u8fd9\u5757\u5185\u5b58\u7a7a\u95f4\u53d6\u4e00\u4e2a\u522b\u540d\u800c\u5df2\r\n * *\/\r\n Point operator + (const Point & point){\r\n int x=this->x+point.x;\r\n int y=this->y+point.y;\r\n return Point(x,y);\r\n }\r\n Point operator - (const Point & point){\r\n int x=this->x-point.x;\r\n int y=this->y-point.y;\r\n return Point(x,y);\r\n }\r\n void operator ++() { \/\/ ++\u5bf9\u8c61\r\n this->x = this->x + 1;\r\n this->y = this->y + 1;\r\n }\r\n void operator ++ (int) { \/\/ \u5bf9\u8c61++\r\n this->x = this->x + 1;\r\n this->y = this->y + 1;\r\n }\r\n \/*\u91cd\u8f7d<< \u8f93\u51fa\u8fd0\u7b97\u7b26\u53f7\r\n * istream \u8f93\u5165 \u7cfb\u7edf\u7684\r\n * ostream \u8f93\u51fa \u7cfb\u7edf\u7684\r\n * *\/\r\n \/* friend void operator << (ostream & _START,Point &point){\r\n _START << \" \u5f00\u59cb\u8f93\u51fa \" << point.x << \" : \" << point.y << \" \u7ed3\u675f\u4e86 \" << endl;\r\n }*\/\r\n \/*\u591a\u4e2a<< \u8fde\u7740\u5199 *\/\r\n friend ostream & operator << (ostream & _START,Point &point){\r\n _START << \" \u5f00\u59cb\u8f93\u51fa \" << point.x << \" : \" << point.y << \" \u7ed3\u675f\u4e86 \" << endl;\r\n return _START;\r\n }\r\n \/\/ istream \u8f93\u5165 \u7cfb\u7edf\u7684\r\n friend istream & operator >> (istream & _START, Point & point) {\r\n \/\/ \u63a5\u6536\u7528\u6237\u7684\u8f93\u5165\uff0c\u628a\u8f93\u5165\u7684\u4fe1\u606f\r\n _START >> point.x >> point.y;\r\n return _START;\r\n }\r\n};\r\nint main(){\r\n Point pointA(30,50);\r\n Point pointB(10,20);\r\n\/\/ Point pointC=pointA-pointB;\r\n ++pointA;\r\n\/\/ cout << pointA.getX() << \" , \" << pointA.getY() << endl;\r\n cout << pointA << pointB <> pointC; \/\/ >> \u662f\u6211\u4eec\u81ea\u5df1\u91cd\u8f7d\u7684\u54e6\r\n cout << \"\u4f60\u8f93\u5165\u7684\u662f\uff1a\" << pointC.getX() << endl;\r\n cout << \"\u4f60\u8f93\u5165\u7684\u662f\uff1a\" << pointC.getY() << endl;\r\n}<\/pre>\n\u7c7b\u5185\u90e8\u8fd0\u7b97\u7b26\u91cd\u8f7d\uff0c\u5141\u8bb8\u8bbf\u95ee\u79c1\u6709\u53d8\u91cf<\/li>\n\u4f20\u5165\u7684\u53c2\u6570\u662f\u5e38\u91cf\u5f15\u7528\uff0cconst \u8868\u793a\u4e0d\u53ef\u66f4\u6539\uff0c& \u53ef\u4ee5\u63d0\u5347\u6027\u80fd\uff0c\u53ea\u4f1a\u6709\u4e00\u4e2a\u53d8\u91cf\u522b\u540d\uff0c\u4e0d\u52a0\u4f1a\u62f7\u8d1d\u4e00\u4efd\uff0c\u6d6a\u8d39\u5185\u5b58\u3002<\/li>\n<< >> \u91cd\u8f7d\uff0c\u9700\u8981\u52a0friend \u53cb\u5143\u51fd\u6570\u6765\u8fdb\u884c\u91cd\u8f7d<\/li>\nostream & _START\uff1a\u8868\u793a\u8f93\u51fa<\/li>\nistream & _START\uff1a\u8868\u793a\u8f93\u5165<\/li>\n3.[] \u8fd0\u7b97\u7b26\u53f7\u91cd\u8f7d<\/strong><\/div>\nclass ArrayClass {\r\nprivate:\r\n int size =0 ; \/\/ \u5927\u5c0f \u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u7ed9size\u8d4b\u9ed8\u8ba4\u503c\uff0c\u4e0d\u7136\u53ef\u80fd\u4f1a\u51fa\u73b0\uff0c\u65e0\u7a77\u5927\u7684\u95ee\u9898\r\n int * arrayValue; \/\/ \u6570\u7ec4\u5b58\u653e int \u7c7b\u578b\u7684\u5f88\u591a\u503c\r\npublic:\r\n ArrayClass(){\r\n \/*\u6307\u9488\u7c7b\u578b\u5fc5\u987b\u5206\u914d\u7a7a\u95f4*\/\r\n arrayValue= static_cast(malloc(sizeof(int *) * 10));\r\n }\r\n void set(int index, int value) {\r\n arrayValue[index] = value; \/\/ []\u76ee\u524d\u4e0d\u662f\u6211\u7684\r\n size+=1;\r\n }\r\n int getSize() { \/\/ size\u6210\u5458\u7684\u76ee\u6807\uff1a\u662f\u4e3a\u4e86\u5faa\u73af\u53ef\u4ee5\u904d\u5386\r\n return this->size;\r\n }\r\n \/\/ \u8fd0\u7b97\u7b26\u91cd\u8f7d [index]\r\n int operator[](int index) {\r\n return this->arrayValue[index]; \/\/ \u7cfb\u7edf\u7684\r\n }\r\n};\r\n\/\/ \u8f93\u51fa\u5bb9\u5668\u7684\u5185\u5bb9\r\nvoid printfArryClass(ArrayClass arrayClass) {\r\n cout << arrayClass.getSize() << endl;\r\n for (int i = 0; i < arrayClass.getSize(); ++i) {\r\n cout << arrayClass[i] << endl; \/\/ []\u662f\u6211\u4eec\u81ea\u5df1\u7684 \u91cd\u8f7d\u7b26\u53f7\r\n }\r\n}\r\nint main(){\r\n ArrayClass arrayClass; \/\/ \u6808\u533a \u5b9e\u4f8b\u51fa\u6765\u7684\u5bf9\u8c61\uff0c\u662f\u5728\u5806\u533a\u4e86\r\n arrayClass.set(0, 100);\r\n arrayClass.set(1, 200);\r\n arrayClass.set(2, 300);\r\n arrayClass.set(3, 400);\r\n arrayClass.set(4, 500);\r\n printfArryClass(arrayClass);\r\n}<\/int><\/pre>\n4.c++\u7ee7\u627f<\/strong><\/div>\nclass Person {\r\npublic:\r\n char *name;\r\n int age;\r\npublic:\r\n Person(char *name, int age) : name(name) {\r\n this->age = age;\r\n cout << \"Person \u6784\u9020\u51fd\u6570\" << endl;\r\n }\r\n void print() {\r\n cout << this->name << \" , \" << this->age << endl;\r\n }\r\n};\r\nclass Student : public Person {\r\nprivate:\r\n char * course;\r\npublic:\r\n Student(char * name, int age, char* course) : Person(name, age) , course(course) {\r\n cout << \"Student \u6784\u9020\u51fd\u6570\" << endl;\r\n }\r\n void test() {\r\n cout << name << endl;\r\n cout << age << endl;\r\n print();\r\n }\r\n};<\/pre>\n
\u9ed8\u8ba4\u662f \u9690\u5f0f\u4ee3\u7801\uff1a : private Person<\/li>\n\u79c1\u6709\u7ee7\u627f\uff1a\u5728\u5b50\u7c7b\u91cc\u9762\u662f\u53ef\u4ee5\u8bbf\u95ee\u7236\u7c7b\u7684\u6210\u5458\uff0c\u4f46\u662f\u5728\u7c7b\u7684\u5916\u9762\u4e0d\u884c<\/li>\n\u5fc5\u987b\u516c\u5f00\u7ee7\u627f\uff0c\u624d\u53ef\u4ee5\u8bbf\u95ee\u7236\u7c7b\u7684\u6210\u5458<\/li>\n\u5148\u6267\u884c\u7236\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u518d\u6267\u884c\u5b50\u7c7b\u7684\u6784\u9020\u51fd\u6570<\/li>\n5.\u591a\u7ee7\u627f<\/strong><\/div>\nclass BaseActivity1 {\r\npublic:\r\n void onCreate() {\r\n cout << \"BaseActivity1 onCreate\" << endl;\r\n }\r\n void onStart() {\r\n cout << \"BaseActivity1 onStart\" << endl;\r\n }\r\n void show() {\r\n cout << \"BaseActivity1 show\" << endl;\r\n }\r\n};\r\nclass BaseActivity2 {\r\npublic:\r\n void onCreate() {\r\n cout << \"BaseActivity2 onCreate\" << endl;\r\n }\r\n void onStart() {\r\n cout << \"BaseActivity2 onStart\" << endl;\r\n }\r\n void show() {\r\n cout << \"BaseActivity2 show\" << endl;\r\n }\r\n};\r\n\/\/ \u5b50\u7c7b \u7ee7\u627f \u4e8c\u4e2a\u7236\u7c7b\r\nclass MainActivity1 : public BaseActivity1, public BaseActivity2{\r\npublic:\r\n void onCreate() {\r\n cout << \"MainActivity1 onCreate\" << endl;\r\n }\r\n void onStart() {\r\n cout << \"MainActivity1 onStart\" << endl;\r\n }\r\n void showSonInfo() {\r\n cout << \"MainActivity1 showSonInfo\" << endl;\r\n }\r\n \/\/ void show() {\r\n \/\/ cout << \"MainActivity1 show\" << endl;\r\n \/\/}\r\n};\r\nint main(){\r\n \/\/ \u8fd9\u4e2a\u662f\u4f18\u5148\u5bfb\u627e\u5b50\u7c7b\u7684\u51fd\u6570\uff0c\u56e0\u4e3a\u7279\u522b\u660e\u786e\uff0c\u6ca1\u6709\u95ee\u9898\uff0c\u8fd8\u6ca1\u6709\u4ea7\u751f\u6b67\u4e49\uff08\u4e8c\u4e49\u6027\uff09\r\n MainActivity1 mainActivity1; \/\/ \u5b50\u7c7b\r\n mainActivity1.onCreate();\r\n mainActivity1.onStart();\r\n mainActivity1.showSonInfo();\r\n \/\/ \u4e0d\u660e\u786e\uff0c\u4e8c\u4e49\u6027\uff0c\u6b67\u4e49 \/*request for member \u2018show' is ambiguous*\/\r\n\/\/ mainActivity1.show();\r\n \/*\u89e3\u51b3\u4e8c\u4e49\u6027 \u901a\u8fc7.\u6765\u5f15\u51fa\u7236\u7c7b \u7136\u540e\u518d\u8c03\u7528*\/\r\n mainActivity1.BaseActivity3::show();\r\n mainActivity1.BaseActivity2::show();\r\n mainActivity1.BaseActivity1::show();\r\n \/\/ \u89e3\u51b3\u65b9\u6848\u4e8c\uff1a \u5b50\u7c7b\u4e0a \u91cd\u5199\u7236\u7c7b\u7684show\u51fd\u6570\r\n mainActivity1.show();\r\n}<\/pre>\n
c++ \u5141\u8bb8\u591a\u7ee7\u627f\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e8c\u4e49\u6027\uff0c\u539f\u5219\u4e0a\u662f\u5c3d\u91cf\u907f\u514d\u4e8c\u4e49\u6027<\/li>\n\u901a\u8fc7\u660e\u786e\u7236\u7c7b\u7684\u65b9\u5f0f\u89e3\u51b3\u4e8c\u4e49\u6027<\/li>\n\u901a\u8fc7\u5b50\u7c7b\u91cd\u5199\u7236\u7c7b\u7684\u65b9\u6cd5\u89c4\u907f\u4e8c\u4e49\u6027<\/li>\n6.\u901a\u8fc7\u865a\u7ee7\u627f\u6765\u89e3\u51b3\u4e8c\u4e49\u6027\u95ee\u9898<\/strong><\/div>\n\/\/ \u7956\u7236\u7c7b\r\nclass Object{\r\npublic:\r\n int number;\r\n void show() {\r\n cout << \"Object show run...\" << endl;\r\n }\r\n};\r\n\/\/ \u7236\u7c7b1\r\nclass BaseActivity1 : virtual public Object {\r\n};\r\n\/\/ \u7236\u7c7b2\r\nclass BaseActivity2 : virtual public Object {\r\n};\r\n\/\/ \u5b50\u7c7b\r\nclass Son : public BaseActivity1, public BaseActivity2 {\r\n};\r\nint main(){\r\n Object object;\r\n BaseActivity1 baseActivity1;\r\n BaseActivity2 baseActivity2;\r\n Son son;\r\n object.number = 100;\r\n baseActivity1.number = 200;\r\n baseActivity2.number = 300;\r\n son.number = 400;\r\n object.show();\r\n baseActivity1.show();\r\n baseActivity2.show();\r\n son.show();\r\n cout << object.number << endl;\r\n cout << baseActivity1.number << endl;\r\n cout << baseActivity2.number << endl;\r\n cout << son.number << endl;\r\n}<\/pre>\n
\u5982\u679c\u6ca1\u6709\u865a\u7ee7\u627f\uff0c\u90a3\u4e48son\u5bf9\u8c61\u8bbf\u95eenumber\u5c31\u4f1a\u62a5\u4e8c\u4e49\u6027\u7684\u95ee\u9898\uff0c\u540c\u65f6\u8bbf\u95eeshow\u65b9\u6cd5\u540c\u6837\u5b58\u5728\u4e8c\u4e49\u6027\u95ee\u9898<\/li>\n\u7531\u4e8e\u5728\u7ee7\u627f\u7684\u65f6\u5019\u6dfb\u52a0\u4e86\u865a\u7ee7\u627f\uff0c\u5c31\u80fd\u89e3\u51b3\u7c7b\u4f3c\u8fd9\u6837\u7684\u95ee\u9898\uff0c\u865a\u7ee7\u627f\u7684\u542b\u4e49\u662f\uff1a\u5c06\u901a\u8fc7\u7ee7\u627f\u5f97\u6765\u7684number\u548cshow\u65b9\u6cd5\uff0c\u653e\u7f6e\u5728\u53e6\u5916\u4e00\u4e2a\u7edf\u4e00\u7a7a\u95f4\u4e0a\uff0c\u8fd9\u6837\u5b50\u7c7b\u518d\u8bbf\u95ee\u7684\u65f6\u5019\u5c31\u4e0d\u4f1a\u51fa\u73b0\u4e8c\u4e49\u6027\u7684\u95ee\u9898\u4e86\u3002<\/li>\n\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8eC++\u8fd0\u7b97\u7b26\u91cd\u8f7d\u4e0e\u591a\u7ee7\u627f\u53ca\u4e8c\u4e49\u6027\u8be6\u89e3\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"
class Point { private: int x,y; public: \/\/ \u7cfb\u7edfC++\u6e90\u7801\uff0c\u5927\u91cf\u4f7f\u7528 […]<\/p>\n","protected":false},"author":362,"featured_media":52821,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-258234","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/258234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=258234"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/258234\/revisions"}],"predecessor-version":[{"id":258237,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/258234\/revisions\/258237"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/52821"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=258234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=258234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=258234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}