-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3. Structures.cpp
More file actions
201 lines (157 loc) · 5.04 KB
/
3. Structures.cpp
File metadata and controls
201 lines (157 loc) · 5.04 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include<iostream>
#include<vector>
using namespace std;
// Structures are used to create custom data types
// In CS we call it ADT(abstract data type) || Abstraction which is a general model of something
//! ------- Creating Structure
// We use PascalCase to make it || We defintion we can't allocate memory || We just tell compiler that it has these things
// typedef struct CustomerData{
// int id;
// string name;
// string email;
// } ep;
//! ------- Initializing Structure
// struct Movie{
// string Title; // String have ' ' by default
// int releaseYear = 0; // We can also gave it a default value
// bool isPopular; // Bool have false by default
// };
//! ------- Unpacked structure
// struct Movie{
// string Title;
// int releaseYear;
// };
//! ------- Nested structure
// struct Address{
// string street;
// string city;
// string zipcode;
// };
// string Customer{
// string name;
// Address customerAddress;
// };
//! ------- Comparing structure
// struct Movie
// {
// string title;
// int releaseYear;
// };
//! ------- Structures with methods
// struct Movie
// {
// string title;
// int releaseYear;
// //* Method is a function which is a part of an object
// bool equals(Movie movie){
// return (
// title == movie.title &&
// releaseYear == movie.releaseYear
// );
// };
// };
//! ------- Defining Enumeration
//* Enum is interally represented by integer || With this we can represent new type that has same constants integer
// enum Action{
// List = 1,
// Add ,
// Update
// };
//! ------- Strongly typed enumerators
// enum class Action{
// List = 1,
// Add ,
// Update
// };
// enum class Action2{
// List = 1,
// Add ,
// Update
// };
//! ------- Unions
union Money
{
/* data */
int carPrice;
int bikePrice;
};
int main(){
//! ------- Creating Structures
// CustomerData customerData; // At this time compiler allocate memory
// cout << "Enter Customer Id: ";
// cin >> customerData.id;
// cout << "Enter Name: ";
// cin >> customerData.name;
// cout << "Enter Email: ";
// cin >> customerData.email;
// cout << "\nName: " << customerData.name << "\t" << "Email: " << customerData.email << "\t" << "Id: " << customerData.id;
//! ------- Initializing Structures
// Movie movie = { "BFM", 2023 };
// cout << "Title: " << movie.Title << " Release Year: " << movie.releaseYear << " Is Popular: " << movie.isPopular;
//! ------- Structured binding || Destructuring || Unpacking
// Movie movie = { "BFM", 2023 };
// auto [Title, releaseYear] {movie};
// cout << "Title: " << movie.Title << " Release Year: " << movie.releaseYear;
//! ------- Array of structures
// vector<Movie> movies;
// // Movie movie = {"BFM", 2023};
// movies.push_back({"BFM 1", 2023});
// movies.push_back({"BFM 2", 2024}); // It's like a regular array
// for(auto movie : movies)
// cout << "Movie Title: " << movie.Title << "\t";
//! ------- Nesting structures
// Customer customer = {
// "Ahad",
// {
// "Nain Sukh",
// "Lahore",
// "54000"
// }
// }
// cout << customer.customerAddress.city;
//! ------- Comparing structures
// Movie Movie1 = { "BFM", 2023 };
// Movie Movie2 = { "BFM", 2023 };
// if(Movie1.title == Movie2.title && Movie1.releaseYear == Movie2.releaseYear){
// cout << "Same";
// }
//! ------- Structures with methods
// Movie Movie1 = { "BFM", 2023 };
// Movie Movie2 = { "BFM", 2023 };
// if(Movie1.equals(Movie2))
// cout << "Same";
//! ------- Defining Enumerations
//* It is an other way to create custom types
// int select;
// cout << "1. List invoice" << endl << "2. Add invoice" << endl << "3. Update invoice" << endl << "Select: " << endl;
// cin >> select;
// if(select == Action::List){
// cout << "List invoice";
// }
// else if(select == Action::Add){
// cout << "Add invoice";
// }
// else if(select == Action::Update){
// cout << "Update invoice";
// }
//! ------- Strongly typed enumerators
//* We can't have two enumerators with same members | We need to cast the action into integer
// int select;
// cout << "1. List invoice" << endl << "2. Add invoice" << endl << "3. Update invoice" << endl << "Select: " << endl;
// cin >> select;
// if(select == static_cast<int> (Action::List)){
// cout << "List invoice";
// }
// else if(select == static_cast<int> (Action::Add)){
// cout << "Add invoice";
// }
// else if(select == static_cast<int> (Action::Update)){
// cout << "Update invoice";
// }
//! ------- Unions
//* Are like a structures but they provide better memory management
Money p1;
p1.carPrice = 10000;
p1.bikePrice = 50000; // It will overwrite other values
cout << p1.carPrice << ' ' << p1.bikePrice << endl;
}