-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOctober_13.cpp
158 lines (117 loc) · 2.72 KB
/
October_13.cpp
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
/* October 13 */
// Dynamic memory allocation in C++ -----------
struct Node {
int data;
Node *next;
};
Node *np = new Node;
..
delete np;
// use new/delete in C++
// Allocating arrays -----------------------------
type *p = new type[length]; // allocation
delete [] p // deallocation
delete[] // to delete arrays
delete // to delete non-arrays
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
// arr on stack, pointing to heap memory
int * arr = new int [n];
for (int i = 0; i < n; ++i) {
arr[i] i*i;
cout << arr[i] << endl;
}
delete [] arr;
return 0;
}
// allocation: stack vs. heap ----------------------
// local variables allocated on the stack
// they disappear when the stack is popped
// heap-allocated data lives on when the stack is popped
// allocations: examples ---------------------------
Node getNode() { // expensive - n is copied on return
Node n;
return n;
}
Node *getNode() { // unsage - n is dead on return
Node n;
return &n;
}
Node *getNode() { // do this
Node *np = new Node;
return np;
}
// operator overloading ---------------------------
#include <iostream>
using namespace std;
struct Vector {
int x, y;
};
Vector operator+(const Vector &v2, const Vector &v2) {
Vector v;
v.x = v1.x + v2.x;
v.y = v1.y + v2.y;
}
Vecotr operator*(const int k, const Vector &v1) {
Vector v;
v.x = k * v1.x;
v.y = k * v1.y;
return v;
}
int main() {
Vector v1, v2;
v1.x = 1;
v1.y = 1;
v2.x = 2;
v2.y = 2;
Vector v3 = v1 + v2;
Vector v4 = v1 * 5;
Vector v5 = 7 * v2;
cout << v3.x << ", " << v3.y << endl;
cout << v4.x << ", " << v4.y << endl;
cout << v5.x << ", " << v5.y << endl;
return 0;
}
// overloading i/o operators
struct Grade {
int theGrade;
};
ostream &operator<<(ostream &out, const Grade &g) {
out << g.theGrade << "%";
return out;
}
istream &operator>>(istream &in, Grade &g) {
in >> g.theGrade;
if (g.theGrade < 0) g.theGrade = 0;
if (g.theGrade > 100) g.theGrade = 100;
return in;
}
// Preprocessing ---------------------------------------
// transform the program before compilation
// preprocessor directives - lines beginning with #
#include <iostream> // search in include dir
#include "file.h" // search in current dir
// #define ----------------------------------------------
// set a preprocessor variables
// use #define VAR VALUE
#define FLAG
#define MAX 10
...
int x[MAX]
...
// to define constants, use const definitions
// #define is not recommended over this ^
// constants defined with #define can be useful for conditional compilation
// together with #if, #elif, and #endif
// special case: #if 0
// conditional compilation
#define os UNIX
#if os == UNIX
int main () {}
#elif os == WINDOWS
int winmain () {
}
#endif