-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-struct-class.cpp
More file actions
134 lines (106 loc) · 2.88 KB
/
1-struct-class.cpp
File metadata and controls
134 lines (106 loc) · 2.88 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
#include <stdio.h>
#include <string.h>
/* C-like struktura
*/
struct my_struct_t {
// vzdycky public
int type;
char name[64];
// struktura muze mit konstruktor
my_struct_t() {
}
// konstruktor muzeme pretezovat
my_struct_t(int t, const char* n) {
type = t;
strcpy_s(name, n);
}
};
void my_set_type(my_struct_t* s, int type) {
if (s == NULL) return;
s->type = type;
}
int my_get_type(my_struct_t* s) {
if (s == NULL) return -1;
return s->type;
}
void my_set_name(my_struct_t* s, const char* name) {
if (s == NULL) return;
memset(s->name, 0, sizeof(strlen(s->name) * sizeof(char)));
strcpy_s(s->name, name);
}
const char* my_get_name(my_struct_t* s) {
if (s == NULL) return NULL;
return s->name;
}
void my_printf_info(my_struct_t* s) {
if (s == NULL) return;
printf("type: %d\n", s->type);
printf("name: %s\n", s->name);
}
/*
*/
class my_class_t {
// private prvky
int type;
char name[64];
// public prvky
public:
// konstruktor
my_class_t() {
}
my_class_t(int type, const char* name) {
this->type = type;
strcpy_s(this->name, name);
}
// destruktor
~my_class_t() {
}
void setType(int type) {
this->type = type;
}
int getType() {
return this->type;
}
void setName(const char* name) {
strcpy_s(this->name, name);
}
const char* getName() {
return this->name;
}
void printInfo() {
printf("type: %d\n", this->type);
printf("name: %s\n", this->name);
}
};
/*
*/
int main() {
// volani bez uziti konstruktoru
my_struct_t static_struct;
static_struct.type = 1;
strcpy_s(static_struct.name, "Ondrej");
printf("my_struct_t:\ttype: %d\tname: %s\n", static_struct.type, static_struct.name);
printf("where i am: %d %s\n", __LINE__, __FILE__);
// pomoci funkci
my_set_type(&static_struct, 5);
my_printf_info(&static_struct);
my_set_name(&static_struct, "Tomas");
my_printf_info(&static_struct);
// volani s uzitim konstruktoru
my_struct_t static_struct_1(1, "Michal");
my_printf_info(&static_struct_1);
printf("where i am: %d %s\n", __LINE__, __FILE__);
// class
my_class_t static_class;
static_class.setType(1);
static_class.setName("Samuel");
// neni povoleno kvuli private pristupu
//printf("my_struct_t:\ttype: %d\tname: %s\n", static_class.type, static_class.name);
printf("my_struct_t:\ttype: %d\tname: %s\n", static_class.getType(), static_class.getName());
static_class.printInfo();
// volani s uzitim konstruktoru
my_class_t static_class_1(5, "Ivo");
static_class_1.printInfo();
printf("where i am: %d %s\n", __LINE__, __FILE__);
return 0;
}