Differences Between C Structures and C++ Structures
The below table clearly describes the differences between C and C++ programming languages Structures concepts where the core principle concepts like static members, data handling, pointers, access modifiers, the importance of the struct keyword and its necessity to declare the type variables, why C++ supports the access modifiers while C is not supporting it. Some code to see the working of the member functions inside the structure, declaring the Structures accessing them etc.
Table depicting the differences between C and C++ Structures.
C Structures | C++ Structures |
In C Structures, we cannot have static members in it. | In C++ Structures, we can have static members in it. |
In C structures Data handling concept is not possible. | In C++ structures Data handling concept is not possible. |
In C Structures, only the pointers to struct are allowed in it. | In C++ Structures, it can have both the pointers and the references attached to the struct. |
In C Structures, they do not have access modifiers. Before implementing any structure functionality in C structures, it is necessary to use the 'struct' keyword to declare the structure type of variables. | In C++ Structures, it supports the access modifiers. In C++ structures, before we implement any Structure functionality, it is not necessary to use the 'struct' keyword to declare the structure type of variables. |
In C structures, direct Initialization of the data members is not possible. | In C++ structures there is direct Initialization of the data members is possible. |
In C structures, they cannot have a constructor inside a structure | In C++, systems can have a constructor inside a structure |
In C structures, only the data members are allowed; it cannot have the member functions. | In C++ Structures, it can hold both the member functions and the data members. |
In C structures, the data hiding feature is not available. | In C++ structures, the data hiding feature is available. |
In C structures, the sizeof() operator will generate 0 for an open system. | In C++ structures the sizeof() operator will generate 1 for empty structure. |
C code to Demonstrate C Structures
Code -1
#include<stdio.h>
struct a{
int data_1;
int data_2;
};
int main()
{
struct a s_data = {1227, 1324,12121,2211,788776,2221,12};
printf("%d %d", s_data.data_1,s_data.data_2, s_data.data_3, s_data.data_4, s_data.data_5, s_data.data_6, s_data.data_7);
return 0;
}
Output
1227 1324 12121 2211 788776 2221 12
Code-2
//C code to create a structure
struct myStruct {
int mynum;
char myletter;
};
int main() {
struct myStruct s1pro;
s1pro.mynum = 3;
s1pro.myletter = 'C';
printf("number: %d\n", s1pro.mynum);
printf("letter: %c\n", s1pro.myletter);
return 0;
}
Output
number: 3
letter: C
Code-2
#include <stdio.h>
#include <string.h>
struct books {
char title[50];
char author[50];
};
int main( ) {
struct books book1;
struct books book2;
strcpy( book1.title, "Coding in Java");
strcpy( book1.author, "Ali zuha");
strcpy( book2.title, "Bill");
strcpy( book2.author, " zara Ali");
printf( "book 1 title : %s\n", book1.title);
printf( "book 1 author : %s\n", book1.author);
printf( "book 2 title : %s\n", book2.title);
printf( "book 2 author : %s\n", book2.author);
return 0;
}
Output
book 1 title : Coding in Java
book 1 author : Ali zuha
book 2 title : Bill
book 2 author : zara Ali
C++ Code to Demonstrate C++ Structures
Code
#include <iostream>
using namespace std;
struct Point {
int x, y;
};
int main(){
struct Point p1 = { 110, 221 };
//code for accessing members of the point p1
p1.x = 40;
cout << "x = " << p1.x << ", y = " << p1.y;
return 0;
}
Output
x = 40, y = 221
C++ Code-2
#include <iostream>
using namespace std;
struct Point {
int x, y, a, b, c, d, e, f, g, h;
};
int main()
{
//code to create an array of structures
struct Point arr[20];
//code to access array members
arr[0].x = 10;
arr[0].y = 20;
arr[0].a = 30;
arr[0].b = 40;
arr[0].c = 50;
arr[0].d = 60;
arr[0].e = 70;
arr[0].f = 80;
arr[0].g = 90;
arr[0].h = 100;
cout << arr[0].x <<endl;
cout << arr[0].y <<endl;
cout << arr[0].a <<endl;
cout << arr[0].b <<endl;
cout << arr[0].c <<endl;
cout << arr[0].d <<endl;
cout << arr[0].e <<endl;
cout << arr[0].f <<endl;
cout << arr[0].g <<endl;
cout << arr[0].h <<endl;
return 0;
}
Output
10
20
30
40
50
60
70
80
90
100