{"id":573,"date":"2017-09-28T21:15:25","date_gmt":"2017-09-28T20:15:25","guid":{"rendered":"http:\/\/www.45rpmsoftware.com\/wordpress\/?p=573"},"modified":"2017-09-29T11:44:45","modified_gmt":"2017-09-29T10:44:45","slug":"object-oriented-c-encapsulation","status":"publish","type":"post","link":"https:\/\/www.45rpmsoftware.com\/blog\/?p=573","title":{"rendered":"Object Oriented C &#8211; Encapsulation"},"content":{"rendered":"<p>If you haven\u2019t read my earlier articles on Object Oriented Programming in C (<a href=\"\u201c\u201c\u201c\u201c\u201chttps:\/\/www.45rpmsoftware.com\/wordpress\/?p=565\u201d\u201d\u201d\u201d\u201d\">Objects<\/a> and <a href=\"\u201c\u201c\u201c\u201c\u201chttps:\/\/www.45rpmsoftware.com\/wordpress\/?p=567\u201d\u201d\u201d\u201d\u201d\">Polymorphism<\/a>) I suggest that you go back and read these first. \u00a0It isn\u2019t necessary to understand these concepts in order to understand Encapsulation &#8211; but it might help give you some background into what I am attempting to achieve here.<\/p>\n<p>To recap, Encapsulation is a means of preventing access to internal object data, ensuring that the data can only be accessed and modified via methods. That\u2019s not to say the internal object data can never be manipulated directly \u2013 but one has the option to decide how best the data should be accessed. In explicitly OOP languages this is often achieved through the use of the \u2018private\u2019, \u2018public\u2019 and \u2018protected\u2019 keywords.<!--more--> <!--\u2014more\u2014--><\/p>\n<p>C doesn\u2019t have anything even remotely like \u2018Private\u2019, \u2018Public\u2019 or \u2018Protected\u2019 keywords &#8211; but we can achieve something similar by bringing a little discipline to our coding practices. \u00a0But first, let\u2019s look at what Private, Public and Protected mean.<\/p>\n<p><b>Private<\/b><br \/>\nPrivate means that the data and methods can only be accessed by the class itself. To use your family as an analogy, this might include your deepest darkest secrets, those that you wouldn\u2019t even admit to your parents. Either that, or the knowledge of what you\u2019re going to get them for Christmas.<\/p>\n<p><b>Public<\/b><\/p>\n<p>Public means that the data and methods can be accessed by anyone. Using the family analogy again, this might include your address. \u00a0After all, this information is freely available in the Electoral Register for everyone to see, not just those people that you are related to.<\/p>\n<p><b>Protected<\/b><\/p>\n<p>Protected means that the data and methods can only be accessed by the classes subclasses. Again, using the family analogy, this might be your love for your family. \u00a0Your family has your love, but the general public doesn\u2019t.<\/p>\n<p>From this we can see that I\u2019m not very good at analogies. \u00a0Let\u2019s see if I\u2019m any better at coding.<\/p>\n<p><i>Public<\/i> and <i>Private<\/i> are quite easy to deal with. \u00a0Anything Public we can put in our header &#8211; anything Private belongs in the source file.<\/p>\n<p>Header &#8211; person.h<\/p>\n<pre>typedef struct person_public personPublic;\r\ntypedef struct person_public {\r\n    int age;\r\n    char* name;\r\n    char* surname;\r\n    char* address;\r\n}\r\n<\/pre>\n<p>Source &#8211; person.c<\/p>\n<pre>#import \u201cperson.h\u201d;\r\ntypedef struct person_private personPrivate;\r\ntypedef struct person_private {\r\n    int creditCardNumber;\r\n    char* deepestDarkestSecret;\r\n}\r\n<\/pre>\n<p>In this trivial example, any code which imports the person.h file will have access to the data and functions defined therein. \u00a0Anything in this h file can therefore be regarded as being <i>public<\/i>. \u00a0The function code will be contained in the person.c source code file, of course, and the data will be manipulated there.<\/p>\n<p>Code and data in the person.c file which doesn\u2019t contain a corresponding definition in the person.h file will not be accessible outside the source code file. \u00a0It can therefore be regarded as being <i>private<\/i>.<\/p>\n<p>So far I don\u2019t think that I\u2019ve had to cheat in any way, or take advantage of any hacks or kludges. \u00a0In fact, I don\u2019t even need to rely on the basic human decency of any programmer using this code to play by the rules &#8211; good style enforces the rules for me. \u00a0Unfortunately, if we want to implement an analogue of <i>protected<\/i>\u00a0we need to get a little bit hacky.<\/p>\n<p>C doesn\u2019t understand the concept of \u2018protected\u2019 and the only way that I can think of for implementing it is to create a second header file &#8211; person_PROTECTED.h. \u00a0person_PROTECTED.h will contain any data and functions which should only be accessible to classes which inherit from the parent. \u00a0Unfortunately, there\u2019s no mechanism to prevent the PROTECTED header from being imported by code which should only ever have access to the regular public header. \u00a0In fact, that\u2019s why I capitalised PROTECTED; that way anyone code reviewing the source knows to pay special attention to whether the PROTECTED header file is allowed to be imported here.<br \/>\nHeader &#8211; person_PROTECTED.h<\/p>\n<pre>typedef struct person_protected personProtected;\r\ntypedef struct person_protected {\r\n    int love;\r\n}\r\n<\/pre>\n<p>The source code, of course, will also need to import this header, becoming:<br \/>\nSource &#8211; person.c<\/p>\n<pre>#import \u201cperson.h\u201d;\r\n#import \u201cperson_PROTECTED.h\u201d;\r\ntypedef struct person_private personPrivate;\r\ntypedef struct person_private {\r\n    int creditCardNumber;\r\n    char* deepestDarkestSecret;\r\n}\r\n<\/pre>\n<p>So there you have it. \u00a0Encapsulation, C style.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you haven\u2019t read my earlier articles on Object Oriented Programming in C (Objects and Polymorphism) I suggest that you go back and read these first. \u00a0It isn\u2019t necessary to understand these concepts in order to understand Encapsulation &#8211; but it might help give you some background into what I am attempting to achieve here. &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.45rpmsoftware.com\/blog\/?p=573\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Object Oriented C &#8211; Encapsulation&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[15,18,28,5,1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/573"}],"collection":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=573"}],"version-history":[{"count":0,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/573\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.45rpmsoftware.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}