{"id":6922,"date":"2014-06-02T11:01:24","date_gmt":"2014-06-02T10:01:24","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=6922"},"modified":"2014-06-02T11:01:24","modified_gmt":"2014-06-02T10:01:24","slug":"struct-vs-class-in-c","status":"publish","type":"post","link":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/2014\/06\/02\/struct-vs-class-in-c\/","title":{"rendered":"struct vs class in C++"},"content":{"rendered":"<p>Maybe I should apologize for going on about C++ yet again, but, following on from the recent <a href=\"http:\/\/www.designnews.com\/lecture-calendar.asp\" target=\"_blank\" rel=\"noopener noreferrer\">online lecture series<\/a> that I conducted, I learned that there is a lot of interest in the topic. I also found the experience a very fruitful source of ideas, inspiration and queries &#8211; much of this will appear here.<\/p>\n<p>During the lectures, I would periodically pose a question, which the attendees could answer via the chat. An interesting one was: how does a <strong>struct<\/strong> differ from a <strong>class<\/strong> in C++? &#8230;<!--more--><\/p>\n<p><a href=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/51\/2014\/06\/1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright wp-image-6923\" src=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/51\/2014\/06\/1-520x467.png\" alt=\"1\" width=\"300\" height=\"269\" \/><\/a>I will start by defining a <strong>struct<\/strong> in C. I would see it as a customized, composite data type, which may be constructed from the existing built-in data types [<strong>int<\/strong>, <strong>char<\/strong>, etc.], bit fields [integers of specified bit size] and other <strong>struct<\/strong>s. This example shows a simple example of a <strong>struct<\/strong> definition, along with the declaration of a variable of that type and access to one of the fields. A <strong>struct<\/strong> is a handy and flexible way to represent data. Similar facilities exist in most modern programming languages.<\/p>\n<p>How does a C++ <strong>class<\/strong> differ from a C <strong>struct<\/strong>? There are a few differences. The key ones are:<\/p>\n<ul>\n<li><a href=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/51\/2014\/06\/2.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright wp-image-6926\" src=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/51\/2014\/06\/2-447x800.png\" alt=\"2\" width=\"200\" height=\"357\" \/><\/a>A <strong>class<\/strong> can also contain functions [called methods].<\/li>\n<li>The member variables and methods are hidden from the outside world, unless their declaration follows a <strong>public<\/strong> label.<\/li>\n<li>There can be a pair of special methods &#8211; the constructor and destructor &#8211; that are run automatically when an instance of the <strong>class<\/strong> [an object] is created and destroyed.<\/li>\n<li>Operators to work on the new data type can be defined using special methods [member functions].<\/li>\n<li>One <strong>class<\/strong> can be used as the basis for the definition of another [inheritance].<\/li>\n<li>Declaring a variable of the new type [an instance of the class; an object] requires just the name of the <strong>class<\/strong> &#8211; the keyword <strong>class<\/strong> is not required.<\/li>\n<\/ul>\n<p>Most of these are illustrated in the example here.<\/p>\n<p><a href=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/51\/2014\/06\/3.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright wp-image-6929\" src=\"https:\/\/blogs.sw.siemens.com\/wp-content\/uploads\/sites\/51\/2014\/06\/3-478x800.png\" alt=\"3\" width=\"200\" height=\"335\" \/><\/a>But what about a <strong>struct<\/strong> in C++? The last example here gives a clue. The <em>only<\/em> difference between a <strong>struct<\/strong> and <strong>class<\/strong> in C++ is the default accessibility of member variables and methods. In a <strong>struct<\/strong> they are public; in a <strong>class<\/strong> they are private.<\/p>\n<p>Having imparted this information, I urge you not to exploit it too heavily. A key priority when you are writing code is to ensure that it is readable [=maintainable]. Somebody &#8211; it could be you &#8211; might need to look at this code in a year&#8217;s time and understand what it does. I have heard advice as follows: Assume that the person that will maintain your code is an armed psychopath, who has limited patience and knows your home address.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Maybe I should apologize for going on about C++ yet again, but, following on from the recent online lecture series&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[313,300,340],"industry":[],"product":[],"coauthors":[],"class_list":["post-6922","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-embedded-software","tag-programming-languages"],"_links":{"self":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/6922","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=6922"}],"version-history":[{"count":0,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/6922\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=6922"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=6922"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=6922"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=6922"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=6922"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=6922"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}