{"id":39,"date":"2008-01-25T22:49:48","date_gmt":"2008-01-26T02:49:48","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=39"},"modified":"2008-01-26T14:15:23","modified_gmt":"2008-01-26T18:15:23","slug":"c-class","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=39","title":{"rendered":"C++ Class"},"content":{"rendered":"<ul>\n<li>Define a class<br \/>\n<code lang=\"cpp\"><br \/>\nclass CBox<br \/>\n{<br \/>\n\/\/ public data members<br \/>\npublic:<br \/>\n\u00a0 double m_Length;<br \/>\n\u00a0 double m_Breadth;<br \/>\n\u00a0 double m_Height;\/\/Constructor<br \/>\nCBox(double lv = 1.0, double bv = 1.0, double hv = 1.0)<br \/>\n{<br \/>\nm_Length = lv;<br \/>\nm_Breadth = bv;<br \/>\nm_Height = hv;<br \/>\n}\/\/ Same as above<br \/>\nCBox(double lv = 1.0, double bv = 1.0, double hv = 1.0) : m_Length(lv), m_Breadth(bv), m_Height(hv)<br \/>\n{<br \/>\n}\/\/ member function<br \/>\ndouble Volume()<br \/>\n{<br \/>\n\u00a0 return m_Length * m_Breadth * m_Height;<br \/>\n}<br \/>\n}; \/\/ Don't forget this semicolon<\/code><\/li>\n<li>Access modifiers\n<ul>\n<li>public<\/li>\n<li>protected<\/li>\n<li>private \/\/Default<\/li>\n<li>static<\/li>\n<\/ul>\n<\/li>\n<li>Constant object of class\n<ul>\n<li>No changes allowed to the object after instantiation<\/li>\n<li>Examle\u00a0\n<ul>\n<li><strong>const<\/strong> CBox standardBox(3.0,5.0,8.0);<\/li>\n<\/ul>\n<\/li>\n<li>Costant object\u00a0can only access constant\u00a0member function\n<ul>\n<li>int Compare(CBox box) <strong>const <\/strong>{&#8230;};<\/li>\n<\/ul>\n<\/li>\n<li>Should declare all member functions that do not alter the class object as <strong>const<\/strong><\/li>\n<li>Methods with same signatures except const are consider two different methods<\/li>\n<\/ul>\n<\/li>\n<li>Instantiate class\n<ul>\n<li>CBox box1;<\/li>\n<li>CBox box2;<\/li>\n<li>CBox box3(10.0, 20.0, 30.0);<\/li>\n<\/ul>\n<\/li>\n<li>Access member data\n<ul>\n<li>box1.m_Length;<\/li>\n<\/ul>\n<\/li>\n<li>Access member functions\n<ul>\n<li>box1.Volume();<\/li>\n<\/ul>\n<\/li>\n<li>Pointer to class\n<ul>\n<li>CBox box1(1.0, 2.0, 3.0);<\/li>\n<li>CBox* pBox1 = &amp;box1;<\/li>\n<li>pBox1-&gt;Volume();<\/li>\n<\/ul>\n<\/li>\n<li>Class reference\n<ul>\n<li>Complex class object should be pass as reference to avoid copying<\/li>\n<li>CBox(const CBox&amp; initB);<\/li>\n<\/ul>\n<\/li>\n<li>Destructor\n<ul>\n<li>~CBox();<\/li>\n<li><strong>Always<\/strong> implement a copy constructor if you dynamically allocate memory for class members\n<ul><code lang=\"cpp\"><br \/>\n\/\/ Copy Constructor definition<br \/>\nCMessage(const CMessage&amp; initM)<br \/>\n{<br \/>\n\u00a0 \/\/ Allocate space for text<br \/>\n\u00a0 pmessage = new char[ strlen(initM.pmessage) + 1 ];<br \/>\n\u00a0 \/\/ Copy text to new memory<br \/>\n\u00a0 strcpy(pmessage, initM.pmessage);<br \/>\n}<br \/>\n<\/code><\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>Exercise\n<ul>\n<li>Create a new project\n<ul>\n<li>File -&gt; New -&gt; Projects -&gt; Win32 Console Application<\/li>\n<\/ul>\n<\/li>\n<li>Create a new class\u00a0\n<ul>\n<li>In Class View\n<ul>\n<li>Right click Classes -&gt; New Class -&gt; MyClass<\/li>\n<\/ul>\n<\/li>\n<li>Two files are created\n<ul>\n<li>MyClass.cpp\n<ul>\n<li>Contains class implementations<\/li>\n<\/ul>\n<\/li>\n<li>MyClass.h\n<ul>\n<li>Contains class definition<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>Add data members<\/li>\n<li>Add inline function members (short functions)<\/li>\n<li>Add other function members\n<ul>\n<li>Add function prototype to header file<\/li>\n<li>Add function implementation to cpp file<\/li>\n<\/ul>\n<\/li>\n<li>Add global\u00a0functions to support MyClass operations\n<ul>\n<li>File -&gt; New -&gt; C++ Source File -&gt; MyClassOperations.cpp<\/li>\n<\/ul>\n<\/li>\n<li>Add new header file for MyClassOperations.cpp\n<ul>\n<li>File -&gt; New -&gt; C++ Header File -&gt; MyClassOperations.h<\/li>\n<\/ul>\n<\/li>\n<li>Use MyClass in main() function\n<ul>\n<li>#include MyClass.h<\/li>\n<li>#include MyClassOperations.h<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Define a class class CBox { \/\/ public data members public: \u00a0 double m_Length; \u00a0 double m_Breadth; \u00a0 double m_Height;\/\/Constructor CBox(double lv = 1.0, double bv = 1.0, double hv = 1.0) { m_Length = lv; m_Breadth = bv; m_Height &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=39\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[11],"tags":[],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-cpp"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-D","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=39"}],"version-history":[{"count":0,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}