{"id":1236,"date":"2018-09-06T14:08:01","date_gmt":"2018-09-06T06:08:01","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=1236"},"modified":"2018-09-06T14:08:01","modified_gmt":"2018-09-06T06:08:01","slug":"c%e7%b1%bb%e7%9a%84%e5%9f%ba%e6%9c%ac%e5%ae%9a%e4%b9%89%e4%b8%8e%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/cpp\/2018\/09\/06\/1408011236.html","title":{"rendered":"C++\u7c7b\u7684\u57fa\u672c\u5b9a\u4e49\u4e0e\u4f7f\u7528"},"content":{"rendered":"<p><strong>1\u3001\u7c7b\u7684\u57fa\u672c\u5b9a\u4e49<\/strong><\/p>\n<p>1.1\u3001class\u5173\u952e\u5b57+\u7c7b\u540d\uff0c\u7c7b\u540d\u4f7f\u7528\u5927\u9a7c\u5cf0\u547d\u540d\u89c4\u5219\uff1b<br \/>\n1.2\u3001\u4f7f\u7528private\u3001protected\u3001public\u4fee\u9970\u6210\u5458\u5c5e\u6027\u4e0e\u65b9\u6cd5\uff1b<br \/>\n1.3\u3001\u65b9\u6cd5\u540d\u4f7f\u7528\u5927\u9a7c\u5cf0\u547d\u540d\u89c4\u5219\uff0c\u6210\u5458\u5c5e\u6027\u4f7f\u7528m_xxxx\u4e0b\u5212\u7ebf\u547d\u540d\u89c4\u5219\uff1b<br \/>\n1.4\u3001\u6784\u9020\u51fd\u6570\u4e0e\u6790\u6784\u51fd\u6570\u540d\u5b57\u4e0e\u7c7b\u540d\u76f8\u540c\uff0c\u65e0\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u53ef\u91cd\u8f7d\uff0c\u6790\u6784\u51fd\u6570\u524d\u52a0~\uff1b<\/p>\n<pre lang=\"cpp\" line=\"1\">class ClassName{\r\nprivate:    \/\/ \u79c1\u6709\u5c5e\u6027\u4e0e\u65b9\u6cd5\r\n    string  m_prop1;\r\nprotected:  \/\/ \u4fdd\u62a4\u5c5e\u6027\u4e0e\u65b9\u6cd5\r\n    int     m_prop2;\r\npublic:     \/\/ \u516c\u5f00\u5c5e\u6027\u4e0e\u65b9\u6cd5\r\n    double  m_prop3;\r\n    \/\/ \u6784\u9020\u51fd\u6570\u4e0e\u6790\u6784\u51fd\u6570\r\n    ClassName();\r\n    ~ClassName();\r\n    void    Method();\r\n};<\/pre>\n<p><strong>2\u3001\u7c7b\u65b9\u6cd5\u7684\u5b9e\u73b0<\/strong><\/p>\n<p>2.1\u3001\u5728Cpp\u6587\u4ef6\u4e2d\uff0c\u5f15\u7528.h\u6216.hpp\u6587\u4ef6\uff0c\u901a\u8fc7ClassName::Method()\u7684\u65b9\u5f0f\u5b9e\u73b0\u7c7b\u65b9\u6cd5\uff1b<br \/>\n2.2\u3001\u540c\u4e00\u4e2aCpp\u6587\u4ef6\u4e2d\u53ef\u5b9e\u73b0\u591a\u4e2a\u7c7b\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u4f5c\u7528\u57df\u9650\u5b9a\u7b26(::)\u533a\u5206\uff1b<\/p>\n<pre lang=\"cpp\" line=\"1\">ClassName::ClassName()\r\n{\r\n    \/\/ \u8fd9\u662f\u9ed8\u8ba4\u7684\u6784\u9020\u51fd\u6570\uff0c\u53ef\u7701\u7565\r\n    \/\/ \u4e00\u822c\u7528\u6765\u5bf9\u8c61\u521d\u59cb\u5316\u5de5\u4f5c\uff1b\r\n    \/\/ \u53ef\u91cd\u8f7d\r\n}<\/pre>\n<p><strong>3\u3001\u57fa\u672c\u793a\u4f8b<\/strong><!--more--><\/p>\n<p>3.1\u3001Student.hpp<\/p>\n<pre lang=\"cpp\" line=\"1\">\/\/\r\n\/\/  Student.hpp\r\n\/\/  cplus\r\n\/\/\r\n\/\/  Created by \u4f59\u897f\u5b89 on 2018\/9\/5.\r\n\/\/  Copyright \u00a9 2018\u5e74 yusian. All rights reserved.\r\n\/\/\r\n\r\n#ifndef Student_hpp\r\n#define Student_hpp\r\n\r\n#include <iostream>\r\nusing namespace std;\r\nclass Student{\r\nprivate:\r\n    string  m_name;\r\n    int     m_age;\r\n    string  m_sex;\r\npublic:\r\n    Student();\r\n    Student(string);\r\n    Student(string, int);\r\n    Student(string, int, string);\r\n    ~Student();\r\n    \r\n    void    SetName(string);\r\n    string  GetName();\r\n    \r\n    void    SetAge(int);\r\n    int     GetAge();\r\n    \r\n    void    SetSex(string);\r\n    string  GetSex();\r\n    \r\n    void    ShowInfo();\r\n};\r\n#endif \/* Student_hpp *\/<\/pre>\n<p>3.2\u3001Student.cpp<\/p>\n<pre lang=\"cpp\" line=\"1\">\/\/\r\n\/\/  Student.cpp\r\n\/\/  cplus\r\n\/\/\r\n\/\/  Created by \u4f59\u897f\u5b89 on 2018\/9\/5.\r\n\/\/  Copyright \u00a9 2018\u5e74 yusian. All rights reserved.\r\n\/\/\r\n\r\n#include \"Student.hpp\"\r\n\r\nStudent::Student(){\r\n    cout << \"## \" << \"\u9ed8\u8ba4\u6784\u9020\" << endl;\r\n}\r\nStudent::Student(string name):m_name(name)\r\n{\r\n    cout << \"## \" << \"String\u6784\u9020\" << endl;\r\n}\r\nStudent::Student(string name, int age):m_name(name),m_age(age)\r\n{\r\n    cout << \"## \" << \"String,int\u6784\u9020\" << endl;\r\n}\r\nStudent::Student(string name, int age, string sex):m_name(name),m_age(age),m_sex(sex)\r\n{\r\n    cout << \"## \" << \"String,int,sex\u6784\u9020\" << endl;\r\n}\r\nStudent::~Student()\r\n{\r\n    cout << \"** \" << m_name << \"\u91ca\u653e...\" << endl;\r\n}\r\n#pragma mark - Set|Get\u65b9\u6cd5\u5b9e\u73b0\r\nvoid Student::SetName(string name)\r\n{\r\n    m_name = name;\r\n}\r\nstring Student::GetName()\r\n{\r\n    return m_name;\r\n}\r\nvoid Student::SetAge(int age)\r\n{\r\n    if (age < 0) age = 0;\r\n    m_age = age;\r\n}\r\nint Student::GetAge()\r\n{\r\n    return m_age;\r\n}\r\nvoid Student::SetSex(string sex)\r\n{\r\n    if (sex.compare(\"\u7537\")){\r\n        m_sex = \"\u7537\";\r\n    }else if (sex.compare(\"\u5973\")){\r\n        m_sex = \"\u5973\";\r\n    }else{\r\n        m_sex = \"\u4fdd\u5bc6\";\r\n    }\r\n}\r\nstring Student::GetSex()\r\n{\r\n    return m_sex;\r\n}\r\n#pragma mark -\r\nvoid Student::ShowInfo()\r\n{\r\n    cout << \"\u59d3\u540d\uff1a\" << m_name << \", \u6027\u522b\uff1a\" << m_sex << \", \u5e74\u9f84\uff1a\" << m_age << endl;\r\n}<\/pre>\n<p>3.3\u3001main.cpp<\/p>\n<pre lang=\"cpp\" line=\"1\">\r\n#include <iostream>\r\n#include \"Student.hpp\"\r\nusing namespace std;\r\n\r\nint main(){\r\n    \r\n    \/\/ \u5728\u6808\u4e2d\u521b\u5efa\u5bf9\u8c61\uff0c\u7531\u7cfb\u7edf\u56de\u6536\u5185\u5b58\u7a7a\u95f4\uff0c\u901f\u5ea6\u5feb\u4f46\u7c7b\u4f3c\u5c40\u90e8\u53d8\u91cf\u4e0d\u80fd\u4e2a\u90e8\u4f7f\u7528\r\n    Student student;\r\n    student.SetName(\"\u5f20\u4e09\");\r\n    student.SetAge(21);\r\n    student.ShowInfo();\r\n    \r\n    \/\/ \u5728\u5806\u4e2d\u521b\u5efa\u5bf9\u8c61\uff0c\u9700\u8981\u624b\u52a8\u7ba1\u7406\u5185\u5b58\uff0c\u4f7f\u7528\u7075\u6d3b\u9002\u5408\u7a7a\u95f4\u4f7f\u7528\u8f83\u5927\u7684\u573a\u666f\r\n    Student *jack = new Student();\r\n    jack->SetName(\"\u6770\u514b\");\r\n    jack->SetAge(25);\r\n    jack->ShowInfo();\r\n    \r\n    Student *rose = new Student(\"Rose\");\r\n    rose->SetSex(\"\u5973\");\r\n    rose->ShowInfo();\r\n    \r\n    Student *sim = new Student(\"Sim\", 22);\r\n    sim->SetSex(\"\u5973\");\r\n    sim->ShowInfo();\r\n    \r\n    Student *licy = new Student(\"licy\", 22, \"\u5973\");\r\n    licy->ShowInfo();\r\n    \r\n    delete jack;\r\n    delete rose;\r\n    delete sim;\r\n    delete licy;\r\n    return 0;\r\n}<\/pre>\n<p>3.4\u3001\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n<p><code>## \u9ed8\u8ba4\u6784\u9020<br \/>\n\u59d3\u540d\uff1a\u5f20\u4e09, \u6027\u522b\uff1a, \u5e74\u9f84\uff1a21<br \/>\n## \u9ed8\u8ba4\u6784\u9020<br \/>\n\u59d3\u540d\uff1a\u6770\u514b, \u6027\u522b\uff1a, \u5e74\u9f84\uff1a25<br \/>\n## String\u6784\u9020<br \/>\n\u59d3\u540d\uff1aRose, \u6027\u522b\uff1a\u7537, \u5e74\u9f84\uff1a567<br \/>\n## String,int\u6784\u9020<br \/>\n\u59d3\u540d\uff1aSim, \u6027\u522b\uff1a\u7537, \u5e74\u9f84\uff1a22<br \/>\n## String,int,sex\u6784\u9020<br \/>\n\u59d3\u540d\uff1alicy, \u6027\u522b\uff1a\u5973, \u5e74\u9f84\uff1a22<br \/>\n** \u6770\u514b\u91ca\u653e...<br \/>\n** Rose\u91ca\u653e...<br \/>\n** Sim\u91ca\u653e...<br \/>\n** licy\u91ca\u653e...<br \/>\n** \u5f20\u4e09\u91ca\u653e...<br \/>\nProgram ended with exit code: 0<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u7c7b\u7684\u57fa\u672c\u5b9a\u4e49 1.1\u3001class\u5173\u952e\u5b57+\u7c7b\u540d\uff0c\u7c7b\u540d\u4f7f\u7528\u5927\u9a7c\u5cf0\u547d\u540d\u89c4\u5219\uff1b 1.2\u3001\u4f7f\u7528private\u3001protected\u3001public\u4fee\u9970\u6210\u5458\u5c5e\u6027\u4e0e\u65b9\u6cd5\uff1b 1.3\u3001\u65b9\u6cd5\u540d\u4f7f\u7528\u5927\u9a7c\u5cf0\u547d\u540d\u89c4\u5219\uff0c\u6210\u5458\u5c5e\u6027\u4f7f\u7528m_xxxx\u4e0b\u5212\u7ebf\u547d\u540d\u89c4\u5219\uff1b 1.4\u3001\u6784\u9020\u51fd\u6570\u4e0e\u6790\u6784\u51fd\u6570\u540d\u5b57\u4e0e\u7c7b\u540d\u76f8\u540c\uff0c\u65e0\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u53ef\u91cd\u8f7d\uff0c\u6790\u6784\u51fd\u6570\u524d\u52a0~\uff1b class ClassName{ private: \/\/ \u79c1\u6709\u5c5e\u6027\u4e0e\u65b9\u6cd5 string m_prop1; protected: \/\/ \u4fdd\u62a4\u5c5e\u6027\u4e0e\u65b9\u6cd5 int m_prop2; public: \/\/ \u516c\u5f00\u5c5e\u6027\u4e0e\u65b9\u6cd5 double m_prop3; \/\/ \u6784\u9020\u51fd\u6570\u4e0e\u6790\u6784\u51fd\u6570 ClassName(); ~ClassName(); void Method(); }; 2\u3001\u7c7b\u65b9\u6cd5\u7684\u5b9e\u73b0 2.1\u3001\u5728Cpp\u6587\u4ef6\u4e2d\uff0c\u5f15\u7528.h\u6216.hpp\u6587\u4ef6\uff0c\u901a\u8fc7ClassName::Method()\u7684\u65b9\u5f0f\u5b9e\u73b0\u7c7b\u65b9\u6cd5\uff1b 2.2\u3001\u540c\u4e00\u4e2aCpp\u6587\u4ef6\u4e2d\u53ef\u5b9e\u73b0\u591a\u4e2a\u7c7b\u7684\u65b9\u6cd5\uff0c\u901a\u8fc7\u4f5c\u7528\u57df\u9650\u5b9a\u7b26(::)\u533a\u5206\uff1b ClassName::ClassName() { \/\/ \u8fd9\u662f\u9ed8\u8ba4\u7684\u6784\u9020\u51fd\u6570\uff0c\u53ef\u7701\u7565 \/\/ \u4e00\u822c\u7528\u6765\u5bf9\u8c61\u521d\u59cb\u5316\u5de5\u4f5c\uff1b \/\/ \u53ef\u91cd\u8f7d } 3\u3001\u57fa\u672c\u793a\u4f8b<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88],"tags":[93,96,94,95],"class_list":["post-1236","post","type-post","status-publish","format-standard","hentry","category-cpp","tag-c-c","tag-96","tag-94","tag-95"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=1236"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1236\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}