{"id":139,"date":"2014-04-20T14:46:00","date_gmt":"2014-04-20T13:46:00","guid":{"rendered":"https:\/\/debuggersspace.com\/index.php\/2014\/04\/20\/what-are-the-differences-between-struct-and-class-in-c\/"},"modified":"2024-10-05T11:22:13","modified_gmt":"2024-10-05T10:22:13","slug":"what-are-the-differences-between-struct-and-class-in-c","status":"publish","type":"post","link":"https:\/\/debuggersspace.com\/index.php\/2014\/04\/20\/what-are-the-differences-between-struct-and-class-in-c\/","title":{"rendered":"What are the differences between struct and class in C++?"},"content":{"rendered":"<div class='booster-block booster-read-block'>\n                <div class=\"twp-read-time\">\n                \t<i class=\"booster-icon twp-clock\"><\/i> <span>Read Time:<\/span>1 Minute, 39 Second                <\/div>\n\n            <\/div><h1>Differences Between Struct and Class in C++<\/h1>\n<p>In C++, both <code>struct<\/code> and <code>class<\/code> are used to define user-defined data types. They are quite similar but have some key differences, especially when it comes to access specifiers and usage conventions. Let\u2019s dive into the differences between them:<\/p>\n<h2>Key Differences<\/h2>\n<table>\n<tbody>\n<tr>\n<th>Feature<\/th>\n<th>Struct<\/th>\n<th>Class<\/th>\n<\/tr>\n<tr>\n<td><strong>Default Access Specifier<\/strong><\/td>\n<td>Members are <strong>public<\/strong> by default.<\/td>\n<td>Members are <strong>private<\/strong> by default.<\/td>\n<\/tr>\n<tr>\n<td><strong>Usage Convention<\/strong><\/td>\n<td>Commonly used for <strong>plain data structures<\/strong>.<\/td>\n<td>Commonly used for <strong>object-oriented designs<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td><strong>Inheritance<\/strong><\/td>\n<td>Inheritance is <strong>public<\/strong> by default.<\/td>\n<td>Inheritance is <strong>private<\/strong> by default.<\/td>\n<\/tr>\n<tr>\n<td><strong>Member Functions<\/strong><\/td>\n<td>Can have constructors, destructors, and member functions.<\/td>\n<td>Can have constructors, destructors, and member functions.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Examples<\/h2>\n<h3>Example of Struct<\/h3>\n<pre><code>struct Person {\r\n    std::string name;\r\n    int age;\r\n\r\n    void printDetails() {\r\n        std::cout &lt;&lt; \"Name: \" &lt;&lt; name &lt;&lt; \", Age: \" &lt;&lt; age &lt;&lt; std::endl;\r\n    }\r\n};\r\n\r\nint main() {\r\n    Person p;\r\n    p.name = \"Alice\";\r\n    p.age = 25;\r\n    p.printDetails(); \/\/ Output: Name: Alice, Age: 25\r\n    return 0;\r\n}<\/code><\/pre>\n<p>In this <code>struct<\/code> example, both <code>name<\/code> and <code>age<\/code> are public by default.<\/p>\n<h3>Example of Class<\/h3>\n<pre><code>class Person {\r\n    std::string name;\r\n    int age;\r\n\r\npublic:\r\n    Person(std::string n, int a) : name(n), age(a) {}\r\n\r\n    void printDetails() {\r\n        std::cout &lt;&lt; \"Name: \" &lt;&lt; name &lt;&lt; \", Age: \" &lt;&lt; age &lt;&lt; std::endl;\r\n    }\r\n};\r\n\r\nint main() {\r\n    Person p(\"Bob\", 30);\r\n    p.printDetails(); \/\/ Output: Name: Bob, Age: 30\r\n    return 0;\r\n}<\/code><\/pre>\n<p>In this <code>class<\/code> example, <code>name<\/code> and <code>age<\/code> are private by default and can only be accessed or modified through member functions.<\/p>\n<h2>Conclusion<\/h2>\n<p>Both <code>struct<\/code> and <code>class<\/code> in C++ allow you to create custom data types with similar functionality. The key difference lies in the default access specifier:<\/p>\n<ul>\n<li><strong>Struct<\/strong> members are public by default, and it is generally used for simpler data structures.<\/li>\n<li><strong>Class<\/strong> members are private by default, making it suitable for complex object-oriented designs where encapsulation is important.<\/li>\n<\/ul>\n<p>Although they are functionally similar, choosing between a <code>struct<\/code> and a <code>class<\/code> often comes down to style and the intended use of your code.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Differences Between Struct and Class in C++ In C++, both struct and class are used to define user-defined data types. They are quite similar but have some key differences, especially when it comes to access specifiers and usage conventions. Let\u2019s dive into the differences between them: Key Differences Feature Struct Class Default Access Specifier Members [&hellip;]<\/p>\n","protected":false},"author":43,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[31,170,104,20,177,188],"tags":[212,213,214],"class_list":["post-139","post","type-post","status-publish","format-standard","hentry","category-c","category-c-2","category-freshers","category-oops","category-programming-languages","category-technical-explorations","tag-c","tag-class","tag-struct"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Himanshu Namdeo","author_link":"https:\/\/debuggersspace.com\/author\/admin\/"},"uagb_comment_info":0,"uagb_excerpt":"Differences Between Struct and Class in C++ In C++, both struct and class are used to define user-defined data types. They are quite similar but have some key differences, especially when it comes to access specifiers and usage conventions. Let\u2019s dive into the differences between them: Key Differences Feature Struct Class Default Access Specifier Members&hellip;","_links":{"self":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/139"}],"collection":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/comments?post=139"}],"version-history":[{"count":3,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/139\/revisions"}],"predecessor-version":[{"id":852,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/139\/revisions\/852"}],"wp:attachment":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/media?parent=139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/categories?post=139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/tags?post=139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}