{"id":217,"date":"2020-08-31T14:53:24","date_gmt":"2020-08-31T14:53:24","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/cplusplus\/?p=217"},"modified":"2020-08-31T14:53:24","modified_gmt":"2020-08-31T14:53:24","slug":"c-data-structures","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/cplusplus\/c-data-structures\/","title":{"rendered":"C++ Data Structures"},"content":{"rendered":"<p>In this lesson, we would learn about C++ data structures. We&#8217;ll see how to create them and how to use them in programs. We&#8217;ll cover the following sub-topics:<\/p>\n<ol>\n<li><a href=\"#t1\">What are Structures?<\/a><\/li>\n<li><a href=\"#t2\">Declaring a Structure<\/a><\/li>\n<li><a href=\"#t3\">Accessing Members of a Structure<\/a><\/li>\n<li><a href=\"#t4\">Using the <em>typedef<\/em> Keyword<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. What are Structures?<\/strong><\/h4>\n<p>A structure in C++ is a collection of variables of different data type under a common name. It is similar to a record in a table. So while a C++ Array is a collection of variables of same type, a structure contains variables of different types.<\/p>\n<p>Use cases include:<\/p>\n<ul>\n<li>a student information made up of id, name, admission date, age, and department<\/li>\n<li>a book record consisting of if, title, author and publish date<\/li>\n<\/ul>\n<p>You may probably think of other examples as well<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Declaring\u00a0 a Structure<\/strong><\/h4>\n<p>Use use the <em><strong>struct<\/strong> <\/em>keyword to define a structure. Specify the struct keyword, followed by an identifier. Then inside curly braces, you can declare all the variables (called members) that make up the structure.<\/p>\n<p>For example:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">struct<\/span> Student {\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> id;\r\n\tstring firstname;\r\n\tstring lastname;\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> age;\r\n\tstring department;\r\n};\r\n<\/pre>\n<p>Here a structure <em><strong>Student<\/strong> <\/em>is defined and is has five members: id, firstname, lastname, age and department.<\/p>\n<p>However, when a struct is created, unlike arrays, no memory is allocated. It&#8217;s similar to declaring a variable without initialization.<\/p>\n<p>After declaring your struct, you can then declare structure type variable in same way you declare other variables. For example<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Student archana;\r\n<\/pre>\n<p>Here, we have declare a variable archana which is of type Student. At this point, the compiler then allocates the required memory.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Accessing Member Variables of Struct<\/strong><\/h4>\n<p>Members of a struct variable are accessed using the<strong> member access operator, dot (.)<\/strong>.<\/p>\n<p>Assuming we want to access department of structure variable archana and assign a value to it. We can achieve it using<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #333333;\">&lt;<\/span>struct_name<span style=\"color: #333333;\">&gt;<\/span>.<span style=\"color: #333333;\">&lt;<\/span>member_name<span style=\"color: #333333;\">&gt;<\/span>\r\n<\/pre>\n<p>Like so<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">archana.department <span style=\"color: #333333;\">=<\/span> <span style=\"background-color: #fff0f0;\">\"Computer Science\"<\/span>\r\n<\/pre>\n<p>The program below show how a struct works. We define a struct, then assign value from the user input<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #557799;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">using<\/span> <span style=\"color: #008800; font-weight: bold;\">namespace<\/span> std;\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">struct<\/span> Student {\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> id;\r\n\tstring firstname;\r\n\tstring lastname;\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> age;\r\n\tstring department;\r\n};\r\n\r\n<span style=\"color: #888888;\">\/\/ main function<\/span>\r\n<span style=\"color: #333399; font-weight: bold;\">int<\/span> <span style=\"color: #0066bb; font-weight: bold;\">main<\/span> () {\r\n\r\n\tStudent archana;\r\n\r\n\tarchana.id <span style=\"color: #333333;\">=<\/span> <span style=\"color: #0000dd; font-weight: bold;\">1<\/span>;\r\n\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Enter Student firstname: \"<\/span>;\r\n\tcin<span style=\"color: #333333;\">&gt;&gt;<\/span> archana.firstname;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Enter Student lastname: \"<\/span>;\r\n\tcin <span style=\"color: #333333;\">&gt;&gt;<\/span> archana.lastname;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Enter age: \"<\/span>;\r\n\tcin <span style=\"color: #333333;\">&gt;&gt;<\/span> archana.age;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Enter department: \"<\/span>;\r\n\tcin <span style=\"color: #333333;\">&gt;&gt;<\/span> archana.department;\r\n\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"<\/span><span style=\"color: #666666; font-weight: bold; background-color: #fff0f0;\">\\n<\/span><span style=\"background-color: #fff0f0;\">******* Student Details ********\"<\/span><span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"ID:         \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> archana.id        <span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Firstname:  \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> archana.firstname <span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Lastname:   \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> archana.lastname  <span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Age:        \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> archana.age       <span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\tcout <span style=\"color: #333333;\">&lt;&lt;<\/span> <span style=\"background-color: #fff0f0;\">\"Department: \"<\/span> <span style=\"color: #333333;\">&lt;&lt;<\/span> archana.department<span style=\"color: #333333;\">&lt;&lt;<\/span>endl;\r\n\r\n   <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #0000dd; font-weight: bold;\">0<\/span>;\r\n}\r\n<\/pre>\n<p>One thing to note is that the structure is defined outside the main method.<\/p>\n<p>The output of this program is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">Enter Student firstname: Kindson\r\nEnter Student lastname: Munonye\r\nEnter age: 34\r\nEnter department: Computer Science\r\n\r\n******* Student Details ********\r\nID:         1\r\nFirstname:  Kindson\r\nLastname:   Munonye\r\nAge:        34\r\nDepartment: Computer\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Using the <em>typedef<\/em> Keyword<\/strong><\/h4>\n<p>Another way to create a structure is to use the <strong>typedef<\/strong> keyword. This make it kind of easier. For example, the code below creates a student structure.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">typedef<\/span> <span style=\"color: #008800; font-weight: bold;\">struct<\/span> {\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> regNo;\r\n\tstring name;\r\n\t<span style=\"color: #333399; font-weight: bold;\">int<\/span> email;\r\n\tstring course;\r\n} Student;\r\n<\/pre>\n<p>With this, you can then create Student objects just like before.<\/p>\n<p>In addition to creation of structures, the <strong><em>typedef<\/em> <\/strong>keyword can also be used to define variables of other data types. Here are a few examples:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">typedef<\/span> <span style=\"color: #333399; font-weight: bold;\">long<\/span> <span style=\"color: #333399; font-weight: bold;\">int<\/span> myInt; <span style=\"color: #888888;\">\/\/I just created my own data type!<\/span>\r\n\r\nmyInt x, y; <span style=\"color: #888888;\">\/\/x any y are now long ints<\/span>\r\n<\/pre>\n<p>So this is the much we have for C++ Structures!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this lesson, we would learn about C++ data structures. We&#8217;ll see how to create them and how to use them in programs. We&#8217;ll cover &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[33,34],"class_list":["post-217","post","type-post","status-publish","format-standard","hentry","category-c-tutorials","tag-struct","tag-typedef"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/217","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/comments?post=217"}],"version-history":[{"count":3,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/217\/revisions"}],"predecessor-version":[{"id":220,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/posts\/217\/revisions\/220"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/media?parent=217"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/categories?post=217"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/cplusplus\/wp-json\/wp\/v2\/tags?post=217"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}