-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructures.cpp
More file actions
65 lines (48 loc) · 1.44 KB
/
structures.cpp
File metadata and controls
65 lines (48 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
// Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure.
struct Rectangle
{
int length;
int breadth;
void getArea()
{
std::cout << length * breadth << std::endl;
}
};
struct Student
{
int grade;
char name[25];
char address[50];
};
struct Card
{
int face;
int shape;
int color;
};
int main()
{
// *** Creating object on Stack
Rectangle r;
r.breadth = 2;
r.length = 5;
r.getArea(); // 10
std::cout << sizeof(r) << std::endl; // 8 bytes (4 bytes for length + 4 bytes for breadth)
Rectangle k = {3, 5};
k.getArea(); // 15
// **** Creating object on Heap
Rectangle *h = new Rectangle();
h->breadth = 10;
h->length = 2;
h->getArea(); // 20
// deallocating memory
delete h;
// ****
Student s = {2, "Mert", "foo"};
// In C, the size of a struct may include padding to ensure proper alignment of its members. The padding is added to align data members according to the architecture's alignment requirements, which can result in the size of the struct being larger than the sum of the sizes of its individual members.
std::cout << sizeof(s) << std::endl;
Card deck[52]; // array of Card struct
std::cout << "sizeof card deck: " << sizeof(deck) << std::endl; // (4 + 4 + 4) * 52 = 624
return 0;
}