{"id":348,"date":"2011-10-13T15:24:31","date_gmt":"2011-10-13T15:24:31","guid":{"rendered":"http:\/\/www.drmop.com\/?p=348"},"modified":"2011-10-13T15:24:31","modified_gmt":"2011-10-13T15:24:31","slug":"general-programming-tutorials-beautiful-classes","status":"publish","type":"post","link":"http:\/\/www.drmop.com\/index.php\/2011\/10\/13\/general-programming-tutorials-beautiful-classes\/","title":{"rendered":"General Programming Tutorials &#8211; Beautiful Classes"},"content":{"rendered":"<p>Welcome to my first blog on general programming techniques and object oriented software design patterns, part of my <a title=\"General Programming Tutorials - Beautiful Classes\" href=\"http:\/\/www.drmop.com\/index.php\/programming\/\" target=\"_self\">general programming series<\/a> .\u00a0I&#8217;m\u00a0sharing these programming techniques and software design patterns with you in the hopes that it will enable you to write more readable, functional, re-usable and extensible code, saving you both time and money in software development.<\/p>\n<p>This blog article is going to cover some standard basic class layout techniques that I use to keep classes neat and readable. Please note that class layout and design is a very subjective subject, so this may not be the most efficient or perfect way of laying out classes but I find that it works great for my needs.<\/p>\n<p>All code outlined within my articles will be mainly based around the C++ language. I may include other languages now and again.<\/p>\n<p><strong>Beautiful Classes<\/strong><\/p>\n<p>When I design classes I tend to think about aesthetics as well as functionality. I like my class to basically look good and appear very readable<\/p>\n<p>Here is the basic layout that I use for most of my classes:<\/p>\n<pre>\r\n<blockquote>\r\nclass MyClass\r\n{\r\n    <span style=\"color: #ff6600;\">\/\/ Section 1 - Public enums, typedefs and static data.<\/span>\r\n    <span style=\"color: #008000;\">\/\/\/\/ Public Types<\/span>\r\npublic:\r\n    typedef        unsigned int    u_int;\r\n    <span style=\"color: #008000;\">\/\/\/\/ Public Types End<\/span>\r\n\r\n    <span style=\"color: #ff6600;\">\/\/  Section 2 \u2013 Properties (Provides public access to class  properties via getters and setters)<\/span>\r\n    <span style=\"color: #008000;\">\/\/\/\/ Properties<\/span>\r\nprivate:\r\n    int            ClassData1;\r\n    bool           ClassData2;\r\npublic:\r\n    void           setClassData1(int data)        { ClassData1 = data; }\r\n    int            getClassData1() const          { return ClassData1; }\r\n    void           setClassData2(bool data)       { ClassData1 = data; }\r\n    int            getClassData2() const          { return ClassData1; }\r\n    <span style=\"color: #008000;\">\/\/\/\/ Properties End<\/span>\r\n\r\n    <span style=\"color: #ff6600;\">\/\/  Section 3 - Private \/ protected types, class data and methods (used by the class internally only)<\/span>\r\n    <span style=\"color: #008000;\">\/\/\/\/ Class Internals<\/span>\r\nprivate:\r\n    bool           PrivateClassData;\r\n    <span style=\"color: #008000;\">\/\/\/\/ Class Internals End<\/span>\r\n\r\n    <span style=\"color: #ff6600;\">\/\/  Section 4 - Construction \/ destruction and initialisation<\/span>\r\n    <span style=\"color: #008000;\">\/\/\/\/ Initialisation<\/span>\r\npublic:\r\n    MyClass()    : ClassData1(0), ClassData2(false), private_class_data(false) {}\r\n    ~MyClass()    {}\r\n    bool          Init();\r\n    void          Release();\r\n    <span style=\"color: #008000;\">\/\/\/\/ Initialisation End<\/span>\r\n\r\n    <span style=\"color: #ff6600;\">\/\/  Section 5 - Public access class functionality<\/span>\r\n    <span style=\"color: #008000;\">\/\/\/\/ Public Access<\/span>\r\npublic:\r\n    void          DoSomething1();\r\n    void          DoSomething2();\r\n    <span style=\"color: #008000;\">\/\/\/\/ Public Access End<\/span>\r\n};<\/blockquote>\r\n<\/pre>\n<p>I usually split my classes into 5 sections:<\/p>\n<ul>\n<li>Section 1 &#8211; Public enums, typedefs and static data<\/li>\n<li>Section 2 &#8211; Properties provide public access to class private data via getters and setters. A property can be thought of as a private class variable that you want to allow the outside world to change but in a safe manner.<\/li>\n<li>Section 3 &#8211; Private \/ protected types, class data and methods (used by the class internally only)<\/li>\n<li>Section 4 &#8211; Construction \/ destruction and initialisation. Note that I usually like to move all class initialisation code out of the constructor and place it into my class Init() method, same goes for \u00a0the destructor and the Release() method. This allows me to re-initialise and tear down a class without having to actually new or delete it. This can prove very useful in many situations such as object pooling to reduce memory fragmentation, where the idea is to preserve the class and re-use it.<\/li>\n<li>Section 5 &#8211; Public access class functionality<\/li>\n<\/ul>\n<p>I find that this kind of system helps me to determine where to quickly look inside my classes to find what I need.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to my first blog on general programming techniques and object oriented software design patterns, part of my general programming series .\u00a0I&#8217;m\u00a0sharing these programming techniques and software design patterns with you in the hopes that it will enable you to write more readable, functional, re-usable and extensible code, saving you both time and money in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[118,3,1],"tags":[128,127,129,125,126],"class_list":["post-348","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-programming","category-uncategorized","tag-beautiful-classes","tag-c-classes","tag-class-layouts","tag-general-programming","tag-programming-tutorials"],"_links":{"self":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts\/348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/comments?post=348"}],"version-history":[{"count":3,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts\/348\/revisions"}],"predecessor-version":[{"id":351,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/posts\/348\/revisions\/351"}],"wp:attachment":[{"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/media?parent=348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/categories?post=348"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.drmop.com\/index.php\/wp-json\/wp\/v2\/tags?post=348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}