Skip to content

Commit b215cfe

Browse files
committed
add 012_Structures.cpp
1 parent da95389 commit b215cfe

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

012_Structures.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include<iostream>
2+
3+
4+
struct Point {
5+
int x,y;
6+
};
7+
8+
struct Shelf {
9+
const char *title;
10+
const char *author;
11+
union {
12+
float dollars;
13+
int euro;
14+
};
15+
enum {
16+
Dollars = 1,
17+
Euro = 2
18+
} currency;
19+
}book;
20+
21+
22+
enum Side { Left, Right };
23+
struct House {
24+
const char *adresse;
25+
unsigned int nHouse_num;
26+
Side enSide;
27+
};
28+
29+
int main(int argc, char const *argv[]) {
30+
struct Point stPoint; //struct keyword optional in c++, but must in C
31+
stPoint.x = 33;
32+
stPoint.y = 50;
33+
std::cout << stPoint.x << '\n';
34+
35+
Point stPoint2 = {5,10}; // x=5 and y=10
36+
Point stPoint3[40] = {}; //array of structure i.e. 40 points
37+
std::cout << stPoint3[0].x << '\n';
38+
39+
// strcpy(book.title, "Live Good");
40+
book.title = "Live Good";
41+
book.author = "Unknown";
42+
book.euro = 25.3;
43+
book.currency = Shelf::Euro;
44+
std::cout << book.title << '\n';
45+
46+
//declare enum inside stucture
47+
House stHouse;
48+
stHouse.adresse = "Max Müller";
49+
stHouse.nHouse_num = 123;
50+
stHouse.enSide = Left; // or Side::Left
51+
House stPeter = {"Peter Klein", 124, Right};
52+
std::cout << stPeter.adresse << '\n';
53+
54+
55+
return EXIT_SUCCESS;
56+
}

0 commit comments

Comments
 (0)