Difference between C structures and C++ structures
In C++, struct and class are exactly the same things, except for that struct defaults to public visibility and class defaults to private visibility.
Some important differences between the C and C++ structures:
- Member functions inside structure: Structures in C cannot have member functions inside structure but Structures in C++ can have member functions along with data members.
- Direct Initialization: We cannot directly initialize structure data members in C but we can do it in C++.
C
// C program to demonstrate that direct// member initialization is not possible in C#include <stdio.h>structRecord {intx = 7;};// Driver Programintmain(){structRecord s;printf("%d", s.x);return0;}/* Output : Compiler Error6:8: error: expected ':', ', ', ';', '}' or'__attribute__' before '=' tokenint x = 7;^In function 'main': */chevron_rightfilter_noneC++
// CPP program to initialize data member in c++#include <iostream>usingnamespacestd;structRecord {intx = 7;};// Driver Programintmain(){Record s;cout << s.x << endl;return0;}// Output// 7chevron_rightfilter_none
Output:7
- Using struct keyword: In C, we need to use struct to declare a struct variable. In C++, struct is not necessary. For example, let there be a structure for Record. In C, we must use “struct Record” for Record variables. In C++, we need not use struct and using ‘Record‘ only would work.
- Static Members: C structures cannot have static members but is allowed in C++.
C
// C program with structure static memberstructRecord {staticintx;};// Driver programintmain(){return0;}/* 6:5: error: expected specifier-qualifier-listbefore 'static'static int x;^*/chevron_rightfilter_noneC++
// C++ program with structure static memberstructRecord {staticintx;};// Driver programintmain(){return0;}chevron_rightfilter_none
This will generate an error in C but no error in C++. - Constructor creation in structure: Structures in C cannot have constructor inside structure but Structures in C++ can have Constructor creation.
C
// C program to demonstrate that Constructor is not allowed#include <stdio.h>structStudent {introll;Student(intx){roll = x;}};// Driver Programintmain(){structStudent s(2);printf("%d", s.x);return0;}/* Output : Compiler Error[Error] expected specifier-qualifier-listbefore 'Student'[Error] expected declaration specifiers or'...' before numeric constant[Error] 's' undeclared (first use5555555555in this function)In function 'main': */chevron_rightfilter_noneC++
// CPP program to initialize data member in c++#include <iostream>usingnamespacestd;structStudent {introll;Student(intx){roll = x;}};// Driver Programintmain(){structStudent s(2);cout << s.roll;return0;}// Output// 2chevron_rightfilter_none
Output:2
- sizeof operator: This operator will generate 0 for an empty structure in C whereas 1 for an empty structure in C++.
// C program to illustrate empty structure#include <stdio.h>// empty structurestructRecord {};// Driver programintmain(){structRecord s;printf("%d\n",sizeof(s));return0;}chevron_rightfilter_noneOutput in C:
0
Output in C++:
1
- Data Hiding: C structures do not allow concept of Data hiding but is permitted in C++ as C++ is an object oriented language whereas C is not.
- Access Modifiers: C structures do not have access modifiers as these modifiers are not supported by the language. C++ structures can have this concept as it is inbuilt in the language.
Related Article: Structure vs Class in C++
This article is contributed by Shubham Chaudhary. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Difference between Stack and Queue Data Structures
- Structures in C
- Structures in C++
- Policy based data structures in g++
- C program to store Student records as Structures and Sort them by Name
- Difference between 1G and 2G
- Difference between Blu-ray and DVD
- Difference between ++*p, *p++ and *++p
- Difference between OOP and POP
- Difference between CLI and GUI
- Difference between H.323 and SIP
- Difference Between BFS and DFS
- Web 1.0, Web 2.0 and Web 3.0 with their difference
- Difference between LAN, MAN and WAN
- Difference between RPC and RMI
Improved By : gyanendra371



